BikeAnimation.cs 3.5 KB

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