How to constrain camera rotation along the x axis (up and down) in a third person game?

Hi; this is my first post, so if I did anything wrong posting or formatting this, let me know! Below, I have some code for my script for my camera. It allows for my camera to rotate up and down (lets player look up and down) along the x axis using the mouse Y input. It’s implementation was easy enough, but what I’m having trouble with is constraining that camera rotation so that it doesn’t rotate above or below a certain range (top constraint for looking up: -30f; bottom constraint for looking down: 30f). The camera is directly attached to the camera; is that wrong? Anyways, here is my code:

// Update is called once per frame
void Update()
{
    if (transform.rotation.x < 30f && transform.rotation.x > -30f)
    { 
        moveMouseRot();
    }
    else
    {

    }
}

void moveMouseRot()
{
    if (transform.rotation.x < 30f && transform.rotation.x > -30f)
    {
        mouseY = Input.GetAxis("Mouse Y");
        transform.Rotate(Vector3.right, -mouseY * rotateSpeedX//is equal to 50 * Time.deltaTime);
    }
}

Can someone help me with constraining the camera so that it cannot look above or below a certain point? Thanks! (Also, don’t worry about my Y axis rotation (looking left to right), as that is handled by rotating the capsule that is my player.)

How to fix the code so that it formats correctly?

The problem here is that ‘transform.rotation’ is a quaternion, which doesn’t store its values as angles. Quaternions are fairly complex, and as such you aren’t supposed to work with them directly, however there are functions to convert quaternions into formats that are easy to work with (including axis angles). Also, the way you are currently attempting to constrain the values would make the camera unmovable once it reached a border. To properly constrain a rotation, you can clamp its angles;

void Update ()
{
    //Use localRotation rather than rotation so that we rotate relative to the camera's up
    Vector3 angles = transform.localRotation.eulerAngles;
    //Despite what the inspector may show, eulerAngles are always stored in the range of [0,360], so we have to remap to [-180,180]
    float yRot = (angles.x + 180f) % 360f - 180f;
    //Make sure *not* to scale this by Time.deltaTime - mouse input in Unity is automatically delta adjusted
    yRot -= Input.GetAxis ("Mouse Y") * rotateSpeedX;

    //Here we clamp between a min and max rotation (replace these with whatever you want the extents to be). The extents shouldn't be outside the range of [-180,180] and for a camera controller also probably should stay within [-89,89] to avoid incorrect rotation
    yRot = Mathf.Clamp (yRot, -30f, 30f);

    //Apply the rotation by converting back to a quaternion. We can't directly set a single value of eulerAngles, which is why we stored all of the quaternion's angles and only updated one
    transform.localRotation = Quaternion.Euler (new Vector3 (yRot, angles.y, angles.z));
}

As for properly formatting code on Unity Answers, there is an icon of binary (0’s and 1’s) next to the attachment button; this is the button for code. Alternatively, so long as you have four spaces at the beginning of a line it will be automatically formatted as code. Just make sure to leave a blank line before, and no blank line after.