Return of Z rotation Value.

Hello I’m new to Unity, and I’m having some problems getting the Z value of a rotation. I’ve got a crate and I want to play a sound when it rotates more then 45 degrees. I tried this:

function Update () {

var crateRotation : Vector3;

crateRotation = gameObject.transform.rotation;

if(crateRotation.z>45)
{
audio.Play();
}

}

Unity returns this: BCE0022: Cannot convert ‘UnityEngine.Quaternion’ to ‘UnityEngine.Vector3’.

That is just because a gameObject.transform.rotation is a quaternion and not a vector. So the error is justified.

Check the quaternion methods here. You should ask for the Euler-angles of your rotation.

gameObject.transform.rotation.eulerAngles.z

Update
Yes you are right about world rotation. Check this method.

gameObject.transform.localEulerAngles.z

Thanks, it solved the issue but not the problem. I’ve got to try a different aproach, it seems to me that it returns the world “Z” not the local from each individual prefab so it’s not working. But thanks anyway, you answer made me check the quaternions methods.