2
0

BikeAnimation.cs 3.2 KB

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