WcBicycleController.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System.Collections.Generic;
  2. using Controller.Bicycle;
  3. using UnityEngine;
  4. using UnityEngine.Serialization;
  5. using Valve.VR.InteractionSystem;
  6. namespace Controller.Bicycle
  7. {
  8. public class WcBicycleController : MonoBehaviour, IBicycleController
  9. {
  10. #region Variables
  11. [Header("Visible Game Objects")] public GameObject rearWheel;
  12. public GameObject frontWheel;
  13. [Header("Game Objects for Physics")] public Transform centerOfMass;
  14. [FormerlySerializedAs("rb")] public Rigidbody rigidBody;
  15. public WheelConfig wheelConfig;
  16. [Header("Values")] public float offsetCollidersFromWheel = 0.25f;
  17. public float maxLeaningAngle = 35f;
  18. public float maxSteeringAngle = 80f;
  19. [Range(0, 15)] [Tooltip("Speed in m/s")]
  20. public float maxSpeed = 11.111f; //40 km/h
  21. public float maxBreakTorque = 2000f;
  22. public float maxMotorTorque = 1000f;
  23. public BicycleControllerMode ControllerMode { get; set; }
  24. public float CurrentSpeed
  25. {
  26. get => currentSpeed;
  27. set => currentSpeed = Mathf.Clamp(value, 0, maxSpeed);
  28. }
  29. public float CurrentSteerAngle
  30. {
  31. get => currentSteerAngle;
  32. set
  33. {
  34. //don't lean while standing / walking to bike
  35. if (rigidBody.velocity.magnitude < .5f) return;
  36. currentSteerAngle = Mathf.Clamp(value, -maxSteeringAngle, maxSteeringAngle);
  37. }
  38. }
  39. public float CurrentLeaningAngle
  40. {
  41. get => currentLeaningAngle;
  42. set => currentLeaningAngle = Mathf.Clamp(value, -maxLeaningAngle, maxLeaningAngle);
  43. }
  44. public Vector3 RigidBodyVelocity => rigidBody.velocity;
  45. private WheelCollider[] allWheelColliders;
  46. private float initialWheelColliderY;
  47. private float currentSteerAngle = 0f;
  48. private float currentLeaningAngle = 0f;
  49. private float currentSpeed;
  50. #endregion
  51. // Start is called before the first frame update
  52. void Start()
  53. {
  54. rigidBody.centerOfMass = centerOfMass.localPosition;
  55. allWheelColliders = wheelConfig.AllWheels;
  56. wheelConfig.AdjustToGameObjects(frontWheel.transform, rearWheel.transform, offsetCollidersFromWheel);
  57. initialWheelColliderY = allWheelColliders[0].transform.localPosition.y;
  58. }
  59. void FixedUpdate()
  60. {
  61. ApplyColliderForces();
  62. Lean();
  63. //RotateStraight();
  64. }
  65. private void ApplyColliderForces()
  66. {
  67. //offsetCollidersFromWheel = Mathf.Max(0.05f, 0.45f - rigidBody.velocity.magnitude * 0.07f);
  68. //wheelConfig.AdjustToGameObjects(frontWheel.transform, rearWheel.transform, offsetCollidersFromWheel);
  69. ControlSteer(new[] {wheelConfig.frontLeft, wheelConfig.frontRight});
  70. ControlTorque(new[] {wheelConfig.rearLeft, wheelConfig.rearRight});
  71. }
  72. private void ControlSteer(IEnumerable<WheelCollider> colliders)
  73. {
  74. //float steering = maxSteeringAngle * CurrentSteerAngle * 0.2f;
  75. //leftWheels.localPosition = -Vector3.up * (relativeLeanAmount * CurrentSteerAngle * rigidBody.velocity.magnitude * 0.2f);
  76. //rightWheels.localPosition = Vector3.up * (relativeLeanAmount * CurrentSteerAngle * rigidBody.velocity.magnitude * 0.2f);
  77. colliders.ForEach(c => c.steerAngle = CurrentSteerAngle);
  78. }
  79. private void ControlTorque(IEnumerable<WheelCollider> colliders)
  80. {
  81. var rbSpeed = rigidBody.velocity.magnitude;
  82. var speedDif = CurrentSpeed - rbSpeed;
  83. var ratio = speedDif / maxSpeed;
  84. var brakeTorque = 0f;
  85. var motorTorque = 0f;
  86. if (speedDif >= .1f) // 0.36 km/h
  87. {
  88. var torque = maxMotorTorque * ratio;
  89. //Debug.Log($"SpeedDif = {speedDif} -> applying Torque {torque} (Ratio: {ratio})");
  90. brakeTorque = 0;
  91. motorTorque = torque;
  92. }
  93. else if (speedDif <= -.1f)
  94. {
  95. var torque = -maxBreakTorque * ratio;
  96. //Debug.Log($"SpeedDif = {speedDif} -> applying brake Torque {torque} (Ratio: {ratio})");
  97. motorTorque = 0;
  98. brakeTorque = torque;
  99. }
  100. foreach (var c in colliders)
  101. {
  102. c.motorTorque = motorTorque;
  103. c.brakeTorque = brakeTorque;
  104. }
  105. }
  106. private void Lean()
  107. {
  108. //reset all wheels to being centered
  109. if (CurrentLeaningAngle == 0) //TODO: maybe add a threshold for leaning, e.g. < -0.05 and > 0.05
  110. {
  111. //leaning left, left wheels up, right wheels down
  112. ApplyOffsetToTransform(wheelConfig.frontLeft.transform, initialWheelColliderY);
  113. ApplyOffsetToTransform(wheelConfig.rearLeft.transform, initialWheelColliderY);
  114. ApplyOffsetToTransform(wheelConfig.frontRight.transform, initialWheelColliderY);
  115. ApplyOffsetToTransform(wheelConfig.rearRight.transform, initialWheelColliderY);
  116. }
  117. //CurrentLeaningAngle < 0 -> leaning left, > 0 -> right
  118. var leaningAbs = Mathf.Abs(CurrentLeaningAngle * Mathf.Deg2Rad);
  119. //calculate offset for wheels; description Docu folder
  120. //1.57079633 rad = 90 deg
  121. var verticalOffset = offsetCollidersFromWheel * Mathf.Sin(leaningAbs) / Mathf.Sin(1.57079633f - leaningAbs);
  122. var yPlusOffset = initialWheelColliderY + verticalOffset;
  123. var yMinusOffset = initialWheelColliderY - verticalOffset;
  124. if (CurrentLeaningAngle < 0) //TODO: maybe add a threshold for leaning, e.g. < -0.05 and > 0.05
  125. {
  126. //leaning left, left wheels up, right wheels down
  127. ApplyOffsetToTransform(wheelConfig.frontLeft.transform, yPlusOffset);
  128. ApplyOffsetToTransform(wheelConfig.rearLeft.transform, yPlusOffset);
  129. ApplyOffsetToTransform(wheelConfig.frontRight.transform, yMinusOffset);
  130. ApplyOffsetToTransform(wheelConfig.rearRight.transform, yMinusOffset);
  131. }
  132. else if (CurrentLeaningAngle > 0)
  133. {
  134. //leaning right, right wheels up, left wheels down
  135. ApplyOffsetToTransform(wheelConfig.frontLeft.transform, yMinusOffset);
  136. ApplyOffsetToTransform(wheelConfig.rearLeft.transform, yMinusOffset);
  137. ApplyOffsetToTransform(wheelConfig.frontRight.transform, yPlusOffset);
  138. ApplyOffsetToTransform(wheelConfig.rearRight.transform, yPlusOffset);
  139. }
  140. }
  141. private void ApplyOffsetToTransform(Transform t, float newY)
  142. {
  143. var oldPos = t.localPosition;
  144. t.localPosition = new Vector3(oldPos.x, newY, oldPos.z);
  145. }
  146. }
  147. }