I’m trying to have my camera rotate on the Y and X axis. Using Rotate works, but when trying to rotate on both the Y and X it causes the world to be at an angle. What I need is a way to rotate the camera along the Y using world up instead of the camera’s up. Anyone know of a way to do this?
Hi,
The problem is your are working in Eular (pronounced “Oil-er”) angles (x, y, z) and in order to arrive at the rotation you want, you need to rotate in X, then Y then Z. This can cause a phenomenon called “Gimbal Lock” which happens when two axis get so close together you effectively lose one of them, and there is no clear axis to rotate on to keep a linear direction while rotating. In other words, when you try and interpolate between two Euler rotations, things get messy. All 3D packages solve this problem by doing transitions in Quaternion rotations, which can describe a direct line around a rotation between a start and end rotation (LERP and SLERP are common ways to do this and are available in Unity so you don’t have to do the math yourself). You are essentially trying to handle an interpolation in Eular space, which means you will need all three axis.
There is an easy solution though, especially for cameras. Simply add a parent game object and drive it in Y, then only set X on your camera. It is very common to split pan, tilt and roll on to three separate objects to keep things clean.
So your saying create a parent, attach Camera to it, and then rotate that parent in Y while rotating the camera in X?
Aren’t you asking how to create the standard asset of the MouseOribit or the MouseLook from the FPS character controller? (techinically MouseLook is MouseOrbit with a distance of zero). Gimbal Lock is handled in the MouseOrbit by limiting the Y axis so that you cannot look straight up or down.
What I just repeated worked.
I’m working on Android so don’t know about that MouseOrbit or MouseLook.
You are correct sir.
(Does anyone get that Saturday Night Llive reference? Am I old?)
Hi guys,
just noticed this thread and Im having a similar problem but strangely parenting isn’t working for me. (I hate rotations)
Firstly I put the objects in a hierarchy
And set up my “clean” rotations on the child object.
After pressing play they still get all f*#ked up.
Any ideas why this might be? It’s so frustrating.
Thanks
Pete
Those two rotations are the same. Rotations are stored as quaternions, and what you see is a conversion from quaternion to euler angles, which has more than one valid result. Floating point imprecision is why the “new” x value is very close to but not quite zero.
–Eric
Thanks Eric5h5,
I guess it makes sense storing them as quaternion rather than additive euler angles. Just sometimes gets kinda confusing.
Pete.