- Home /
Limit local Rotations on one axis
Hi,
i got problems with limiting rotation of cylinder on one axis. I tried almost everything from eulers to quaternions, but i am sure it is only my lack of experience why i am not able to pull this out.
I managed to limit its rotations on X axis, but strangely same code doesnt work for Z axis. Here is the code i got for limiting the X axis.
I know eulerAngles shouldnt be used for setting values separately, but this is only solution that worked for me on X axis.
// Limit rotations
if(Input.GetButton("down")) { if(transform.rotation.eulerAngles.x > 89) { transform.rotation.eulerAngles.x = 89;
}
} else if(Input.GetButton("up")) { if(transform.rotation.eulerAngles.x < 50) { transform.rotation.eulerAngles.x = 50;
}
}
Thanks in advance.
Luke
What are you trying to do? Let a rigidbody bounce around without turning? $$anonymous$$ake sure a bouncing rigidbody is never too far from straight up? Use up/down to spin an object? Also, from the manual: "Do not set one of the eulerAngles axis separately (eg. eulerAngles.x = 10; )"
Sorry, i should explain it better. I want to rotate cylinder by user input at pivot point, not spin. I know i shouldnt use eulerAngles, but this was only solution i was able to limit rotations with.
rotate is spin. You can rotate end-over-end, like a propeller or like a top. You can use Euler angles, but read the examples. Searching "keys rotate" here gives lot of hints, depending on what you want.
well, as i said, i need to limit the rotation (spin). As you can see in the code i have above, i need to limit it between angle 50 and 90 on X axis. This solution works for X axis, while i know its not good solution as i shouldnt use eulerAngles for this, but i dont know how else do it.
Problem is that while i rotate the cylinder in scene view the values of rotation goes from 0 ( default rotation ) to plus and $$anonymous$$us values. So in scene view when i test it, i can tell that i need to limit it between say 30 and -30 on z axis but when the game runs the rotation doesnt gets $$anonymous$$us values, its getting only 0 - 360 values so the negatie value i tested in scene view is now 330-360 ( the original 0 - (-30)). So thats another problem i dont understand why is appearing.
Answer by Owen-Reynolds · Apr 22, 2011 at 05:43 PM
It's perfectly fine to set rotation using the classic three X/Y/Z rotations (the ones you see in the Editor for rotation, now called Euler angles.) The rule is you have to set them all at once, or else funny things can happen.
To do this, best to have separate vars for the rotations. After you set the euler angles, Unity may convert -10 to 350, or other odd things. If you treat your locals as the "Master copy", it won't matter.
float facing=0, spin=0; // spin is local zRotation, like on a bullet
// somewhere in Update, or FixedUpdaye:
// many ways to set facing and spin. Using arrow keys for a quick test: facing += Input.GetAxis("Horizontal")*120*Time.deltaTime; // standard way to spin at 30 degrees/second: spin += Input.GetAxis("Vertical")*30*Time.deltaTime;
// limit some number: spin = Mathf.Clamp(spin, -30, 30); // the cool kids' way if(spin>30) spin =30; // normal persons' way if(spin<-30) spin =-30;
// this is how the Unity docs say to safely set euler rotations: transform.rotation = Quaternion.Euler(0,facing,spin);
amazing, thanks.Works great. I tried to figure this out myself, but were lost. The trick seems to be in setting each rotation as separate variable, feeding in the axis input and clamp that value. Again, many thanks.
Answer by bardeov · Apr 20, 2012 at 03:39 PM
I fix your script for rotate limiting, but you can´t add numbers lower than 1 and higher than 359 because it is in degrees.
private var firsEnd = false; private var secondEnd = false;
function OnGUI(){ firsEnd = transform.rotation.eulerAngles.y > 359; secondEnd = transform.rotation.eulerAngles.y < 1; }
function Update(){
if (firsEnd) transform.rotation.eulerAngles.y = 359; if (secondEnd) transform.rotation.eulerAngles.y = 1; }
Sorry for my bad englis. I´m from Czech
Answer by mthicke2 · May 15, 2013 at 05:59 AM
for anyone else trying to do this - the easiest way is to use the Unity Rotation Constraint script - Add Component>Script>Rotation Constraint
Your answer
Follow this Question
Related Questions
Limit local rotation 6 Answers
How do you combine local and planar translation? 1 Answer
Rotating Object on its Local World Axis 4 Answers
Rotation Limit 2 Answers