LerpSlopeCollider.cs 4.0 KB

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