Rotate the bike,s wheels to move the bike.

Hi, i am working on a bike racing game and so far i have applied the force on bike to move the bike and used Quaternion and lerp function to tilt and turn the bike. now i want to just apply my force on wheels to rotate them and move my bike like a real bike moves.

can any body please refer me any tutorial or some script or hint to get it on?

P.S i am making this game for iPhone.

thanks.

This which is complicated

I used this for a start

http://www.docstoc.com/docs/26688666/Unity3D---Car-Tutorial

When you get to the part where you update the wheel meshes tho I found that there was a bug and the wheels moved to the center of the car. On my car I got rid of that part and then copied it for my own script with the bug fixed. Here it is.
Your heirarchy MUST be this for my script and all their centers must be the same, which is best done by some copy-pasting and deleting componets. You have to assign the varibles for the script in the inspector, FR wheel collider is the wheel collider, Fr wheel transform is the wheel’s mesh.

My setup must be done for each wheel individually, which means you can have unlimited wheels :slight_smile:

vehicle

wheel collider (child of vehicle body)

empty object with the script(child of vehicle body not the wheel collider)

wheel mesh(child of empty with script)


var FRWheelCollider : WheelCollider;

var FRWheelTransform : Transform;

private var FRWheelRotation : float = 0;


function UpdateWheelHeight(wheelTransform : Transform, collider : WheelCollider) 
{
    var localPosition : Vector3 = wheelTransform.localPosition;
    var hit : WheelHit;

    // see if we have contact with ground   
    if (collider.GetGroundHit(hit)) 
	{
        // calculate the distance between current wheel position and hit point, correct
        // wheel position
		
        localPosition.y -= Vector3.Dot(wheelTransform.position - hit.point, transform.up) - collider.radius;
    }
    else 
	{
        // no contact with ground, just extend wheel position with suspension distance
		
       localPosition = Vector3(0,collider.suspensionDistance * -1,0);
    }
    // actually update the position
    wheelTransform.localPosition = localPosition;
}

function Update() 
{
    var deltaTime : float = Time.deltaTime;
        
    // calculate the rotation of the wheels
    FRWheelRotation = Mathf.Repeat(FRWheelRotation + deltaTime * FRWheelCollider.rpm * 360.0 / 60.0, 360.0);

    // set the rotation of the wheels
    FRWheelTransform.localRotation = Quaternion.Euler(FRWheelRotation, FRWheelCollider.steerAngle, 0.0);
    
    //BLWheelTransform.localRotation = Quaternion.Euler(BLWheelRotation, 0, 0.0);

    // now some more difficult stuff: suspension, we have to manually move the wheels up and down
    // depending on the point of impact. As we have to do it twice (two wheels) we put it in a separate function
    UpdateWheelHeight(FRWheelTransform, FRWheelCollider);
    //UpdateWheelHeight(BRWheelTransform, BRWheelCollider);
}