How to rotate gun only on X and Y axis?

I have a game where the player is stationary and enemies walk at them. I have a gun in front of the player view that moves left and right / up and down with input from the mouse. However, The Z rotation of the gun always makes itself flip upside down after enough mouse movement.

Here’s the code.

public class RotateGun : MonoBehaviour
{
    [SerializeField] private float horizontalSpeed = 2.0f;
    [SerializeField] private float verticalSpeed = 2.0f;

    // Update is called once per frame
    void Update()
    {
        float h = horizontalSpeed * Input.GetAxis("Mouse X");
        float v = verticalSpeed * Input.GetAxis("Mouse Y");
                
        transform.Rotate(v, h, 0);        
    }
}

If anyone can send me in the right direction, I’d appreciate it.

Did you try to freeze the axis in the rigidbody?

195425-screenshot-2022-04-23-121213.png