2
0

CustomWheelColliderSlopeAdjustment.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using UnityEngine;
  2. public class CustomWheelColliderSlopeAdjustment : MonoBehaviour
  3. {
  4. public CustomWheelCollider frontWheel;
  5. public CustomWheelCollider rearWheel;
  6. public Transform rotatePoint;
  7. public float timeDifferenceTolerance = 0.0001f;
  8. public Vector3 dif; //TODO: remove
  9. private float angle;
  10. private bool isHitting;
  11. private Transform t;
  12. private void Start()
  13. {
  14. t = transform;
  15. }
  16. private void FixedUpdate()
  17. {
  18. isHitting = false;
  19. if (frontWheel.CurrentHitNumber > 1)
  20. {
  21. angle = -.5f;
  22. Rotate();
  23. return;
  24. }
  25. if (rearWheel.CurrentHitNumber > 1)
  26. {
  27. angle = .5f;
  28. Rotate();
  29. return;
  30. }
  31. //TODO: this is probably called almost never
  32. if (frontWheel.CurrentHit == null || rearWheel.CurrentHit == null) return;
  33. var fwHit = frontWheel.CurrentHit.Value;
  34. var rwHit = rearWheel.CurrentHit.Value;
  35. if (fwHit.Timestamp - rwHit.Timestamp <= timeDifferenceTolerance) UpdateTransformRotation(fwHit, rwHit);
  36. }
  37. /*private void OnGUI()
  38. {
  39. GUI.TextField(new Rect(Screen.width - 200, 10, 190, 80),
  40. $"Hitting? {isHitting}\nAngle: {angle:n2}\nrw: {rearWheel.CurrentHit?.Hit.point}\nfw: {frontWheel.CurrentHit?.Hit.point}");
  41. }*/
  42. private void UpdateTransformRotation(HitInfo fwHit, HitInfo rwHit)
  43. {
  44. isHitting = true;
  45. dif = t.TransformPoint(fwHit.Hit.point) - t.TransformPoint(rwHit.Hit.point);
  46. angle = -Mathf.Atan(dif.y / dif.z) * Mathf.Rad2Deg; //TODO: is it that easy?
  47. Debug.DrawLine(rwHit.Hit.point, rwHit.Hit.point + t.right, Color.red);
  48. Rotate();
  49. //t.RotateAround(rotatePoint.position, t.right, angle) ;
  50. //t.localRotation = Quaternion.Euler(t.localRotation.eulerAngles + Vector3.right * angle);
  51. }
  52. private void Rotate()
  53. {
  54. t.RotateAround(rotatePoint.position, t.right, angle);
  55. angle = 0f;
  56. }
  57. }