How can i clamp the rotation

hey guys, I was trying to clamp the rotation of the x-axis but for some reason, the z-axis kept changing from 180 to -180 degrees which messed up the clamping

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

public class Came: MonoBehaviour
{
    public float MouseSens;
    float y;
    float x;
    // Start is called before the first frame update
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
    }

    // Update is called once per frame
    void Update()
    {
        x -= Input.GetAxis("Mouse Y");
        x = Mathf.Clamp(x, -90, 90);
        y += Input.GetAxis("Mouse X");
        transform.eulerAngles =  new Vector3(x, y, 0) * MouseSens;
    }
}

I’m new to unity so if someone could help me it would be much appreciated, ty!!

J’ai aussi le même problème :frowning:

100000001218704--1121384--clear.png

Ay!! I’m back again, for some reason when I multiplied each variable separately it worked anyways for anyone who has the same problem here’s how to fix it :

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

public class Came : MonoBehaviour
{
    public float MouseSens;
    float y;
    float x;
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
    }
    void Update()
    { 
        x -= Input.GetAxis("Mouse Y") * MouseSens;
        x = Mathf.Clamp(x, -90, 90);
        y += Input.GetAxis("Mouse X") * MouseSens;
        transform.eulerAngles =  new Vector3(x , y, 0);
    }
}