LerpSlopeCollider.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using Controller.Bicycle;
  3. using Roads;
  4. using UnityEngine;
  5. namespace Wheels
  6. {
  7. public class LerpSlopeCollider : MonoBehaviour
  8. {
  9. public int collisionLayer = 1;
  10. public Transform rearWheelContact;
  11. public Transform frontWheelContact;
  12. public Rigidbody bike;
  13. private const float TOLERANCE = 0.005f;
  14. private const float MAX_RAYCAST_LENGTH = 2f;
  15. private float distanceUntilCompletelyRotated;
  16. private Transform bikeTransform;
  17. private float currentFrontWheelSlope;
  18. private float currentFrontWheelMinY;
  19. private bool rotate;
  20. public int SlopeDirection { private set; get; } = 1;
  21. public delegate void OnSlopeChangedEvent(float timestamp, float slope);
  22. public event OnSlopeChangedEvent OnSlopeChanged;
  23. private void Start()
  24. {
  25. bikeTransform = bike.transform;
  26. distanceUntilCompletelyRotated = Vector3.Distance(frontWheelContact.position, rearWheelContact.position);
  27. }
  28. private void OnGUI()
  29. {
  30. GUI.TextField(new Rect(10, 200, 200, 20), $"Direction = {SlopeDirection}");
  31. GUI.TextField(new Rect(10, 220, 200, 20), $"MinY = {currentFrontWheelMinY}");
  32. GUI.TextField(new Rect(10, 250, 200, 20), $"rotation.x = {bikeTransform.localRotation.eulerAngles.x:n2}");
  33. GUI.TextField(new Rect(10, 270, 200, 20), $"Slope = {currentFrontWheelSlope}");
  34. }
  35. private void FixedUpdate()
  36. {
  37. var fwHit = CastRay(frontWheelContact.position, Vector3.down);
  38. if (fwHit != null)
  39. {
  40. if (Mathf.Abs(currentFrontWheelSlope - fwHit.SlopeDeg) > TOLERANCE)
  41. {
  42. rotate = true; //begin rotation
  43. currentFrontWheelSlope = fwHit.SlopeDeg;
  44. OnSlopeChanged?.Invoke(Time.fixedTime, currentFrontWheelSlope);
  45. }
  46. var minYDif = currentFrontWheelMinY - fwHit.MinY;
  47. if (Mathf.Abs(minYDif) > TOLERANCE)
  48. {
  49. SlopeDirection = -Math.Sign(minYDif);
  50. currentFrontWheelMinY = fwHit.MinY;
  51. }
  52. }
  53. RotateIfNeeded();
  54. }
  55. private IRoad CastRay(Vector3 pos, Vector3 direction)
  56. {
  57. var layerMask = 1 << collisionLayer;
  58. if (Physics.Raycast(pos, direction, out var hit, MAX_RAYCAST_LENGTH, layerMask))
  59. {
  60. Debug.DrawRay(pos, direction * hit.distance, Color.green);
  61. var roadHit = hit.collider.gameObject.GetComponentInParent<IRoad>();
  62. return roadHit;
  63. }
  64. Debug.DrawRay(pos, direction * MAX_RAYCAST_LENGTH, Color.white);
  65. return null;
  66. }
  67. private void RotateIfNeeded()
  68. {
  69. if (rotate)
  70. {
  71. var bikeAngle = bikeTransform.localRotation.eulerAngles.x;
  72. if (bikeAngle < -180)
  73. {
  74. bikeAngle = Mathf.Abs(360 + bikeAngle);
  75. }
  76. else if (bikeAngle > 180)
  77. {
  78. bikeAngle = Mathf.Abs(-360 + bikeAngle);
  79. }
  80. var slopeDif = SlopeDirection * (currentFrontWheelSlope - bikeAngle);
  81. if (Mathf.Abs(currentFrontWheelSlope - bikeAngle) <
  82. TOLERANCE)
  83. {
  84. rotate = false;
  85. return;
  86. }
  87. var forwardBikeSpeed = bikeTransform.InverseTransformDirection(bike.velocity).z;
  88. var rot = slopeDif * Time.fixedDeltaTime * forwardBikeSpeed / distanceUntilCompletelyRotated;
  89. if (slopeDif > 0) //lift front wheel
  90. {
  91. bikeTransform.RotateAround(frontWheelContact.position, -frontWheelContact.right,
  92. rot);
  93. }
  94. else //dip front wheel
  95. {
  96. bikeTransform.RotateAround(frontWheelContact.position, frontWheelContact.right,
  97. -rot);
  98. }
  99. }
  100. }
  101. }
  102. }