LerpSlopeCollider.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using Roads;
  3. using UnityEngine;
  4. namespace Wheels
  5. {
  6. public class LerpSlopeCollider : MonoBehaviour
  7. {
  8. public delegate void OnSlopeChangedEvent(float timestamp, float slope);
  9. private const float TOLERANCE = 0.005f;
  10. private const float MAX_RAYCAST_LENGTH = 2f;
  11. public int collisionLayer = 1;
  12. public Transform rearWheelContact;
  13. public Transform frontWheelContact;
  14. public Rigidbody bike;
  15. private Transform bikeTransform;
  16. private float currentFrontWheelMinY;
  17. private float currentFrontWheelSlope;
  18. private float distanceUntilCompletelyRotated;
  19. private bool rotate;
  20. public int SlopeDirection { private set; get; } = 1;
  21. private void Start()
  22. {
  23. bikeTransform = bike.transform;
  24. distanceUntilCompletelyRotated = Vector3.Distance(frontWheelContact.position, rearWheelContact.position);
  25. }
  26. private void FixedUpdate()
  27. {
  28. var fwHit = CastRay(frontWheelContact.position, Vector3.down);
  29. if (fwHit != null)
  30. {
  31. if (Mathf.Abs(currentFrontWheelSlope - fwHit.SlopeDeg) > TOLERANCE)
  32. {
  33. rotate = true; //begin rotation
  34. currentFrontWheelSlope = fwHit.SlopeDeg;
  35. OnSlopeChanged?.Invoke(Time.fixedTime, currentFrontWheelSlope);
  36. }
  37. var minYDif = currentFrontWheelMinY - fwHit.MinY;
  38. if (Mathf.Abs(minYDif) > TOLERANCE)
  39. {
  40. SlopeDirection = -Math.Sign(minYDif);
  41. currentFrontWheelMinY = fwHit.MinY;
  42. }
  43. }
  44. RotateIfNeeded();
  45. }
  46. private void OnGUI()
  47. {
  48. GUI.TextField(new Rect(10, 200, 200, 20), $"Direction = {SlopeDirection}");
  49. GUI.TextField(new Rect(10, 220, 200, 20), $"MinY = {currentFrontWheelMinY}");
  50. GUI.TextField(new Rect(10, 250, 200, 20), $"rotation.x = {bikeTransform.localRotation.eulerAngles.x:n2}");
  51. GUI.TextField(new Rect(10, 270, 200, 20), $"Slope = {currentFrontWheelSlope}");
  52. }
  53. public event OnSlopeChangedEvent OnSlopeChanged;
  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. bikeAngle = Mathf.Abs(360 + bikeAngle);
  73. else if (bikeAngle > 180) bikeAngle = Mathf.Abs(-360 + bikeAngle);
  74. var slopeDif = SlopeDirection * (currentFrontWheelSlope - bikeAngle);
  75. if (Mathf.Abs(currentFrontWheelSlope - bikeAngle) <
  76. TOLERANCE)
  77. {
  78. rotate = false;
  79. return;
  80. }
  81. var forwardBikeSpeed = bikeTransform.InverseTransformDirection(bike.velocity).z;
  82. var rot = slopeDif * Time.fixedDeltaTime * forwardBikeSpeed / distanceUntilCompletelyRotated;
  83. if (slopeDif > 0) //lift front wheel
  84. bikeTransform.RotateAround(frontWheelContact.position, -frontWheelContact.right,
  85. rot);
  86. else //dip front wheel
  87. bikeTransform.RotateAround(frontWheelContact.position, frontWheelContact.right,
  88. -rot);
  89. }
  90. }
  91. }
  92. }