Rotation Problem - Simulating Gimbal lock

I’m Having a problem with rotations.
I’ve done some research about this problem but it seems that everyone solves it with rotateArround.
I’m trying to replicate the rotations of a SpaceShip on a graphic representation. For that, I have to replicate its rotation on X, Y and Z on another object.
My object 1 is a gray cube and object two is a Colored cube with other cubes in it.

The red represents X.
The green represents y.
The blue represents z.

When I rotate cube 1 on X axis, the cube two red (and its children) should rotate the same amount.
The Y and Z work ok but the X is big problem.
55401-cubes.png
[55403-cubes-3.png |55403]

In the end - the lowest cube on the hierarchy should be on the same position that the reference object.

The problems are:
-x only rotates between [270, 90] and after that the cube rotates in a weird way.
- when I copy the rotation values using quaternions the W recalculates automatically and I can’t copy on only one axis.

The CODE:

			if (xBool) {
				transform.localEulerAngles = new Vector3 (originalObj.localEulerAngles.x, transform.localEulerAngles.y, transform.localEulerAngles.z);
			}
			if (yBool) {
				transform.localEulerAngles = new Vector3 (transform.localEulerAngles.x, originalObj.localEulerAngles.y, transform.localEulerAngles.z);
			}
			if (zBool) {
				transform.localEulerAngles = new Vector3 (transform.localEulerAngles.x, transform.localEulerAngles.y, originalObj.localEulerAngles.z);
			}	

Can someone help me?

thank you for your time!

Some Unity rotation functions and angle based stuff tend to work up to 180 degrees. Working in Euler in Unity is a dangerous thing in general and you should consider contructing a quaternion out of your rotational information. Look at Quaternions in the Scripting API. You will find you can modify a quaternion using euler if you need. Unity sometimes cant decide if its 90 or -270 etc. You need to add some data conditioning to your degrees before you use the data. if(degrees < 0) //This kind of thing. { degrees += 360; } Sorry if Im of no help whatsoever.

1 Answer

1

Instead of editing the localEuler angles directly I believe you can avoid Gimbal Lock by using Unity’s built in Euler function:

transform.rotation = Quaternion.Euler(originalObj.localEulerAngles.x, transform.localEulerAngles.y, transform.localEulerAngles.z);