Preventing camera 360° camera rotation

Hi! I’m making a camera in my FPS-game and I faced the problem that when I look down or up too much, the camera doesn’t stop at 90°, so I can look at 360°. How can I fix it? Here’s the script:

mouseInput = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y")) * mouseSensitivity;

            transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z - mouseInput.x);

            viewCam.transform.localRotation = Quaternion.Euler(viewCam.transform.localRotation.eulerAngles + new Vector3(0f, mouseInput.y, 0f));

“viewCam” is the “public Camera” object.
Thanks for any help!

You want to keep track of the change in yaw and pitch, then apply it. This allows you to clamp the values of yaw and pitch at predefined values as well, before applying them. So no matter how much the mouse would be moved up, pitch wouldnt change. I could post some code examples for just this, but i believe watching this video would help you a lot more, and give you other useful advice for how to manage a camera:

If you just want to see the working solution, look at 10:30.

1 Like