Unity3D

Suspension

//Makes the wheel mesh spin as well as provide suspension effect.

#pragma strict

private var wheelMesh : Transform;
private var wheelCollider : WheelCollider;
var currentWl : String;

function Start () {

wheelCollider = gameObject.GetComponent(WheelCollider);

if (this.gameObject.name == "FL.Col") {currentWl = "FL";}
else if (this.gameObject.name == "FR.Col") {currentWl = "FR";}
else if (this.gameObject.name == "RL.Col") {currentWl = "RL";}
else if (this.gameObject.name == "RR.Col") {currentWl = "RR";}

wheelMesh = GameObject.Find(currentWl).transform;
}

function Update () {

 var hit : RaycastHit;
 
 if (Physics.Raycast(wheelCollider.transform.position, -wheelCollider.transform.up, hit, wheelCollider.suspensionDistance + wheelCollider.radius)) {
 wheelMesh.position = hit.point + wheelCollider.transform.up * wheelCollider.radius;
 }

 else {
 wheelMesh.position = wheelCollider.transform.position - (wheelCollider.transform.up * wheelCollider.suspensionDistance);
 }
 
 wheelMesh.transform.Rotate(wheelCollider.rpm/10,0,0);
 wheelMesh.localEulerAngles.y = wheelCollider.steerAngle - wheelMesh.localEulerAngles.z;
}
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