Understanding how to use Clamp with mouse inputs...

so I want to clamp the rotation so players don’t do summersaults.

The issue is no matter what values I place in the clamp feature of my code, i can never get it to be right.

I must be doing something wrong or not understanding it.

        if (turretMode)
        {
            Cursor.lockState = CursorLockMode.Locked;
            Cursor.visible = false;

            //obtain the forward direction of main camera
            Vector3 cameraForward = Camera.main.transform.forward;


            //Obtain the center point for the camera from prefab
            turretCameraLocation = turretSelected.gameObject.transform.GetChild(0).GetChild(0);
            //move camera to the centerpoint
            transform.position = turretCameraLocation.position;
            //obtain direction of centr point
            Vector3 turretCameraForward = turretCameraLocation.forward;
            //rotate in the direction the camera is forward
            transform.rotation = Quaternion.LookRotation(turretCameraForward, Vector3.up);

            //mouse inputs based on sensitivity
            yaw += Input.GetAxis("Mouse X") * mouseSensitivity;

            pitch -= Input.GetAxis("Mouse Y") * mouseSensitivity;


            //WHY WONT YOU WORK?!?!?!?!
            pitch = Mathf.Clamp(pitch, pitchMinMax.x, pitchMinMax.y);

            //smoothing mouse rotation
            currentRotation = Vector3.SmoothDamp(currentRotation, new Vector3(pitch, yaw), ref rotationSmoothVelocity, rotationSmoothTime);

            //rotating camera basd on the rotation from mouse inputs
            transform.eulerAngles = currentRotation;

            //obtain weapon part within prefab
            turretWeapon = turretSelected.gameObject.transform.GetChild(0).GetChild(1).gameObject;

            //rotate towards the camera forward direction
            turretWeapon.transform.rotation = Quaternion.LookRotation(cameraForward, Vector3.up);


        }
    }

What do you mean by “it doesn’t work”? Does it just not apply any clamping at all?

What values are in the “pitchMinMax” variable? I assume the intention is to use X for minimum and Y for maximum.

Yes, public Vector2 pitchMinMax = new Vector2(0, 180);

It works to the point i dont summersault, but i want to be able to look up more…cant adjust the values to get any success. if i remove the code the clamping is gone so it works to some degree.

How are you attempting to adjust the values? If you’re changing the script file, the new value of a public variable won’t take. The first time you added the value, Unity compiled the code, noticed a new public variable, set it to the default value(0,180), and then saved that value. When you change the script file, the new value (say, -45,45) becomes the new default value, but because that variable already has a saved value (0,180), it keeps the saved variable.

You can change it in the script file to be a private variable (in which case Unity won’t save values for it), or tweak it in the inspector.

Yeah, weird. I set the public variable above the start class with all thee othere variables. adjusting it in the code didn’t make changes.

When i did it in inspector and changd th values it worked fine. So thanks!