BikeAnimation.cs 3.5 KB

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