2
0

BicycleController.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. using UnityEngine.EventSystems;
  7. using UnityEngine.Serialization;
  8. using Valve.VR.InteractionSystem;
  9. public class BicycleController : MonoBehaviour
  10. {
  11. #region Variables
  12. [Header("Visible Game Objects")] public GameObject rearWheel;
  13. public GameObject frontWheel;
  14. [Header("Game Objects for Physics")] public Transform centerOfMass;
  15. [FormerlySerializedAs("rb")] public Rigidbody rigidBody;
  16. public WheelConfig wheelConfig;
  17. [Header("Values")] public float offsetCollidersFromWheel = 0.25f;
  18. public float maxLeaningAngle = 35f;
  19. public float maxSteeringAngle = 80f;
  20. public float maxMotorTorque = 2000f;
  21. public float maxBreakTorque = 2000f;
  22. public float CurrentSteerAngle
  23. {
  24. get => currentSteerAngle;
  25. set => currentSteerAngle = Mathf.Clamp(value, -maxSteeringAngle, maxSteeringAngle);
  26. }
  27. public float CurrentMotorTorque
  28. {
  29. get => currentMotorTorque;
  30. set => currentMotorTorque = Mathf.Clamp(value, -maxMotorTorque, maxMotorTorque);
  31. }
  32. public float CurrentBrakeTorque
  33. {
  34. get => currentBrakeTorque;
  35. set => currentBrakeTorque = Mathf.Clamp(value, -maxBreakTorque, maxBreakTorque);
  36. }
  37. public float CurrentLeaningAngle
  38. {
  39. get => currentLeaningAngle;
  40. set => currentLeaningAngle = Mathf.Clamp(value, -maxLeaningAngle, maxLeaningAngle);
  41. }
  42. private WheelCollider[] allWheelColliders;
  43. private float initialWheelColliderY;
  44. private float currentSteerAngle = 0f;
  45. private float currentMotorTorque = 0f;
  46. private float currentBrakeTorque = 0f;
  47. private float currentLeaningAngle = 0f;
  48. #endregion
  49. // Start is called before the first frame update
  50. void Start()
  51. {
  52. rigidBody.centerOfMass = centerOfMass.localPosition;
  53. allWheelColliders = wheelConfig.AllWheels;
  54. wheelConfig.AdjustToGameObjects(frontWheel.transform, rearWheel.transform, offsetCollidersFromWheel);
  55. initialWheelColliderY = allWheelColliders[0].transform.localPosition.y;
  56. }
  57. void FixedUpdate()
  58. {
  59. ApplyColliderForces();
  60. Lean();
  61. //RotateStraight();
  62. }
  63. private void ApplyColliderForces()
  64. {
  65. //offsetCollidersFromWheel = Mathf.Max(0.05f, 0.45f - rigidBody.velocity.magnitude * 0.07f);
  66. //wheelConfig.AdjustToGameObjects(frontWheel.transform, rearWheel.transform, offsetCollidersFromWheel);
  67. ControlSteer(new[] {wheelConfig.frontLeft, wheelConfig.frontRight});
  68. ControlTorque(new[] {wheelConfig.rearLeft, wheelConfig.rearRight});
  69. }
  70. private void ControlSteer(IEnumerable<WheelCollider> colliders)
  71. {
  72. //float steering = maxSteeringAngle * CurrentSteerAngle * 0.2f;
  73. //leftWheels.localPosition = -Vector3.up * (relativeLeanAmount * CurrentSteerAngle * rigidBody.velocity.magnitude * 0.2f);
  74. //rightWheels.localPosition = Vector3.up * (relativeLeanAmount * CurrentSteerAngle * rigidBody.velocity.magnitude * 0.2f);
  75. colliders.ForEach(c => c.steerAngle = CurrentSteerAngle);
  76. }
  77. private void ControlTorque(IEnumerable<WheelCollider> colliders)
  78. {
  79. foreach (var c in colliders)
  80. {
  81. c.motorTorque = CurrentMotorTorque;
  82. c.brakeTorque = CurrentBrakeTorque;
  83. }
  84. }
  85. private void Lean()
  86. {
  87. //reset all wheels to being centered
  88. if (CurrentLeaningAngle == 0) //TODO: maybe add a threshold for leaning, e.g. < -0.05 and > 0.05
  89. {
  90. //leaning left, left wheels up, right wheels down
  91. ApplyOffsetToTransform(wheelConfig.frontLeft.transform, initialWheelColliderY);
  92. ApplyOffsetToTransform(wheelConfig.rearLeft.transform, initialWheelColliderY);
  93. ApplyOffsetToTransform(wheelConfig.frontRight.transform, initialWheelColliderY);
  94. ApplyOffsetToTransform(wheelConfig.rearRight.transform, initialWheelColliderY);
  95. }
  96. //CurrentLeaningAngle < 0 -> leaning left, > 0 -> right
  97. var leaningAbs = Mathf.Abs(CurrentLeaningAngle * Mathf.Deg2Rad);
  98. //calculate offset for wheels; description Docu folder
  99. //1.57079633 rad = 90 deg
  100. var verticalOffset = offsetCollidersFromWheel * Mathf.Sin(leaningAbs) / Mathf.Sin(1.57079633f - leaningAbs);
  101. var yPlusOffset = initialWheelColliderY + verticalOffset;
  102. var yMinusOffset = initialWheelColliderY - verticalOffset;
  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, yPlusOffset);
  107. ApplyOffsetToTransform(wheelConfig.rearLeft.transform, yPlusOffset);
  108. ApplyOffsetToTransform(wheelConfig.frontRight.transform, yMinusOffset);
  109. ApplyOffsetToTransform(wheelConfig.rearRight.transform, yMinusOffset);
  110. }
  111. else if (CurrentLeaningAngle > 0)
  112. {
  113. //leaning right, right wheels up, left wheels down
  114. ApplyOffsetToTransform(wheelConfig.frontLeft.transform, yMinusOffset);
  115. ApplyOffsetToTransform(wheelConfig.rearLeft.transform, yMinusOffset);
  116. ApplyOffsetToTransform(wheelConfig.frontRight.transform, yPlusOffset);
  117. ApplyOffsetToTransform(wheelConfig.rearRight.transform, yPlusOffset);
  118. }
  119. }
  120. private void ApplyOffsetToTransform(Transform t, float newY)
  121. {
  122. var oldPos = t.localPosition;
  123. t.localPosition = new Vector3(oldPos.x, newY, oldPos.z);
  124. }
  125. }