Turning car's tires left/right

Hi everyone,

I’m really new to this so bear with me ^^;

I have a car model I made with Maya which looks like this

11528-carmodel.png

The problem that I’m facing is rotating the front tires right and left, or should I say, limiting their Y rotation so they won’t go beyond 20 degree or something.

To avoid Gimbal lock, I’m using the Tire_FL_Nurbs and Tire_FR_Nurbs for that.
This is the script code I attached to both of them.

void Update ()
{
    Debug.Log("y = "+ transform.rotation.y);
    if (Input.GetKey(KeyCode.RightArrow))
    {
        if (transform.rotation.y < 0.2)
            transform.Rotate(new Vector3(0f, 1f, 0f));
    }
    else
    {
        if (transform.rotation.y > 0)
        {
            transform.Rotate(new Vector3(0f, -1f, 0f));
        }
    }
    if (Input.GetKey(KeyCode.LeftArrow))
    {
        if (transform.rotation.y > -0.2)
            transform.Rotate(new Vector3(0f, -1f, 0f));
    }
    else
    {
        if (transform.rotation.y < 0)
        {
            transform.Rotate(new Vector3(0f, 1f, 0f));
        }
    }	
}

Everything works fine when the car’s Y axis is 0. but when it changes, the code still try to make the tires look as if the car’s Y axis is still 0.

I’m not familiar with the concept, but I think that I need to use the car’s front Victor3. However, since the nurbs are children to the car’s body, doesn’t that mean I shouldn’t have to worry about this?

I’m using the Unity 4.1.3.

The first major problem is that you are dealing with ‘transform.rotation’ directly. Transform.rotation is a quaternion…a 4D construct that is non-intuitive. The Unity reference indicates you should not deal with its components (x,y,z,w) directly unless you have a firm understanding of Quaternions. You can deal with Transform.eulerAngles, but there are some gotchas there as well. Typically you want to keep your own vector3 for the rotation and treat ‘transform.eulerAngles’ as write-only.

As for your angle issue, what you really want is Transform.localEulerAngles. This will be the rotation relative to the parent object. So when you limit the wheels turning to 20 degrees, that 20 degrees will be relative to the parent object of the wheels.