'Vertical rotation camera Unity [duplicate]

How can I limit the vertical rotation of a camera in unity 3d. Using Mathf.Clamp (xRot, min, max) does not work because its not clamping the angle

float xRot=Input.GetAxis("Mouse Y")*lookSens;
cam.transform.Rotate(new Vector3(-xRot,0,0),Space.Self);


Solution 1:[1]

Add these lines to your code:

Vector3 CurrentCameraAngle = cam.transform.eulerAngles;

if (transform.eulerAngles.x > MaxAngle)
      cam.transform.eulerAngles = new Vector3(MaxAngle, CurrentCameraAngle.y, CurrentCameraAngle.z);
else if (transform.eulerAngles.x < MinAngle)
         cam.transform.eulerAngles = new Vector3(MinAngle,CurrentCameraAngle.y, CurrentCameraAngle.z);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Amir H. Ebadatiyaan