# Mathf.Clamp help

I am trying to get an object that rotates on the X axis by input of mouse Y and stay with in a certain rotation area, this is what I have so far

``````public float Min = 15f;
public float Max = 10f;

void Update ()
{
transform.Rotate  ((Mathf.Clamp(Input.GetAxis ("Mouse Y"), Min, Max)), 0, 0);
}
``````

but all that happens is that it spins in circles non stop. What did I do wrong here. Any help is greatly appreciated.

This behavior makes sense. First of all, your Max shouldn’t be less than your Min value. And most of all, Input.GetAxis will return a value between -1 and 1.
As the value you pass in the Clamp function is always less than the Min, the Clamp function will always return the Min value!
So Input.GetAxis is useless in this case!
And you also should use Time.deltaTime or the result will be different depending of the framerate (I assume that’s not he behavior you expect)

Cheers!

Often a good procedure to follow in a bug is to decide and conquer. See what’s the problem. First, you can try transform.Rotate(x) and replace x with something you know is independent and not bugged. If i.e. transform.rotate(5) works, then you know it’s not that part.

Next, you can try the Mathf.clamp by itself and see what value it turns up.

print(Mathf.Clamp(Input.GetAxis (“Mouse Y”), Min, Max));

You will see that it will turn up as 10, because that’s what clamp does. If your input is smaller than Min, and often mouse input is, then it gets clamped by Min.

If you want to clamp the rotation of your object, you’re going to have to clamp the rotation of the object - not the input.

``````//Add this after your rotation
transform.EulerAngles.y = Mathf.clamp (transform.EulerAngles.y, Min, Max);
``````