RbBicycleController.cs 4.6 KB

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