- Home /
Mathf.Clamp rotation does not work properly
im trying to limit the rotation of the gun on the turret but it just snaps at min and max value. Thanks.
void FixedUpdate() { Mouse(); } void Mouse() { Vector3 pos = Camera.main.WorldToScreenPoint(transform.position); Vector3 dir = Input.mousePosition - pos; float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg; angle = Mathf.Clamp(angle,160,-160); Debug.Log(angle); transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward); }
problem-2-1.gif
(485.4 kB)
Comment
Answer by xxmariofer · Dec 03, 2020 at 01:01 PM
you first need to clamp the angle from -180 to 180, for example, right know angle could be 270 and 270 should be -90 (they are the same angle) but in your code you would clamp it to 160 instead of -90
Your answer
![](https://koobas.hobune.stream/wayback/20220613020955im_/https://answers.unity.com/themes/thub/images/avi.jpg)