Clamping Rotation In Unity2D

I’m currently working on a rocket ship script, and I want it to clamp rotation. It’s also following my mouse and moving towards it on the X-axis. It is constantly moving up on the Y-axis. I’ve been confused with this issue for a while now. Here is my code:

void FaceMouse()
    {
        Quaternion rotation = transform.rotation; //Right here is the beginning of the attempt to add a clamp.

        if (rotation.z > 180)
        {
            rotation.z = rotation.z = 360;
        }

        rotation.z = Mathf.Clamp(rotation.z, -40f, 40f);
        transform.rotation = rotation; // This is the end.

        Vector3 mousePosition = Input.mousePosition;
        mousePosition = camera.ScreenToWorldPoint(mousePosition);

        directionX = (mousePosition - transform.position).normalized;
        directionY = transform.position.normalized;

        transform.up = directionX;
    }

From my limited skills, it seems your rocket will never pass the 40 angle. If you want it to keep rotating, you need to get the current rotation and add the mathf.clamp - Meaning you will add rotation… but clamp it at 40 per rotation call. Like I said, I don’t see the add to existing rotation here thus why it keeps going up.

Oh you read it wrong. I want it to keep going up, but the way I’m clamping it won’t actually clamp it. I’m trying to clamp the rotation. But… what you said about clamping per rotation call makes me want to put it in Update instead. I will test this tomorrow. Thanks