- Home /
This question was
closed Dec 25, 2016 at 05:46 PM by
JeffHardddyyy for the following reason:
Other
Question by
JeffHardddyyy · Dec 21, 2016 at 08:16 PM ·
c#euleranglestransform.rotate
EularAngels Question / Lock a Rotation?
So I have this script, and there's this part of rotation :
//ROTATION
if (Input.GetKey(KeyCode.UpArrow))
{//rotate up by 10
gameObject.transform.Rotate(-10f, 0, 0);
}
if (Input.GetKey(KeyCode.LeftArrow))
{//rotate Left by 10
gameObject.transform.Rotate(0, 0, -10f);
}
if (Input.GetKey(KeyCode.RightArrow))
{//rotate Right by 10
gameObject.transform.Rotate(0, 0, 10f);
}
if (Input.GetKey(KeyCode.DownArrow))
{//rotate Down by 10
gameObject.transform.Rotate(10f, 0, 0);
}
Now I am wondering, Is there any way to restrict it so it doesn't go past a certain degree. I know it's called EulerAngels
But I don't know how to implement it into this, Do I do it in separate Parenthesis?
Thanks in advance
Comment
Answer by UnityCoach · Dec 21, 2016 at 09:41 PM
You have to store intermediate values to limit them, something in the like of :
private float xMin = -30;
private float xMax = 30;
void Update ()
{
//ROTATION
if (Input.GetKey(KeyCode.UpArrow))
{
float x = 10f;
if ((gameObject.transform.eulerAngles.x + x) < xMin)
x = xMin - gameObject.transform.eulerAngles.x;
else if ((gameObject.transform.eulerAngles.x + x) > xMax)
x = xMax - gameObject.transform.eulerAngles.x;
gameObject.transform.Rotate(x, 0, 0);
}
}
Though, rotations are tricky if you want to do this on all three axes.
Follow this Question
Related Questions
Why inconsistant calculation when round to X degrees? 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Mathf.Lerp won't stop snapping 2 Answers
What's wrong with my if states of eulerAxis.x in Update? 1 Answer