**Question**by hectorales · Mar 05, 2018 at 06:17 PM · quaternionrotation axisquaternionsgimbal-lockangleaxis

# Gimbal Lock Problem

Hello everyone, I have this problem with Quaternions, I tried to pass the rotation of some objects to the rotation of one object, but at some angles I get the Gimbal Lock problem, like when the X rotation is 90, the Y rotation rotates on the Z axis, this is the code:

```
switch (axis)
{
case Axis.X:
switch (typeOfEditing)
{
case EditType.Rotation:
if (oldRotAxis != transform.localEulerAngles.x)
{
newRotAxis = transform.localEulerAngles.x - oldRotAxis;
objToMove.localRotation = Quaternion.AngleAxis(newRotAxis, Vector3.right) * objToMove.localRotation;
follower.localRotation = Quaternion.Euler(newRot);
oldRotAxis = transform.localEulerAngles.x;
}
break;
}
break;
case Axis.Z:
switch (typeOfEditing)
{
case EditType.Rotation:
if (oldRotAxis != -transform.localEulerAngles.x)
{
newRotAxis = -transform.localEulerAngles.x + oldRotAxis;
objToMove.localRotation = Quaternion.AngleAxis(newRotAxis, Vector3.forward) * objToMove.localRotation;
follower.localRotation = Quaternion.Euler(newRot);
oldRotAxis = -transform.localEulerAngles.x;
}
break;
}
break;
case Axis.Y:
switch (typeOfEditing)
{
case EditType.Rotation:
if (oldRotAxis != transform.localEulerAngles.y)
{
newRotAxis = transform.localEulerAngles.y - oldRotAxis;
objToMove.localRotation = Quaternion.AngleAxis(newRotAxis, Vector3.up) * objToMove.localRotation;
follower.localRotation = Quaternion.Euler(newRot);
oldRotAxis = transform.localEulerAngles.y;
}
break;
}
break;
}
```

Is there a method, to apply a rotation to each axis and evading the Gimbal Lock? I have tried also the Quaternion.Euler but gives me the same results.

**Answer** by hectorales
·
Mar 05, 2018 at 06:53 PM

Quaternions are a complicated thing...

So I got the answer from this:

https://forum.unity.com/threads/understanding-rotations-in-local-and-world-space-quaternions.153330/

which I change:

```
objToMove.localRotation = Quaternion.AngleAxis(newRotAxis, Vector3.up) * objToMove.localRotation;
```

to:

```
objToMove.localRotation = objToMove.localRotation * Quaternion.AngleAxis(newRotAxis, Vector3.up);
```

So pretty much the order actually matters, and now it works.

Can anyone explain why the order matters?

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Need help understanding Quaternion.AngleAxis 0 Answers

How to rotate a local axis of an object to another object? 0 Answers

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

difference between using Quaternion.Set() vs setting fields. 0 Answers

How can I find direction with known 2 projection angles on xz and yz planes? 2 Answers