2
0

WcBicycleController.cs 6.6 KB

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