- Home /
transfrom.Rotate changes z axis incorrectly
The following code is controlling a camera:
float yRotation = Input.GetAxis("Mouse Y") * rotationSpeed;
yRotation *= Time.deltaTime;
float xRotation = Input.GetAxis("Mouse X") * rotationSpeed;
xRotation *= Time.deltaTime;
transform.Rotate(yRotation, xRotation, 0, Space.World);
Rotation speed is currently set to 50, but that's not the problem. My problem is that for some reason unknown to me, this code manages to alter the z rotation of the camera. As far as I can tell, it really shouldn't!!!!(1) Any insights are welcome.
Answer by aldonaletto · Oct 03, 2011 at 01:36 AM
When you rotate around the Y axis, the X axis stay fixed (you're using Space.World). If you rotate 90 degrees to the right, for instance, the camera will end looking to the X direction. At this angle, if you try to rotate "vertically", you actually will tilt left and right. Changing to Space.Self just inverts the problem: if you rotate around the X axis (vertically), the local Y axis gets tilted, and the horizontal rotation too.
You can make a mouse look script doing some changes in your code:
float rotationSpeed = 50; float yRotation = 0; float xRotation = 0;
void Update(){
yRotation -= Input.GetAxis("Mouse Y") * rotationSpeed * Time.deltaTime;
yRotation = Mathf.Clamp(yRotation, -80, 80);
xRotation += Input.GetAxis("Mouse X") * rotationSpeed * Time.deltaTime;
xRotation = xRotation % 360;
transform.localEulerAngles = new Vector3(yRotation, xRotation, 0);
}
The trick here is that Unity calculates a rotation equivalent to what we expect: it calculates the vertical (around X) rotation first, then combine it with the horizontal rotation (around Y) - without tilting any angle.
The vertical rotation is limited to +/- 80 degrees to avoid a gimbal lock when the angle reaches +/- 90 degrees, and the inversion of the horizontal axis when the angle surpasses these values.
First off, +1 for using 1337 characters! XD Second, why would you set the xRotation to the remainder of 360 division?
The modulo operator is used to return the angle to 0 when 360 is reached: an angle of 375 would become 15 degrees, for instance, which's the same thing in a circumference.
It's a standard precaution when dealing with angles, because some routines may not correctly handle angles outside this range (better safe than sorry!)
Answer by qvision · Jun 10, 2013 at 11:18 PM
How can i do this without clamping the y angles. I have been trying (unsuccesfully) to write a main camera controller that emulates a spaceship and i am getting gimbal lock even though i am using quaternions ?
Your answer
Follow this Question
Related Questions
Rotate and Move towards Mouse Point 0 Answers
Flight Sim Mouselook 1 Answer
Cursor/Mouse movement to controller 0 Answers