Mathf.Clamp having strange behaviour

60 Views Asked by At

So i have a simple character controller but i want to clamp the camera so that i cant turn 180 degree vertically. As far as i know my code should have worked.

The values i want it to clamp by are min-70 and max70. But whenever i enter those as Min and Max nothing changes. This is my current code

            float x = Mathf.Clamp(_joystick.Vertical * (_moveSpeed * Time.deltaTime), camMin, camMax);
            myCamera.transform.eulerAngles -= new Vector3(x, 0);

I feel like im missing something by now but i am unable to find what. So any help would be appreciated :D

1

There are 1 best solutions below

3
JonasH On

You are only clamping the change in angle, not the actual angle. You should do something like:

var angles = myCamera.transform.eulerAngles;
var x = angles.X + _joystick.Vertical * (_moveSpeed * Time.deltaTime);
x = Mathf.Clamp(x, camMin, camMax);
myCamera.transform.eulerAngles = new Vector3(x, angles.Y, angles.Z);