Unity3D

AntiRoll Bar

//Anti-Roll Bar Script as used in tutorial.
//Script credit : https://forum.unity3d.com/threads/how-to-make-a-physically-real-stable-car-with-wheelcolliders.50643/

var wheelLname: String;
var wheelRname : String;

var WheelL : WheelCollider;
var WheelR : WheelCollider;
var AntiRoll = 5000.0;

function Start () {

 WheelL = GameObject.Find(wheelLname).GetComponent(WheelCollider);
 WheelR = GameObject.Find(wheelRname).GetComponent(WheelCollider);
}


function FixedUpdate ()
 {
 var hit : WheelHit;
 var travelL = 1.0;
 var travelR = 1.0;

 var groundedL = WheelL.GetGroundHit(hit);
 if (groundedL)
 travelL = (-WheelL.transform.InverseTransformPoint(hit.point).y - WheelL.radius) / WheelL.suspensionDistance;

 var groundedR = WheelR.GetGroundHit(hit);
 if (groundedR)
 travelR = (-WheelR.transform.InverseTransformPoint(hit.point).y - WheelR.radius) / WheelR.suspensionDistance;

 var antiRollForce = (travelL - travelR) * AntiRoll;

 if (groundedL)
 GetComponent.<Rigidbody>().AddForceAtPosition(WheelL.transform.up * -antiRollForce,
 WheelL.transform.position); 
 if (groundedR)
 GetComponent.<Rigidbody>().AddForceAtPosition(WheelR.transform.up * antiRollForce,
 WheelR.transform.position); 
 }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s