12345678910111213141516171819202122232425262728293031323334353637383940 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class CustomWheelColliderSlopeAdjustment : MonoBehaviour
- {
- public CustomWheelCollider frontWheel;
- public CustomWheelCollider rearWheel;
- public float timeDifferenceTolerance = 0.1f;
- public Vector3 dif; //TODO: remove
- private Transform t;
-
- private void Start()
- {
- t = transform;
- }
- private void FixedUpdate()
- {
- if (frontWheel.CurrentHit == null || rearWheel.CurrentHit == null) return;
-
- var fwHit = frontWheel.CurrentHit.Value;
- var rwHit = rearWheel.CurrentHit.Value;
- if (fwHit.Timestamp - rwHit.Timestamp <= timeDifferenceTolerance)
- {
- UpdateTransformRotation(fwHit, rwHit);
- }
- }
- private void UpdateTransformRotation(HitInfo fwHit, HitInfo rwHit)
- {
- dif = t.TransformPoint(fwHit.Hit.point) - t.TransformPoint(rwHit.Hit.point);
- var angle = -Mathf.Atan(dif.y / dif.z) * Mathf.Rad2Deg; //TODO: is it that easy?
- t.localRotation = Quaternion.Euler(t.localRotation.eulerAngles + Vector3.right * angle);
- }
- }
|