BicycleController.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public enum VelocityMode
  5. {
  6. ApplySpeedToRigidBody,
  7. ApplyTorqueToWheelCollider,
  8. ApplySpeedToWheelCollider
  9. }
  10. public class BicycleController : MonoBehaviour
  11. {
  12. [Header("GameObjects")] public GameObject rearWheel;
  13. public GameObject frontWheel;
  14. public GameObject crank;
  15. public GameObject bike;
  16. //public GameObject pedalL;
  17. //public GameObject pedalR;
  18. public GameObject fork;
  19. public Transform centerOfMass;
  20. [Header("Configuration")] public VelocityMode velocityMode = VelocityMode.ApplyTorqueToWheelCollider;
  21. [Header("Values")] public float oneRotationSpeed = 2.7f;
  22. public float crankMultiplier = 2f;
  23. public List<AxleInfo> axleInfos; // the information about each individual axle
  24. public float maxMotorTorque = 12f; // maximum torque the motor can apply to wheel
  25. public float maxSteeringAngle = 5f; // maximum steer angle the wheel can have
  26. [Range(0, 1)] public float relativeLeanAmount = 0.01f;
  27. public Transform leftWheels;
  28. public Transform rightWheels;
  29. private float currentSteerAngle = 0f;
  30. private float currentSpeed = 0f;
  31. private float currentLeaningAngle = 0f;
  32. public float rotSpeed = 10;
  33. private Vector3[] wheelPositions;
  34. public Rigidbody rb;
  35. private Quaternion startForkRot;
  36. private Vector3 upDirection = Vector3.up;
  37. // Start is called before the first frame update
  38. void Start()
  39. {
  40. //rb = GetComponent<Rigidbody>();
  41. rb.centerOfMass = centerOfMass.localPosition;
  42. //startForkRot = fork.transform.localRotation;
  43. wheelPositions = new Vector3[axleInfos.Count];
  44. for (int i = 0; i < axleInfos.Count; i++)
  45. {
  46. wheelPositions[i] = axleInfos[i].wheel.center;
  47. }
  48. }
  49. // Update is called once per frame
  50. void Update()
  51. {
  52. //RotateMeshes();
  53. //RotateFork();
  54. Debug.Log("rotation: " + currentSteerAngle);
  55. }
  56. public void FixedUpdate()
  57. {
  58. ApplyWheelForce();
  59. Lean();
  60. //RotateStraight();
  61. }
  62. void RotateMeshes()
  63. {
  64. RotateObject(crank, 1);
  65. //RotateObject(pedalL, -1);
  66. //RotateObject(pedalR, -1);
  67. RotateObject(rearWheel, crankMultiplier);
  68. RotateObject(frontWheel, crankMultiplier);
  69. }
  70. void RotateFork()
  71. {
  72. fork.transform.localRotation = startForkRot;
  73. fork.transform.RotateAround(fork.transform.position, fork.transform.up, maxSteeringAngle * currentSteerAngle);
  74. }
  75. void Lean()
  76. {
  77. upDirection = Vector3.Normalize(Vector3.up + transform.right *
  78. (maxSteeringAngle * relativeLeanAmount * currentSteerAngle * rb.velocity.magnitude) / 100);
  79. }
  80. void ApplyWheelForce()
  81. {
  82. float motor = maxMotorTorque;
  83. float steering = maxSteeringAngle * currentSteerAngle;
  84. leftWheels.localPosition = -Vector3.up * (relativeLeanAmount * currentSteerAngle * rb.velocity.magnitude);
  85. rightWheels.localPosition = Vector3.up * (relativeLeanAmount * currentSteerAngle * rb.velocity.magnitude);
  86. if (velocityMode == VelocityMode.ApplySpeedToRigidBody)
  87. {
  88. //TODO: figure out what to do when going upwards?
  89. rb.velocity = bike.transform.forward * currentSpeed;
  90. }
  91. foreach (AxleInfo axleInfo in axleInfos)
  92. {
  93. if (axleInfo.steering)
  94. {
  95. axleInfo.wheel.steerAngle = steering;
  96. }
  97. if (velocityMode == VelocityMode.ApplySpeedToWheelCollider)
  98. {
  99. if (axleInfo.motor && rb.velocity.magnitude < currentSpeed)
  100. {
  101. axleInfo.wheel.motorTorque = motor;
  102. }
  103. else if (axleInfo.motor)
  104. {
  105. axleInfo.wheel.motorTorque = 0;
  106. }
  107. }
  108. }
  109. }
  110. public void SetSpeed(float speed)
  111. {
  112. currentSpeed = speed;
  113. }
  114. public void SetLeaningAngle(float angle)
  115. {
  116. currentLeaningAngle = angle;
  117. }
  118. public void SetSteeringAngle(float angle)
  119. {
  120. currentSteerAngle = angle;
  121. }
  122. //rotates the meshes
  123. void RotateObject(GameObject obj, float multiplier)
  124. {
  125. obj.transform.Rotate(Time.deltaTime * rb.velocity.magnitude * (360f / oneRotationSpeed) * multiplier, 0, 0);
  126. //obj.transform.Rotate(Time.deltaTime * rotSpeed * (360f / oneRotationSpeed) * multiplier, 0, 0);
  127. }
  128. public void SetTorque(float crankTorque)
  129. {
  130. foreach (AxleInfo axleInfo in axleInfos)
  131. {
  132. if (velocityMode == VelocityMode.ApplyTorqueToWheelCollider) ;
  133. {
  134. if (axleInfo.motor)
  135. {
  136. axleInfo.wheel.motorTorque = crankTorque;
  137. }
  138. }
  139. }
  140. }
  141. [System.Serializable]
  142. public class AxleInfo
  143. {
  144. public WheelCollider wheel;
  145. public bool motor; // is this wheel attached to motor?
  146. public bool steering; // does this wheel apply steer angle?
  147. }
  148. }