CustomWheelColliderSlopeAdjustment.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class CustomWheelColliderSlopeAdjustment : MonoBehaviour
  6. {
  7. public CustomWheelCollider frontWheel;
  8. public CustomWheelCollider rearWheel;
  9. public float timeDifferenceTolerance = 0.1f;
  10. public Vector3 dif; //TODO: remove
  11. private Transform t;
  12. private void Start()
  13. {
  14. t = transform;
  15. }
  16. private void FixedUpdate()
  17. {
  18. if (frontWheel.CurrentHit == null || rearWheel.CurrentHit == null) return;
  19. var fwHit = frontWheel.CurrentHit.Value;
  20. var rwHit = rearWheel.CurrentHit.Value;
  21. if (fwHit.Timestamp - rwHit.Timestamp <= timeDifferenceTolerance)
  22. {
  23. UpdateTransformRotation(fwHit, rwHit);
  24. }
  25. }
  26. private void UpdateTransformRotation(HitInfo fwHit, HitInfo rwHit)
  27. {
  28. dif = t.TransformPoint(fwHit.Hit.point) - t.TransformPoint(rwHit.Hit.point);
  29. var angle = -Mathf.Atan(dif.y / dif.z) * Mathf.Rad2Deg; //TODO: is it that easy?
  30. t.localRotation = Quaternion.Euler(t.localRotation.eulerAngles + Vector3.right * angle);
  31. }
  32. }