Clamp Rotation Problem

Hey guys, after a long time searching about a way to Clamp the rotation, I finally found out something.
Everything’s working Ok, except for one thing.
The cannon I’m using for a game need to rotate along eulerAngles.x axis. The Start X rotation is 270, but even if I move it up or down, the rotation increase it value.

For exemple, when I move mouse up, the rotation increase from 270 to 345(value I want to clamp).
And when I move mouse down, the rotation increase the same value, from 270, to 345.

What I want is that when I move mouse up, the rotation increase and when I move mouse down, the rotation decrease, so that way my Clamp should work correctly. Here’s the code:


var angles: Vector3 = Vector3.zero;

var rotationVelocity: float;

function Start() {

angles.y = transform.eulerAngles.y;

angles.x = transform.eulerAngles.x;

angles.z = transform.eulerAngles.z;

}

function Update() {

print(transform.eulerAngles.x); // that’s how I found out the problem

if(Input.GetAxis(“Mouse Y”) > 0) {

angles.x += rotationVelocity * Time.deltaTime;

angles.x = Mathf.Clamp(angles.x, 270, 345);

transform.eulerAngles.x = angles.x;

}

if(Input.GetAxis(“Mouse Y”) < 0) {

angles.x -= rotationVelocity * Time.deltaTime;

angles.x = Mathf.Clamp(angles.x, 270, 345);

transform.eulerAngles.x = angles.x;

}

}


Sorry about my English, I’m brazilian.
Thanks from now!

Thanks! I could find out and solve the problem!

Very nice tip to make the code cleaner! :wink:

Thank you again!