How to get an rotation axis in unity (easy for experts)

I want a Vector3’s z axis(name: angle) to get the current object’s z axis of it’s rotation. I wrote something like this,-

angle.z = transform.localEularAngles.z;

But I discovered that angle still stays default to ‘0’ and dosen’t gets the z axis rotation of the object. Is this code is wrong? How can I write that angle.z’s code to get the object’s rotation? If you know the answer please tell me, I’m a beginner with Unity’s codings!
[If my English grammer is bad so I’m sorry. I’m actually not an English man. So my Eenglish grammer can be bad.]

The above code has a typo in it (it is “Euler” not “Eular”), so it will not compile. I’ll just assume you retyped it here rather than copy/pasting it.

The angle returned will be the rotation along the local Z axis between this object and its parent. If there is no parent, it is the world Z axis.

This means if you put it on a child object and rotate the parent, it will continue to STAY at zero, not change. You need to change the actual object.

If you want to see it change, put this little codelet on any GameObject you want, press PLAY and then in the editor, spin it around the Z axis using the editor:

Debug.Log( "angle: " + transform.localEulerAngles.z);
1 Like

If your transform has a parent Transform.localEulerAngles only return the rotation in euler angles of the local rotation. So try to use:

angle.z = transform.eulerAngles.z;

to get the rotation in Z relating to the world space.

I actually wrote ‘Euler’ in my main code but not eular there. But here I suddenly wrote eular, sorry, my mistake.

Here’s half of my code with some changes.

        if (Input.GetKeyDown("left"))
        {
            rotating.z = 1;
        }
        if (Input.GetKeyDown("right"))
        {
            rotating.z = -1;
        }
        if (Input.GetKeyUp("left"))
        {
            rotating = new Vector3(0, 0, 0);
            rzlerp = "toright";
            angle.z = transform.localEulerAngles.z;
        }
        if (Input.GetKeyUp("right"))
        {
            rotating = new Vector3(0, 0, 0);
            rzlerp = "toleft";
            angle.z = transform.localEulerAngles.z;
        }
        if (rzlerp == "toright")
        {
            angle.z *= 0.9f;
            transform.eulerAngles = angle;
            if (angle.z < 5)
            {
                rzlerp = "null";
            }
        }
        if (rzlerp == "toleft")
        {
            angle.z *= 0.9f;
            transform.eulerAngles = angle;
            if (-5 < angle.z)
            {
                rzlerp = "null";
            }
        }
    }
}

I saw in the inspector when I release left arrow key, the angle of course discreases. But when I release right arrow key the angle suddenly changes to more biggere number than 200! And also the rzlerp get’s the string “null”. But I think when I release the right arrow key, the rzlerp should be “toleft” and angle.z should be discreased. Why rzlerp isn’t turning to “toleft” and angle.z get’s bigger number when I release the right arrow key? If you can solve the problem so please help me to solve it.
If you’ll need the full code so ask me. I have no problem to share the full code.