- Home /
Quaternion.Euler tilts rotation on z axis, even though the z rotation is set to 0
So I've got a first person camera that rotates based on mouse movement, and I am assigning the camera's transform using a quaternion. However, when I create the rotation needed, the camera slowly tilts on the z axis, even though I have clearly set the z rotation to 0. I don't really know how to use Quaternions, and previous answers about this problem have been very specific to the question, and when I try to fix it, I get the exact same problem, no matter how many different ways I write the code differently.
Here is the code I am using:
void Update ()
{
transform.rotation *= Quaternion.Euler (-Input.GetAxis ("Mouse Y"), Input.GetAxis ("Mouse X"), 0);
}
First a guess. You want to yaw in world space (camera should rotate around the world up), while pitching should happen in local space.
Separate your axis and apply the yaw to the rotation of the transform and the pitch to the localRotation of the transform.
Answer by maccabbe · Jul 16, 2015 at 05:57 PM
"Rotating by the product lhs * rhs is the same as applying the two rotations in sequence, lhs first and then rhs."
So let's try rotating the camera by applying a rotation (90, 90, 0) then (-90, 0, 0). Rotate the camera 90 degrees on the x axis then 90 degrees on the y axis and back 90 degress on the x axis. At this point you have a rotation which is a 90 degree turn on the z axis.
To keep the z rotation 0 you probably want to only apply one rotation on the x and one rotation on the y axis. For instance
float xRot=0;
float yRot=0;
void Update(){
xRot+=-Input.GetAxis("Mouse Y");
yRot+=Input.GetAxis("Mouse X");
transform.rotation=Quaternion.Euler(xRot, yRot, 0);
}
Thanks, I read somewhere that multiplying quaternions was the same as adding, I had no idea that was the problem.
Your answer
Follow this Question
Related Questions
Quaternion values at instantiation. 1 Answer
Rotate on 2 different axes 1 Answer
How to smoothly align an object to a surface, but only partially 2 Answers
Rotation 90.0001 and 1.001719 bugs! 1 Answer
Rotate an object relative to Camera 2 Answers