Clamp Camera Rotation

Hello! I am trying to create FPS character scripts from scratch so I can have better control over how the character moves. The movement script worked better than I initially thought it would, though now I have hit a wall. While trying to program the camera movement, I found that the camera can rotate forever on it’s X axis. I tried using a clamp, but I haven’t worked much with rotations, so this is new territory. I’ve looked all over this forum and over Stack Overflow and am having trouble fitting the clamp methods into the context of the script I have. It may be something simple and I may just be ignorant of how to go about it, but any help is greatly appreciated!

Here is the script:

public class CharacterCam : MonoBehaviour
{
    public GameObject playerCam;

    public float Sensetivity = 10.0f;
    public float MaxCameraRotation;
    public float MinCameraRotation;

    private Vector3 rotatePlayerDir;
    private Vector3 rotateCamDir;

    // Use this for initialization
    void Start ()
    {
        
	}

    // Update is called once per frame
    void Update()
    {
        rotatePlayerDir = new Vector3(0, Input.GetAxis("Mouse X"));
        rotatePlayerDir = rotatePlayerDir * Sensetivity;

        rotateCamDir = new Vector3(Input.GetAxis("Mouse Y"), 0);
        rotateCamDir = rotateCamDir * Sensetivity * -1.0f;

        transform.Rotate(rotatePlayerDir * Time.deltaTime);

        playerCam.transform.Rotate(rotateCamDir * Time.deltaTime);
    }
}

I’ve tried many different things, but none have given me any results other than completely limiting the X rotation altogether.

I managed to make it work, although it stutters when hitting the max/min values. Here’s the script in case it’ll help someone:

public class CharacterCam : MonoBehaviour
{
    public GameObject playerCam;

    public float Sensetivity = 10.0f;
    public float minView;
    public float maxView;

    // Use this for initialization
    void Start ()
    {
        
	}

    // Update is called once per frame
    void Update()
    {
        Vector3 rotatePlayerDir;
        Vector3 rotateCamDir;

        bool minValuePassed;

        float xRot = playerCam.transform.localEulerAngles.x;

        if(xRot > 179)
        {
            minValuePassed = true;
        }
        else
        {
            minValuePassed = false;
        }

        rotatePlayerDir = new Vector3(0, Input.GetAxis("Mouse X"), 0);

        rotateCamDir = new Vector3(Input.GetAxis("Mouse Y"), 0);

        transform.Rotate(rotatePlayerDir * Time.deltaTime * Sensetivity);

        playerCam.transform.Rotate(rotateCamDir * Time.deltaTime * Sensetivity * -1.0f);

        if(Input.GetKeyDown(KeyCode.Q))
        {
            Debug.Log("X-rotation" + xRot);
        }

        if(xRot > maxView && xRot < minView)
        {
            if(minValuePassed == true)
            {
                playerCam.transform.localEulerAngles = new Vector3(minView, 0, 0);
            }
            else
            {
                playerCam.transform.localEulerAngles = new Vector3(maxView, 0, 0);
            }
        }
    }
}

If anyone cam help with the stuttering then I’d appreciate help on this topic! But this’ll work for now.