# Why does only the first of sequential quaternion operations work?

This is an add on to my previous question about camera flipping with the first mouse move. The solution there works, but, having to resort to compensating torque feels a little dirty. So, I’ve been fiddling with quaternions again. I have a code segment that works for rotating the camera around the X axis and another that works for rotating the camera around the Y axis, but, if I put the code segments in sequence, only the first one works. The axis that follows seems frozen. Switching the order changes which axis is frozen. This happens even with both segments in LateUpdate(). And, if one segment is in Update() and the other is in LateUpdate() then the one in LateUpdate() succeeds. Is there some reason why one quaternion operation locks out a following quaternion operation? The code segment follows:

``````if (!firstpass) {

// Do Y operations:

rotationY += Input.GetAxis("Mouse Y") * cameraSensitivity * Time.deltaTime;
while (Mathf.Abs(rotationY) > 360) {
rotationY = (Mathf.Abs(rotationY) - 360) * Mathf.Abs(rotationY)/rotationY; // abs * sign
}
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
Quaternion quaternionY = Quaternion.AngleAxis(rotationY, Vector3.right);
transform.localRotation = initialRotation * quaternionY;

// Do X operations:

rotationX += Input.GetAxis("Mouse X") * cameraSensitivity * Time.deltaTime;
while (Mathf.Abs(rotationX) > 360) {
rotationX = (Mathf.Abs(rotationX) - 360) * Mathf.Abs(rotationY)/rotationY; // abs * sign
}
rotationX = Mathf.Clamp (rotationX, minimumX, maximumX);
Quaternion quaternionX = Quaternion.AngleAxis(rotationX, Vector3.up);
transform.localRotation = initialRotation * quaternionX;

} else {

initialRotation = transform.localRotation;

rotationX = 0;
rotationY = 0;

firstpass = false;

}
``````

Hi

I can’t guess off the top of my head exactly whether your calculations are correct, but there’s definitely a problem there. You basically go:

• do some calculations for Y
• set my rotation based on those calculations
• do some calculations for X
• set my rotation based on those calculations

So the 2nd step always overrides the first. You might removing line 11 altogether, then changing line 21 to:

transform.localRotation = initialRotation * quaternionX * quaternionY;