- Home /
Creating limits on how far something can rotate.
I'm needing to add the ability to look up/down in my game, which I have figured out, but I need to assign limits on how far you can look up and down.
How would I go about doing this with the simplest code possible?
I was thinking that a simple if-then statement saying that if the rotation is above 90 degrees upwards, it would go back to 89 would be ideal, but to do that I need to know how to get the angle of an object.
So what do?
Answer by hathol · May 20, 2012 at 04:55 PM
Assuming that up/down is a rotation around your local x axis
// get your local rotation as euler angles
Vector3 localTempRot = transform.localEulerAngles;
// transform the range from 0 -> 360 to -180 -> +180
// not necessary, but it makes things easier
if(localTempRot.x > 180)
localTempRot.x = 360 - localTempRot.x;
// check and change if necessary
if(localTempRot.x > 90)
localTempRot.x = 89;
else if(localTempRot.x < -90)
localTempRot.x = -89;
//assign back to your object
transform.localEulerAngles = localTempRot;
Your answer
Follow this Question
Related Questions
Is anyone able to fix my code? 3 Answers
Transform.rotate limits 3 Answers
How can I set a limit to my object rotation???(Here's the code) 2 Answers
Camera Limit Rotations Problem 1 Answer
Limiting the rotation of an object 1 Answer