Is it possible to rotate an object around its z-axis without changing its x-axis and y-axis?

Hi Unity Answers,

I’m currently working on a small game involving a rotating cube. The player clicks a button, which Lerps the cube’s rotation on the z-axis by 90 degrees. This works fine, but if I click the other button (which rotates the cube around the x-axis), the cube will behave strangely. Sometimes it will rotate in the wrong direction, sometimes it will jitter. But I know that one of the key problems here is the z-axis rotating the x-axis during the Lerp sequence.

Here is the code I use to lerp:

IEnumerator LerpRotation(float a_Start, float a_End, float a_Duration, string a_Axis)
{
    float i = 0;
    float Rotation;

    //A stylistic choice meant to make the cube rotate faster until it finishes. 
    float IncreaseRate = 0.0002f;
    while (i < 1)
    {
        //a_Duration, in this scenario, is 2.0f
        i += Time.deltaTime * a_Duration * IncreaseRate;
        Rotation = Mathf.LerpAngle(a_Start, a_End, i);

        switch (a_Axis)
        {
            case "x":
                WonderCube.transform.eulerAngles = new Vector3(
                    Rotation,
                    WonderCube.transform.eulerAngles.y,
                    WonderCube.transform.eulerAngles.z);
                break;
            case "y":
                WonderCube.transform.eulerAngles = new Vector3(
                    WonderCube.transform.eulerAngles.x,
                    Rotation,
                    WonderCube.transform.eulerAngles.z);
                break;
            case "z":
                WonderCube.transform.eulerAngles = new Vector3(
                    WonderCube.transform.eulerAngles.x,
                    WonderCube.transform.eulerAngles.y,
                    Rotation);
                break;
        }

        if (IncreaseRate < 2.0f)
            IncreaseRate += 0.05f;

        yield return null;
    }
}

Again, this seems to work fine for the z-axis, but as soon as the x-axis is involved, the cube starts misbehaving. I’ve done some research and found something called Gimbal Lock (found here: Avoiding Gimbal Lock - Questions & Answers - Unity Discussions), which might be related. However, I’m unsure if this is the same thing. I’d just like to be able to rotate around the z-axis without adjusting the direction of my x-axis and y-axis.

I’m thinking a dummy that doesn’t rotate might work, but RotateAround is (according to Unity) obsolete, and I’m not sure how to use a dummy’s axis for rotation.

I’d really appreciate some help on what I can do to improve this rotation code. Thanks very much.

EDIT: I’ve continued researching the problem, but I haven’t found any solutions. I’ve tried researching if I was rotating in local space, but i’m using eulerAngles which is in World Space.

I also recorded a gif of the issue. Sorry for the low quality: Issues Regarding Cube rotation - Album on Imgur

It’s hard to see it, but the jitter I mentioned is demonstrated in that gif. When I click the right button twice, the Y rotation and the Z Rotation jump to 180, despite the fact that only the X Rotation should be changed here.

I’d also like to clarify, as I have a better grasp on the problem now: my issues are that the rotation of the cube is follow the body, which I don’t want, and that the cube is acting strangely when I rotate it on the X-Axis (regardless of the orientation of the Z-Axis and Y-Axis, as seen in the gif).

The basic answer to any question involving gimbal lock is: use quaternions not Euler angles. They work for cameras, rotating/tumbling objects, whatever. Recommended.

In most cases the problem just goes away. Quaternion.RotateTowards() or QuaternionFromToRotation() is your friend.