- Home /
Mathf.Clamp not working as expected with rotation values
Hey !
I'm having a annoying issue with Mathf.Clamp. The issue is that I can't clamp the rotation between 330 and 30. This script below is attached to a gun held by a character. I want to basicly limit the rotation between 330 and 30. I've tried limiting the angle in between 30 and 330 but then the gun shoots backwards.
I hope you guys could help me out, and sorry about my shitty explanation.
void Update ()
{
Vector3 mousePos = Input.mousePosition;
mousePos.z = -(transform.position.x - Camera.main.transform.position.x);
Vector3 objectPos = Camera.main.WorldToScreenPoint (transform.position);
mousePos.x = mousePos.x - objectPos.x;
mousePos.y = mousePos.y - objectPos.y;
float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
angle += 180f; //So the gun is directed towards the mouse
/*if(angle < 330)
{
transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
}*/
//angle = Mathf.Clamp(angle, 330, 30);
//angle = Mathf.Min(30);
//angle = Mathf.Max (330);
angle = ClampAngle (angle, 330, 30);
//angle = Mathf.Clamp (angle, -100, 100);
//transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
Debug.Log (angle);
transform.eulerAngles = new Vector3(0, 0, angle);
}
public static float ClampAngle (float angle, float min, float max) {
if (angle < 30.0f)
angle += 30.0f;
if (angle > 330.0f)
angle -= 330.0f;
//Debug.Log (angle);
return Mathf.Clamp (angle, min, max);
}
Answer by Graham-Dunnett · Apr 25, 2014 at 03:06 PM
Mathf.Clamp is documented as taking three values. The second is the min value and the third is the max value. Each time you use this API you have the min and max values reversed. I think we can all agree here that 330 is bigger than 30. I suspect what you really want is the angle to vary between -30 and +30. (Otherwise I think you'd say you want to limit between 30 and 330.) So you'd want to precondition the angle to be in the range -180..180 prior to calling Clamp(angle, -30.0f, 30.0f).
Thanks for the answer Graham ! Your answer is correct, I managed to make the gun rotation limatation working if the character is flipped to the left. Do you know how to reverse the code, so the rotation is limited on the opposite side ?
angle = $$anonymous$$athf.Clamp (angle, -50.0f, 50.0f); //Use this if character is flipped to the right
I have encountered the same problem in my code, and my only problem is that mathf.clamp doesn't like negative values. So when I'm trying to limit a cameras rotation between -15 and 35 on the x axis and between -175 and -105 on the y axis. On the x axis, the rotation stops at 35, but as soon as I go over the 0 value on the x axis, it snaps to 35. As for the y axis, it is constantly locked to -105.
$$anonymous$$athf.Clamp works fine for negative values- your issue is in your application of it, which requires some code for people to look at. Please create a new question with the relevant code where the problem occurs (preferably boiled down to exclude everything that isn't necessary to reproduce the issue).
Your answer
Follow this Question
Related Questions
Clamp camera quaternion so you can't look up or down more than 90 degrees? 3 Answers
Clamp Vertical Aiming 1 Answer
Clamp issue 1 Answer
How to clamp rotation 4 Answers