BikeAnimation.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System.Collections.Generic;
  2. using Controller;
  3. using Sensors;
  4. using UnityEngine;
  5. namespace Animation
  6. {
  7. public class BikeAnimation : MonoBehaviour
  8. {
  9. #region Variables
  10. [Header("Wheels - rotating around center")]
  11. public GameObject rearWheel;
  12. public GameObject frontWheel;
  13. [Header("Other with defined rotation axis")]
  14. public Rotatable crankSet;
  15. public Rotatable fork;
  16. [Header("Parameters")] public float wheelCircumference = 2.096f;
  17. public float defaultCadence = 80;
  18. private BicycleController bicycleController;
  19. private readonly Dictionary<Rotatable, Vector3> initialPositions = new Dictionary<Rotatable, Vector3>();
  20. private readonly Dictionary<Rotatable, Quaternion> initialRotations = new Dictionary<Rotatable, Quaternion>();
  21. #endregion
  22. // Start is called before the first frame update
  23. void Start()
  24. {
  25. bicycleController = GetComponentInParent<BicycleController>();
  26. initialPositions[crankSet] = crankSet.gameObject.transform.localPosition;
  27. initialPositions[fork] = fork.gameObject.transform.localPosition;
  28. initialRotations[crankSet] = crankSet.gameObject.transform.localRotation;
  29. initialRotations[fork] = fork.gameObject.transform.localRotation;
  30. }
  31. void Update()
  32. {
  33. var cadence = BikeSensorData.Instance.PowermeterData?.InstantaneousCadence ?? defaultCadence; //rpm
  34. var cadenceAngle = cadence / 60 * 360 * Time.deltaTime; //rps * 360 deg for one round
  35. RotateWheels();
  36. RotateAroundDefinedAxis(fork, bicycleController.CurrentSteerAngle);
  37. RotateAroundDefinedAxis(crankSet, cadenceAngle, false);
  38. }
  39. private void RotateWheels()
  40. {
  41. var speed = bicycleController.rigidBody.velocity.magnitude;
  42. var rps = speed / wheelCircumference;
  43. var angle = rps * Time.deltaTime;
  44. //TODO: only works for this specific model
  45. rearWheel.transform.RotateAround(rearWheel.transform.position, -rearWheel.transform.right, angle);
  46. frontWheel.transform.RotateAround(frontWheel.transform.position, -frontWheel.transform.right, angle);
  47. }
  48. private void RotateAroundDefinedAxis(Rotatable rotatable, float angle, bool resetBeforeRotate = true)
  49. {
  50. if (resetBeforeRotate)
  51. {
  52. rotatable.gameObject.transform.localPosition = initialPositions[rotatable];
  53. rotatable.gameObject.transform.localRotation = initialRotations[rotatable];
  54. }
  55. Vector3 axis;
  56. if (rotatable.rotateAxis == RotateAxis.Up)
  57. axis = rotatable.rotateAround.up;
  58. else if (rotatable.rotateAxis == RotateAxis.Forward)
  59. axis = rotatable.rotateAround.forward;
  60. else
  61. axis = rotatable.rotateAround.right;
  62. fork.gameObject.transform.RotateAround(rotatable.rotateAround.position, axis, angle);
  63. }
  64. //rotates the meshes
  65. void RotateObject(GameObject obj, float multiplier)
  66. {
  67. obj.transform.Rotate(
  68. Time.deltaTime * bicycleController.rigidBody.velocity.magnitude * (360f / 12f) * multiplier, 0,
  69. 0);
  70. //obj.transform.Rotate(Time.deltaTime * rotSpeed * (360f / oneRotationSpeed) * multiplier, 0, 0);
  71. }
  72. }
  73. }