RbBicycleController.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using UnityEngine;
  2. namespace Controller.Bicycle
  3. {
  4. public class RbBicycleController : BicycleControllerBaseBehaviour, IBicycleController
  5. {
  6. private void Awake()
  7. {
  8. rbTransform = rigidBody.transform;
  9. rigidBody.freezeRotation = true;
  10. rigidBody.centerOfMass = centerOfMass.position;
  11. }
  12. private void Start()
  13. {
  14. speedBooster = BikeSpeedBooster.Instance;
  15. }
  16. private void FixedUpdate()
  17. {
  18. //rigidBody.isKinematic = currentSpeed <= THRESHOLD_STANDING;
  19. ApplyVelocity();
  20. ApplySteerAngleAndRotation();
  21. }
  22. /*private void OnGUI()
  23. {
  24. GUI.TextField(new Rect(200, 100, 400, 50), $"Boost = {speedBooster.Boost}");
  25. }*/
  26. //TODO: maybe add some kind of rolling physics after downhill
  27. private void ApplyVelocity()
  28. {
  29. var targetVelocity = CalculateTargetVelocity();
  30. var velocityChange = targetVelocity - rigidBody.velocity;
  31. velocityChange.y = 0;
  32. rigidBody.AddForce(velocityChange, ForceMode.VelocityChange);
  33. }
  34. private Vector3 CalculateTargetVelocity()
  35. {
  36. if (adjustSpeedToSlope)
  37. {
  38. previousSoeedAdjusted = currentSpeedAdjusted;
  39. var boost = speedBooster.Boost;
  40. float nextSpeedAdjusted;
  41. if (boost > 1 && CurrentSpeedSensed < 5.55f) //5.55 m/s = 20 km/h
  42. nextSpeedAdjusted = 5.55f * boost;
  43. else
  44. nextSpeedAdjusted = CurrentSpeedSensed * speedBooster.Boost;
  45. currentSpeedAdjusted = Mathf.Max(Mathf.Max(0,
  46. previousSoeedAdjusted - Time.deltaTime * MAX_DIF_SPEED_ADJUSTED_PER_SECOND), nextSpeedAdjusted);
  47. }
  48. else
  49. {
  50. currentSpeedAdjusted = CurrentSpeedSensed;
  51. }
  52. var tv = new Vector3(0, 0, currentSpeedAdjusted);
  53. tv = rbTransform.TransformDirection(tv);
  54. return tv;
  55. }
  56. private void ApplySteerAngleAndRotation()
  57. {
  58. //don't lean and rotate when veeeeeery sloooow/standing. Otherwise bike will rotate already
  59. if (CurrentSpeed < 0.3f) //ca 1 km/h
  60. {
  61. CurrentSteerAngle = 0;
  62. CurrentLeaningAngle = 0;
  63. }
  64. var sumMode = controllerMode.weightLeaning + controllerMode.weightSteering;
  65. var calculatedSteerAngle = (controllerMode.weightSteering * CurrentSteerAngle +
  66. controllerMode.weightLeaning + currentLeaningAngle) /
  67. sumMode; //TODO: maybe define what leaning angle means as steering angle;
  68. var r = rbTransform.localRotation.eulerAngles;
  69. float rectifiedZ;
  70. if (r.z > 180f)
  71. rectifiedZ = -360 + r.z;
  72. else if (r.z < -180f)
  73. rectifiedZ = 360 + r.z;
  74. else
  75. rectifiedZ = r.z;
  76. var leanDif = -CurrentLeaningAngle - rectifiedZ;
  77. rbTransform.localRotation =
  78. Quaternion.Euler(r + new Vector3(0, calculatedSteerAngle, leanDif) * Time.fixedDeltaTime);
  79. }
  80. #region Variables
  81. private Transform rbTransform;
  82. private float currentSteerAngle;
  83. private float currentLeaningAngle;
  84. private float currentSpeedAdjusted;
  85. private float previousSoeedAdjusted;
  86. private const float MAX_DIF_SPEED_ADJUSTED_PER_SECOND = 10f;
  87. private BikeSpeedBooster speedBooster;
  88. [Tooltip("Needs a RbBicycleSlopeSpeedManager to be attached to this GameObject")]
  89. public bool adjustSpeedToSlope = true;
  90. public Vector3 Forward => rbTransform.forward;
  91. public Vector3 Right => -rbTransform.right;
  92. public Vector3 Up => rbTransform.up;
  93. public BicycleControllerMode ControllerMode
  94. {
  95. get => controllerMode;
  96. set => controllerMode = value;
  97. }
  98. public float CurrentSpeedSensed { private set; get; }
  99. public float CurrentSpeed
  100. {
  101. get => adjustSpeedToSlope ? currentSpeedAdjusted : CurrentSpeedSensed;
  102. set => CurrentSpeedSensed = Mathf.Clamp(value, 0, maxSpeed);
  103. }
  104. public float CurrentSpeedKph => CurrentSpeed * 3.6f;
  105. public float CurrentSteerAngle
  106. {
  107. get => currentSteerAngle;
  108. set => currentSteerAngle = Mathf.Clamp(value, -maxSteeringAngle, maxSteeringAngle);
  109. }
  110. public float CurrentLeaningAngle
  111. {
  112. get => currentLeaningAngle;
  113. set
  114. {
  115. //don't lean while standing / walking to bike
  116. if (rigidBody.velocity.magnitude < .5f) return;
  117. currentLeaningAngle = Mathf.Clamp(value, -maxLeaningAngle, maxLeaningAngle);
  118. }
  119. }
  120. public Vector3 RigidBodyVelocity => rigidBody.velocity;
  121. #endregion
  122. }
  123. }