- Home /
Camera controller makes camera involuntary rotate around local z-axis
Used this code found here http://unity3d.cba.pl/Documentation/Documentation/ScriptReference/Input.GetAxis.html
float h = horizontalSpeed * Input.GetAxis("Mouse X");
float v = -verticalSpeed * Input.GetAxis("Mouse Y");
transform.Rotate(v, h, 0f);
It works. But if I move the cursor in circles the camera slowly rotates around it's local z-axis tilting the world view. The camera is otherwise stationary.
Does anyone know what to do to rectify this? All help is appreciated.
Answer by fueldown · Jun 02, 2013 at 12:27 PM
use
transform.Rotate(v, h, transform.rotation.z);
OR
transform.rotation += Quaternion.Euler(v, h, 0f);
instead. You are changing current rotation along z axis to 0f, which is not correct.
Thanks, but that didn't work.
Have allready tried:
transform.Rotate(v, h, transform.rotation.z);
That just keeps the camera spinning around the local z-axis even after I stop moving the cursor in circle
The latter:
transform.rotation += Quaternion.Euler(v, h, 0f);
Didn't work, at least not for C# and I don't think the += operator is overloaded for quaternions. But I figured it was supposed to be the same as:
transform.rotation *= Quaternion.Euler(new Vector3(v, h, 0f));
Unfortunately this didn't work either. The camera still rotates around local z-axis.
Edit:
Acutally:
transform.Rotate(v, h, transform.rotation.z);
doesn't keep the camera spinning, but spins it back to it's 0 rotation on the z-axis over several seconds. This is still not what I want tough. I want it to not spin around the local z-axis at all.
my bad with += . I was actually wanted to say *=. Anyways, looks like you got your answer.
Answer by FirstTimeUnited · Jun 02, 2013 at 08:11 PM
Well looks like I finally found the answer. The problem is gimbal lock apparently. All though I thought one of the advantages of using quaternions was to avoid gimabl locks.
If anybody else has this problem use the code posted on the following link. It worked for me. He also explains the problem.
http://answers.unity3d.com/questions/172728/transfromrotate-changes-z-axis-incorrectly.html
Sidenote: Find it weird that gimabl lock is not mentioned in the official documentation for Input.GetAxis(see link at top of post). My code was taken directly from there and didn't work. If we can't rely on the creators of the game engine who can we rely on?
Your answer
![](https://koobas.hobune.stream/wayback/20220613103323im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rotating An Object On Its Axis Once 3 Answers
2D Camera Rotation Along Level Boundaries 1 Answer
transform.rotation doesn't stop 1 Answer
How can I rotate an object towards the mouse in only one axis? 1 Answer
rotation script in unity 3 Answers