Limit vertical rotation of Camera

Hello, I am new at Unity and C# and I am trying to create a basic FPS controller w/ a limited rotation about the x-axis (vertical rotation).

	public float mouseSensitivity = 3.0f; //Mouse sensitivity.
	public float viewRange = 60.0f;
	float rotVertical = 0;
	
	// Use this for initialization
	void Start () {

	}
	
	// Update is called once per frame
	void Update () {
		//Camera

				float rotHorizontal = Input.GetAxisRaw("Mouse X") * mouseSensitivity;

				transform.Rotate ( 0, rotHorizontal, 0);


				rotVertical = Input.GetAxisRaw("Mouse Y") * mouseSensitivity;

				rotVertical = Mathf.Clamp ( rotVertical, -viewRange, viewRange);

				Camera.main.transform.localRotation *= Quaternion.Euler( -rotVertical, 0, 0);

However, I am still able to rotate 360 degrees vertically. Any help would be appreciated!

Try clamping it after you apply the rotation.

Camera.main.transform.localEulerAngles = new Vector3( Mathf.Clamp( Camera.main.transform.localEulerAngles.x, -viewRange, viewRange), 0, 0);

By the way, Camera.main is a rather costly operation in terms of framerate. I recommend you use the following.

Transform cameraTransform;
void Start () {
cameraTransform = Camera.main.transform;
}

Then, replace all other instances of Camera.main.transform with cameraTransform. Ctrl+H might be helpful.

I needed the logic to camera tilting angle (axis x) in inspector.

I use this:

        // ..  in void Update() in an if statement, only when left mouse is pressed if( Input.GetMouseButton(0)) { ... }
        
        // vertically mouse y delta to last frame
        var fY   = Input.GetAxis(InputAxis.MOUSE_Y);
        var v    = gcProfile.CameraTurnSpeedVertically * fY;
        
        // make rotation
        var rotation = new Vector3(-v, 0, 0);
        var tt       = target.transform;
        tt.Rotate(rotation, Space.Self);
        
        // limits for angle in tilt x axis, as seen in inspector
        const int bottom = 45;
        const int top    = -90;
        
        // convert transform to values, as seen in inspector
        var eulerTiltX = TransformUtils.GetInspectorRotation(tt).x;
        Debug.Log("local euler tilt (x): " + eulerTiltX + " <- " + fY);
        
        // x (inspector angle for tilt angle)
        
        // as long as try to rotate camera vertically in fY direction and the angle in inspector reaches limit ...
        
        if (eulerTiltX > bottom) //  && fY < 0 -- not really nessessary
        {
                 // then: set inspector value to bottom boundary
        	TransformUtils.SetInspectorRotation(tt, new Vector3(bottom, 0, 0));
        }
        else if (eulerTiltX < top) // && fY > 0 -- not really nessessary
        {
        	TransformUtils.SetInspectorRotation(tt, new Vector3(top, 0, 0));
        }

public float SpeedH = 10f;
public float SpeedV = 10f;

    private float yaw = 0f;
    private float pitch = 0f;
    private float minPitch = -30f;
    private float maxPitch = 60f;
    
    void Update()
    {
        CameraRotate();
    }

    void CameraRotate() {
        yaw += Input.GetAxis("Mouse X") * SpeedH;
        pitch -= Input.GetAxis("Mouse Y") * SpeedV;
        pitch = Mathf.Clamp(pitch, minPitch, maxPitch); 
        transform.eulerAngles = new Vector3(pitch, yaw, 0f);
        
    }

That’s what I did and clamping works perfectly.

private float cameraPanMax = 20f; // 20 degree
private float cameraPanMin = 340f; // -20 degree (360 - 20)

// Update() starts here

float moveHorizontal = CrossPlatformInputManager.GetAxisRaw("Horizontal");
float moveVertical = CrossPlatformInputManager.GetAxisRaw("Vertical");

if (moveVertical != 0) 
            {
                Vector3 rotateDir = new Vector3(-moveVertical, 0f, 0f);
                CameraManager.activeCam.Rotate(rotateDir * rotSpeed * Time.deltaTime);

                float currentAngle = CameraManager.activeCam.localEulerAngles.x;

                bool isHigherThanRange = currentAngle > cameraPanMax && currentAngle < 50f;
                bool isLowerThanRange = currentAngle < cameraPanMin && currentAngle > 300f;

                if (isHigherThanRange)
                {
                    CameraManager.activeCam.localEulerAngles = new Vector3(Mathf.Clamp(currentAngle, 0f, cameraPanMax), 0, 0);
                }

                if (isLowerThanRange) 
                {
                    CameraManager.activeCam.localEulerAngles = new Vector3(Mathf.Clamp(currentAngle, cameraPanMin, cameraPanMin), 0, 0);
                }
            }

Solved, This worked fine for me.