How can I lock the z axis of the rotation of an object????

I’m making a diving FPS character and it moves in the direction in wich the camera is looking.
With the mouse you rotate the player (a sphere with a camera inside) in the X and Y axis, but it also rotates the Z axis and make a weird movement, I want to lock the Z rotation.

Here is the code of the camera movement:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WaterMouseLook : MonoBehaviour {

	public float MouseSensitivity = 100f;
	public Transform playerBody;
	public float xRotation = 0f;

	void Start(){

		Cursor.lockState = CursorLockMode.Locked;
	}
	
	void Update () {

		if (Input.GetMouseButtonDown (0)){

			Cursor.lockState = CursorLockMode.Locked;
		}

		float mouseX = Input.GetAxis ("Mouse X") * MouseSensitivity * Time.deltaTime;
		float mouseY = Input.GetAxis ("Mouse Y") * MouseSensitivity * Time.deltaTime;

		xRotation -= mouseY;
		xRotation = Mathf.Clamp (xRotation, -90f, 90f);

		playerBody.Rotate (Vector3.up * mouseX);
		playerBody.Rotate (-Vector3.right * mouseY);  
        //I WANT TO LOCK THE Z AXIS OF THE ROTATION
	}
}

I’m not sure if this is what you are looking for, but here goes:


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WaterMouseLook : MonoBehaviour
{
    public float MouseSensitivity = 100f;
    public Transform playerBody;
    public float yRotation;
    public float xRotation;

    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {

            Cursor.lockState = CursorLockMode.Locked;
        }

        float mouseX = Input.GetAxis("Mouse X") * MouseSensitivity * Time.deltaTime;
        float mouseY = Input.GetAxis("Mouse Y") * MouseSensitivity * Time.deltaTime;

        yRotation -= mouseY;
        yRotation = Mathf.Clamp(yRotation, -90f, 90f);

        xRotation -= mouseX;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);

        Quaternion rotation = playerBody.rotation;
        rotation.eulerAngles = new Vector3(yRotation, xRotation, 0);
        playerBody.rotation = rotation;
    }
}

I had the same problem, but the way I fixed it was move the x-axis part of the script to the camera so you can look up and down and left and right, but dont get the z-axis tilt.