- Home /
[PROBLEM] Camera help?
My camera tends to go upside down when viewing up or down too far, is there a way to get it to stop doing this? I'm currently using a single stick controller to control the camera. Here's my code that I hoped would constrain the camera, but it didn't work:
if (m_Cam.transform.rotation.x < 0.4f && m_Cam.transform.rotation.x > -.4f)
{
m_Cam.transform.Rotate(-lv, 0.0f, 0.0f);
}
else if (m_Cam.transform.rotation.x >= 0.4f)
{
m_Cam.transform.Rotate(-0.4f, 0.0f, 0.0f);
}
else if (m_Cam.transform.rotation.x <= -0.4f)
{
m_Cam.transform.Rotate(0.4f, 0.0f, 0.0f);
}
Thank you so much!
I think there is a "simple mouse rotator" script in the third person or first person default package that might handle what you want. It can be limited to vertical only if you need, and you could limit it just right to whatever angle you need.
What you've described is called gimbal lock. The reason we use Quaternions is that they can avoid this, but when you think in terms of yaw/pitch/roll absolute angles, that returns gimbal lock to the problem. Basically, if you're looking up or down at 90 degrees, there is no difference between rolling or yawing. Rolling is how you turn a camera upside down or sideways, so the confusion of which way is up is the result.
That said, the rotation member of a transform is a Quaternion. Exa$$anonymous$$e the documentation of the Quaternion in Unity and you see they warn you not to mess with the x, y, z or w properties in the Quaternion. They should also warn you that these values don't mean what you probably assume they mean. Testing the Quaternion's x property may not do what you're expecting unless you understand Quaternions. I'm not even sure what the code would do under all expected orientations.
Starting over, basically, consider what Quaternions do for us. They are fashioned like a compass for 3 dimensions. There's a vector inside that points toward a direction, and a factor that describes the rotation in roll along that axis. This means you can apply relative rotations to the Quaternion without any gimball lock issues.
Look through and experiment with the Quaternion class provided by Unity, some attention to the "RotateTowards", the "FromToRotation" and the various examples Google provides on Quaternions (it's a study by itself).
The real point is that if we think in terms of angles by axis, we get gimball lock. When we think the way Quaternions were designed, we think in terms of relative rotations that aren't bound to the world axis, so gimball lock is eli$$anonymous$$ated.
Okay after watching a few videos and reading some on quaternions and gimbal locks I think I understand how it works, although i'm still a bit confused on how to parent one of the properties like z, so that the other properties will move along with them at the same time. I'm still a bit new to C# and would greatly appreciate any method you could lead me to that would solve this issue. Thanks for such an in depth explanation, really helped me understand it better!