WcBicycleController.cs 6.5 KB

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