Problem with rotations...

Hi guys,I’m heaving problem with object’s rotations. This works while rotating on 1 axis ( x axis),but if i add another axis(y axis) it causes 3rd axis(z axis) to change.It is quite annoying as I want to update only x and y axis,and leave z axis on 0.

here’s bit of code:

``````playerRot = transform.rotation;

aX = Quaternion.AngleAxis(angleX * rotationXSensitivity * Time.deltaTime, Vector3.right);
aY = Quaternion.AngleAxis(angleY * rotationYSensitivity * Time.deltaTime, Vector3.up);

desiredRotation = aX * aY;
transform.rotation = desiredRotation * playerRot ;
``````

Cheers

That doesn’t work too :

`````` transform.Rotate(new Vector3(camX * Time.deltaTime, camY* Time.deltaTime, 0f), Space.Self);
``````

Could someone explain me why it updates z axis too?

Because that’s how a euler angle works, and is partly why “Gimbal Lock” is a thing and is the reason for Quaternions being used to avoid that. For those two axis to change together, the third axis has to be adjusted to accommodate that rotation. It sounds like you’re trying to do a rotate around, which is what transform.RotateAround() is specifically for and makes it very simple.

Well,I already tried transform.Rotate/RotateAround and I get always similar output. It updates z axis.
Gimbal Lock …That’s why I used Quaternions,but they seems to have similar problems.That rotations drive me crazy.

What’s wrong with that example? I’m using quaternions,but it updates z axis too:

``````playerRot = transform.rotation;
aX = Quaternion.AngleAxis(angleX * rotationXSensitivity * Time.deltaTime, Vector3.right);
aY = Quaternion.AngleAxis(angleY * rotationYSensitivity * Time.deltaTime, Vector3.up);

desiredRotation = aX * aY;
transform.rotation = desiredRotation * playerRot ;
``````

It rotate object around x and y, but over time z axis just goes crazy.
Hmm…I wonder what I’m missing…

The Euler angles still have to follow their own rules, using the Quaternion just allows you to skip over the gimbal lock issue. Why does it matter what values the Z axis has as long as your object is rotating how you want it? If the rotations LOOK correct, then those are the needed values for those rotations in the inspector.

If you absolutely want to avoid that issue, then use a Vertical rotation object and Horizontal child object, with the Camera then a child of that. Then what you do is only rotate the X axis on the Vertical, and the Y axis on the Horizontal, preventing Z axis from being altered in any way and making rotations simple due to the power of parent transform inheritance.

Looking at your video though… That wouldn’t be what you want since you’re just trying to change the forward direction of a moving object.

Somehow transform.localEulerAngles works like a charm,except that 270 deg,but it is not a problem as angles are going to be clamped at around 70deg.
What I noticed is that actually I have no problem with z axis at all,the problem is rotation around x axis. Rotation takes place around world axis not local.And here’s another question.How to set Quaternions rotation around world/local axis?
Eulers got Space.World/Self but what about quaternions?
I read in other posts that order of transform.rotation in :

``````transform.rotations = transform.rotations * quaternionRotation;
``````

is important :

``````// world rotation
transform.rotations =  quaternionRotation * transform.rotations;

// local rotation
transform.rotations = transform.rotations * quaternionRotation;
``````

which makes no sense as result is going to be same.BUT I also read that if it comes to Quaternions, operator ‘*’ means rotation and not multiply.

Sorry for digging into this,but I’m gonna use rotations a lot and I’m trying to understand them,so I won’t be coming back here with every single issue.

Cheers for help…

Me again;)
I spend last few days reading about gimbal lock,quaternions and eulers,and came up with working code,The only problem I got left is rotate around local axis.While using Eulers it’s easy,I’m heaving problem with getting Quaterinions rotation around locals.How can I get local rotation in this example?

``````        rot.y += joyControllerScript.JoyHorizontal() * 50 * Time.deltaTime ;
rot.x -= joyControllerScript.JoyVertical() * 50 * Time.deltaTime;

rotLerped.y = Mathf.Lerp(rotLerped.y, rot.y, 1f * Time.deltaTime);
rotLerped.x = Mathf.Lerp(rotLerped.x, rot.x, 1f * Time.deltaTime);
rotLerped.z = 0f;

playerCamera.transform.rotation =  Quaternion.Euler(rotLerped) ;
``````

Thanks for all suggestions and help

I’m not sure I fully understand this, but in the context of the previous comment, I think you mean you want to set the local rotation, so that it is affected by its parent’s rotation + the one you’re feeding it? If so, have you tried simply setting transform.localRotation?

PS: It seems like you’ve come to some of the same conclusions already, but here’s an article I wrote about why Euler angles fail and some strategies to avoid using them. It may help you organize your thoughts a bit to have the info laid out in words.

What I’m trying to achieve is to rotate camera using lerp on local x ,y and later z (pitch,jaw and roll it calls I think).So camera must be able to rotate 360 and more along all axis…With previous examples I had a problem with gimbal lock and lerp taking short way.The code above works perfect except,that rotations takes place around world axis and not local.When I rotate camera +/- 180 deg around X axis and try to rotate it around Y axis,then camera rolls as camera’s local Z axis is in parallel to world’s Y axis.
All that rotations are quite confusing,but I’m doing my best to understand them

Whats the way to apply this rotations to local axis?

``````playerCamera.transform.localRotation = Quaternion.whatever;