RbBicycleController.cs 4.5 KB

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