Need help with Quaternion locking

Please help.

I am trying to develop a puzzle game in which you must rotate a cube one face at a time, but I am having a great deal of trouble with the Quartenion.Euler function. It seems that when the Z axis is locally pointing in the direction of Y (i.e. when rotation X equals 90 or 270), attempts to rotate on that Z axis only results in rotations on the Y. :(:face_with_spiral_eyes:

I desperately need to be able to rotate the cube to view each and every side. I have tried everything to fix this, but just cannot fathom the reason for it.

a simplified version of my code (which I created to check that I wasn’t going crazy) is:

using UnityEngine;
using System.Collections;

public class cubeRotate2 : MonoBehaviour {
	private float mouseShiftX;
	private float mouseShiftY;
	public float Xdegrees;

	// Use this for initialization
	void Start () {
		Xdegrees = 90.0f;
	}
	
	void Update () {
		mouseShiftX =  Screen.width/2 - Input.mousePosition.x;
		mouseShiftY = Screen.height/2 - Input.mousePosition.y;
		
		transform.rotation = Quaternion.Euler(Xdegrees, mouseShiftX, mouseShiftY);
	}
}

The other values of X which I have tested (namely 0 and 180) present no such problems, and even the reverse; when the X axis is locally pointing in the direction of Y, presents no such issues. Please Help.

it will probably be easier to deal with localRotation

Sounds like Gimbal Lock

Remarkably, the answer seems to be a combination of both your answers, jlcnz and Tseng. So, my thanks to you both. :):slight_smile:

Looking at the diagrams on the link submitted by Tseng gave me the idea to add a couple of empty game objects in the scene, such that the scene Hierarchy looks like:

cubeRotateZ
cubeRotateY
Cube

And attached to the cube, the script (the cube controls the Xrotation only; cubeRotateY, the localY; and cubeRotateZ, the localZ only) :

using UnityEngine;

using System.Collections;

public class cubeRotate2 : MonoBehaviour {

	private float mouseShiftX;
	private float mouseShiftY;
	public float Xdegrees;
	public Transform rotatorY; //link up empty gameobjects in editor
	public Transform rotatorZ; 

	void Start () {
		Xdegrees = 90;
	}

	void Update () {
		mouseShiftX =  Screen.width/2 - Input.mousePosition.x;
		mouseShiftY = Screen.height/2 - Input.mousePosition.y;

		transform.localRotation = Quaternion.Euler(90, 0, 0);
		rotatorY.localRotation = Quaternion.Euler(0, mouseShiftX, 0);
		rotatorZ.localRotation = Quaternion.Euler(0, 0, mouseShiftY);
	}

}

It’s rough, and still needs a lot of work: but it’s getting there. So thank you.

You can of course also try using Transform.Rotate, maybe it takes care of the gimbal lock