LerpSlopeCollider.cs 4.1 KB

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