LerpSlopeCollider.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. private int slopeDirection = 1;
  20. private void Start()
  21. {
  22. bikeTransform = bike.transform;
  23. distanceUntilCompletelyRotated = Vector3.Distance(frontWheelContact.position, rearWheelContact.position);
  24. }
  25. private void OnGUI()
  26. {
  27. GUI.TextField(new Rect(10, 200, 200, 20), $"Direction = {slopeDirection}");
  28. GUI.TextField(new Rect(10, 220, 200, 20), $"MinY = {currentFrontWheelMinY}");
  29. GUI.TextField(new Rect(10, 250, 200, 20), $"rotation.x = {bikeTransform.localRotation.eulerAngles.x:n2}");
  30. GUI.TextField(new Rect(10, 270, 200, 20), $"Slope = {currentFrontWheelSlope}");
  31. }
  32. private void FixedUpdate()
  33. {
  34. var fwHit = CastRay(frontWheelContact.position, Vector3.down);
  35. if (fwHit != null)
  36. {
  37. if (Mathf.Abs(currentFrontWheelSlope - fwHit.Slope) > TOLERANCE)
  38. {
  39. rotate = true; //begin rotation
  40. currentFrontWheelSlope = fwHit.Slope;
  41. }
  42. var minYDif = currentFrontWheelMinY - fwHit.MinY;
  43. if (Mathf.Abs(minYDif) > TOLERANCE)
  44. {
  45. slopeDirection = -Math.Sign(minYDif);
  46. currentFrontWheelMinY = fwHit.MinY;
  47. }
  48. }
  49. RotateIfNeeded();
  50. }
  51. private IRoad CastRay(Vector3 pos, Vector3 direction)
  52. {
  53. var layerMask = 1 << collisionLayer;
  54. if (Physics.Raycast(pos, direction, out var hit, MAX_RAYCAST_LENGTH, layerMask))
  55. {
  56. Debug.DrawRay(pos, direction * hit.distance, Color.green);
  57. var roadHit = hit.collider.gameObject.GetComponentInParent<IRoad>();
  58. return roadHit;
  59. }
  60. Debug.DrawRay(pos, direction * MAX_RAYCAST_LENGTH, Color.white);
  61. return null;
  62. }
  63. private void RotateIfNeeded()
  64. {
  65. if (rotate)
  66. {
  67. var bikeAngle = bikeTransform.localRotation.eulerAngles.x;
  68. if (bikeAngle < -180)
  69. {
  70. bikeAngle = Mathf.Abs(360 + bikeAngle);
  71. }
  72. else if (bikeAngle > 180)
  73. {
  74. bikeAngle = Mathf.Abs(-360 + bikeAngle);
  75. }
  76. var slopeDif = slopeDirection * (currentFrontWheelSlope - bikeAngle);
  77. if (Mathf.Abs(currentFrontWheelSlope - bikeAngle) <
  78. TOLERANCE)
  79. {
  80. rotate = false;
  81. return;
  82. }
  83. var forwardBikeSpeed = bikeTransform.InverseTransformDirection(bike.velocity).z;
  84. var rot = slopeDif * Time.fixedDeltaTime * forwardBikeSpeed / distanceUntilCompletelyRotated;
  85. if (slopeDif > 0) //lift front wheel
  86. {
  87. bikeTransform.RotateAround(frontWheelContact.position, -frontWheelContact.right,
  88. rot);
  89. }
  90. else //dip front wheel
  91. {
  92. bikeTransform.RotateAround(frontWheelContact.position, frontWheelContact.right,
  93. -rot);
  94. }
  95. }
  96. }
  97. }
  98. }