WcBicycleController.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. private WheelCollider[] allWheelColliders;
  45. private float initialWheelColliderY;
  46. private float currentSteerAngle = 0f;
  47. private float currentLeaningAngle = 0f;
  48. private float currentSpeed;
  49. #endregion
  50. // Start is called before the first frame update
  51. void Start()
  52. {
  53. rigidBody.centerOfMass = centerOfMass.localPosition;
  54. allWheelColliders = wheelConfig.AllWheels;
  55. wheelConfig.AdjustToGameObjects(frontWheel.transform, rearWheel.transform, offsetCollidersFromWheel);
  56. initialWheelColliderY = allWheelColliders[0].transform.localPosition.y;
  57. }
  58. void FixedUpdate()
  59. {
  60. ApplyColliderForces();
  61. Lean();
  62. //RotateStraight();
  63. }
  64. private void ApplyColliderForces()
  65. {
  66. //offsetCollidersFromWheel = Mathf.Max(0.05f, 0.45f - rigidBody.velocity.magnitude * 0.07f);
  67. //wheelConfig.AdjustToGameObjects(frontWheel.transform, rearWheel.transform, offsetCollidersFromWheel);
  68. ControlSteer(new[] {wheelConfig.frontLeft, wheelConfig.frontRight});
  69. ControlTorque(new[] {wheelConfig.rearLeft, wheelConfig.rearRight});
  70. }
  71. private void ControlSteer(IEnumerable<WheelCollider> colliders)
  72. {
  73. //float steering = maxSteeringAngle * CurrentSteerAngle * 0.2f;
  74. //leftWheels.localPosition = -Vector3.up * (relativeLeanAmount * CurrentSteerAngle * rigidBody.velocity.magnitude * 0.2f);
  75. //rightWheels.localPosition = Vector3.up * (relativeLeanAmount * CurrentSteerAngle * rigidBody.velocity.magnitude * 0.2f);
  76. colliders.ForEach(c => c.steerAngle = CurrentSteerAngle);
  77. }
  78. private void ControlTorque(IEnumerable<WheelCollider> colliders)
  79. {
  80. var rbSpeed = rigidBody.velocity.magnitude;
  81. var speedDif = CurrentSpeed - rbSpeed;
  82. var ratio = speedDif / maxSpeed;
  83. var brakeTorque = 0f;
  84. var motorTorque = 0f;
  85. if (speedDif >= .1f) // 0.36 km/h
  86. {
  87. var torque = maxMotorTorque * ratio;
  88. //Debug.Log($"SpeedDif = {speedDif} -> applying Torque {torque} (Ratio: {ratio})");
  89. brakeTorque = 0;
  90. motorTorque = torque;
  91. }
  92. else if (speedDif <= -.1f)
  93. {
  94. var torque = -maxBreakTorque * ratio;
  95. //Debug.Log($"SpeedDif = {speedDif} -> applying brake Torque {torque} (Ratio: {ratio})");
  96. motorTorque = 0;
  97. brakeTorque = torque;
  98. }
  99. foreach (var c in colliders)
  100. {
  101. c.motorTorque = motorTorque;
  102. c.brakeTorque = brakeTorque;
  103. }
  104. }
  105. private void Lean()
  106. {
  107. //reset all wheels to being centered
  108. if (CurrentLeaningAngle == 0) //TODO: maybe add a threshold for leaning, e.g. < -0.05 and > 0.05
  109. {
  110. //leaning left, left wheels up, right wheels down
  111. ApplyOffsetToTransform(wheelConfig.frontLeft.transform, initialWheelColliderY);
  112. ApplyOffsetToTransform(wheelConfig.rearLeft.transform, initialWheelColliderY);
  113. ApplyOffsetToTransform(wheelConfig.frontRight.transform, initialWheelColliderY);
  114. ApplyOffsetToTransform(wheelConfig.rearRight.transform, initialWheelColliderY);
  115. }
  116. //CurrentLeaningAngle < 0 -> leaning left, > 0 -> right
  117. var leaningAbs = Mathf.Abs(CurrentLeaningAngle * Mathf.Deg2Rad);
  118. //calculate offset for wheels; description Docu folder
  119. //1.57079633 rad = 90 deg
  120. var verticalOffset = offsetCollidersFromWheel * Mathf.Sin(leaningAbs) / Mathf.Sin(1.57079633f - leaningAbs);
  121. var yPlusOffset = initialWheelColliderY + verticalOffset;
  122. var yMinusOffset = initialWheelColliderY - verticalOffset;
  123. if (CurrentLeaningAngle < 0) //TODO: maybe add a threshold for leaning, e.g. < -0.05 and > 0.05
  124. {
  125. //leaning left, left wheels up, right wheels down
  126. ApplyOffsetToTransform(wheelConfig.frontLeft.transform, yPlusOffset);
  127. ApplyOffsetToTransform(wheelConfig.rearLeft.transform, yPlusOffset);
  128. ApplyOffsetToTransform(wheelConfig.frontRight.transform, yMinusOffset);
  129. ApplyOffsetToTransform(wheelConfig.rearRight.transform, yMinusOffset);
  130. }
  131. else if (CurrentLeaningAngle > 0)
  132. {
  133. //leaning right, right wheels up, left wheels down
  134. ApplyOffsetToTransform(wheelConfig.frontLeft.transform, yMinusOffset);
  135. ApplyOffsetToTransform(wheelConfig.rearLeft.transform, yMinusOffset);
  136. ApplyOffsetToTransform(wheelConfig.frontRight.transform, yPlusOffset);
  137. ApplyOffsetToTransform(wheelConfig.rearRight.transform, yPlusOffset);
  138. }
  139. }
  140. private void ApplyOffsetToTransform(Transform t, float newY)
  141. {
  142. var oldPos = t.localPosition;
  143. t.localPosition = new Vector3(oldPos.x, newY, oldPos.z);
  144. }
  145. }
  146. }