BicycleController.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. namespace Controller
  10. {
  11. public class BicycleController : MonoBehaviour
  12. {
  13. #region Variables
  14. [Header("Visible Game Objects")] public GameObject rearWheel;
  15. public GameObject frontWheel;
  16. [Header("Game Objects for Physics")] public Transform centerOfMass;
  17. [FormerlySerializedAs("rb")] public Rigidbody rigidBody;
  18. public WheelConfig wheelConfig;
  19. [Header("Values")] public float offsetCollidersFromWheel = 0.25f;
  20. public float maxLeaningAngle = 35f;
  21. public float maxSteeringAngle = 80f;
  22. public float maxMotorTorque = 2000f;
  23. public float maxBreakTorque = 2000f;
  24. public float CurrentSteerAngle
  25. {
  26. get => currentSteerAngle;
  27. set => currentSteerAngle = Mathf.Clamp(value, -maxSteeringAngle, maxSteeringAngle);
  28. }
  29. public float CurrentMotorTorque
  30. {
  31. get => currentMotorTorque;
  32. set => currentMotorTorque = Mathf.Clamp(value, -maxMotorTorque, maxMotorTorque);
  33. }
  34. public float CurrentBrakeTorque
  35. {
  36. get => currentBrakeTorque;
  37. set => currentBrakeTorque = Mathf.Clamp(value, -maxBreakTorque, maxBreakTorque);
  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 currentMotorTorque = 0f;
  48. private float currentBrakeTorque = 0f;
  49. private float currentLeaningAngle = 0f;
  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. foreach (var c in colliders)
  82. {
  83. c.motorTorque = CurrentMotorTorque;
  84. c.brakeTorque = CurrentBrakeTorque;
  85. }
  86. }
  87. private void Lean()
  88. {
  89. //reset all wheels to being centered
  90. if (CurrentLeaningAngle == 0) //TODO: maybe add a threshold for leaning, e.g. < -0.05 and > 0.05
  91. {
  92. //leaning left, left wheels up, right wheels down
  93. ApplyOffsetToTransform(wheelConfig.frontLeft.transform, initialWheelColliderY);
  94. ApplyOffsetToTransform(wheelConfig.rearLeft.transform, initialWheelColliderY);
  95. ApplyOffsetToTransform(wheelConfig.frontRight.transform, initialWheelColliderY);
  96. ApplyOffsetToTransform(wheelConfig.rearRight.transform, initialWheelColliderY);
  97. }
  98. //CurrentLeaningAngle < 0 -> leaning left, > 0 -> right
  99. var leaningAbs = Mathf.Abs(CurrentLeaningAngle * Mathf.Deg2Rad);
  100. //calculate offset for wheels; description Docu folder
  101. //1.57079633 rad = 90 deg
  102. var verticalOffset = offsetCollidersFromWheel * Mathf.Sin(leaningAbs) / Mathf.Sin(1.57079633f - leaningAbs);
  103. var yPlusOffset = initialWheelColliderY + verticalOffset;
  104. var yMinusOffset = initialWheelColliderY - verticalOffset;
  105. if (CurrentLeaningAngle < 0) //TODO: maybe add a threshold for leaning, e.g. < -0.05 and > 0.05
  106. {
  107. //leaning left, left wheels up, right wheels down
  108. ApplyOffsetToTransform(wheelConfig.frontLeft.transform, yPlusOffset);
  109. ApplyOffsetToTransform(wheelConfig.rearLeft.transform, yPlusOffset);
  110. ApplyOffsetToTransform(wheelConfig.frontRight.transform, yMinusOffset);
  111. ApplyOffsetToTransform(wheelConfig.rearRight.transform, yMinusOffset);
  112. }
  113. else if (CurrentLeaningAngle > 0)
  114. {
  115. //leaning right, right wheels up, left wheels down
  116. ApplyOffsetToTransform(wheelConfig.frontLeft.transform, yMinusOffset);
  117. ApplyOffsetToTransform(wheelConfig.rearLeft.transform, yMinusOffset);
  118. ApplyOffsetToTransform(wheelConfig.frontRight.transform, yPlusOffset);
  119. ApplyOffsetToTransform(wheelConfig.rearRight.transform, yPlusOffset);
  120. }
  121. }
  122. private void ApplyOffsetToTransform(Transform t, float newY)
  123. {
  124. var oldPos = t.localPosition;
  125. t.localPosition = new Vector3(oldPos.x, newY, oldPos.z);
  126. }
  127. }
  128. }