Mathf.Clamp not working, and how to keep z to 0?

Hi, I am currently making code on unity C# and I am encountering an error where I cannot clamp xRotation to -90 and 90 degrees, meaning I am allowed to look up and down with no limits, which I do not want.
I’m not really an expert programmer and I’ve been following Brackey’s tutorials (The video I was following FIRST PERSON MOVEMENT in Unity - FPS Controller - YouTube) but this isn’t working and I am not sure why.

        //Moving the player and camera dependant on the mouse x and mouse y
        float mouseX = Input.GetAxis("Mouse X") * sensitivity * Time.deltaTime;
        float mouseY = Input.GetAxis("Mouse Y") * sensitivity * Time.deltaTime;
        float xRotation = 0f;

        //Clamping xRotation
        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -9f, 9f);

        //Transforming the camera and player on the mouse x
        cam.transform.Rotate(xRotation, mouseX, 0f);
        player.transform.Rotate(0f, mouseX, 0f);

That is the area where I am encountering this problem. I would also appreciate if you could tell me how to keep the z rotation to 0, I don’t want my camera to be slanted.

Many thanks for any help :slight_smile:

You are clamping the maximum amount of rotation at once (when mouse has moved) and do nothing actually to limit the camera or player rotation. What you need to do is to check your rotations after you have applied changes and maybe revert angles back to some desired limits