- Home /
Limiting the camera's rotation using quaternions, camera sticking to top/bottom limit angles
I've done my research here, but can't seem to find a solution.
My script:
void Update () {
deltax = Input.GetAxis("Mouse X");
deltay = Input.GetAxis("Mouse Y");
if (yinvert) deltay *= -1;
print(deltay);
print(Quaternion.Angle(Quaternion.Euler(0, 0, 0), Quaternion.Euler(cam.transform.localRotation.x + deltay+ysens, 0, 0)));
if (Quaternion.Angle(Quaternion.Euler(0, 0, 0), Quaternion.Euler(cam.transform.localRotation.x + deltay+ysens,0,0)) < .7)
{
cam.transform.Rotate(-deltay * ysens, 0, 0, Space.Self);
} else
{
}
transform.Rotate(0, deltax*xsens, 0, Space.Self);
}
This is a mouselook camera.
My problem:
I'm trying to only let the camera rotate if the rotation it will make in the current frame will not make it rotate too far. "too far" is being defined as farther up than pointing directly to the sky (x rotation of >270) or farther down than pointing directly at the ground (x rotation of >90)
What's happening instead is that the camera will not rotate vertically at all. If i remove the "+ deltay*ysens" from the print/if statement lines in the above code, the camera rotates to the limits then gets stuck.
I obviously need to include the rotation in the current frame to prevent sticking, but the camera refuses to move in that case.
What am I missing here?