using UnityEngine;
using System.Collections;
/// <summary>
/// If you don’t have a stabilizer bar, you tend to have a lot of trouble with body roll in a turn.
/// http://www.edy.es/dev/2011/10/the-stabi ... -vehicles/
/// </summary>
public class TransmissionAntiRollBar : MonoBehaviour
{
public WheelCollider WheelL;
public WheelCollider WheelR;
public float AntiRoll = 5000f;
[HideInInspector]
public Rigidbody rigidbodyCashed; // Unity 5 Compatible
void Start()
{
rigidbodyCashed = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
WheelHit hit;
float travelL = 1f;
float travelR = 1f;
bool groundedL = WheelL.GetGroundHit(out hit);
if (groundedL)
travelL = (-WheelL.transform.InverseTransformPoint(hit.point).y - WheelL.radius) / WheelL.suspensionDistance;
bool groundedR = WheelR.GetGroundHit(out hit);
if (groundedR)
travelR = (-WheelR.transform.InverseTransformPoint(hit.point).y - WheelR.radius) / WheelR.suspensionDistance;
float antiRollForce = (travelL - travelR) * AntiRoll;
if (groundedL)
rigidbodyCashed.AddForceAtPosition(WheelL.transform.up * -antiRollForce, WheelL.transform.position);
if (groundedR)
rigidbodyCashed.AddForceAtPosition(WheelR.transform.up * antiRollForce, WheelR.transform.position);
}
}