- Home /
Rotating a model with increments
Hi there, I have an hexagon and I want to press a key and rotate it Smoothly by 60 degrees over the Z axis in order to have one of its faces aligned with the horizon, press the key again and rotate another 60 degrees, and so on by 60 degrees to the left/right.
I'm trying a few things but I can't get it to work properly I can make it work perhaps once or twice but it's kind of unstable, can anyone tell me please the best way to do this?
Here is the code I'm using but can't say if it is much of a help.
void Update()
{
if (Input.GetKey(KeyCode.LeftArrow))
{
left = true;
}
if (left)
{
StartCoroutine(TurnLeft());
}
if (Input.GetKey(KeyCode.RightArrow))
{
right = true;
}
if (right)
{
StartCoroutine(TurnRight());
}
}
public IEnumerator TurnRight()
{
rotation = 60;
Quaternion to = Quaternion.Euler(0, 0, rotation);
transform.rotation = Quaternion.Lerp(transform.rotation, to, Time.deltaTime * 3f);
yield return new WaitForSeconds(2f);
transform.rotation = to;
right = false;
yield return null;
}
public IEnumerator TurnLeft()
{
Vector3 eulerAngles = transform.eulerAngles;
if (eulerAngles.z >= 0 && eulerAngles.z <= 60)
{
rotation = 60;
transform.eulerAngles = eulerAngles;
}
else if (eulerAngles.z >= 60 && eulerAngles.z <= 120)
{
rotation = 120;
transform.eulerAngles = eulerAngles;
}
Quaternion to = Quaternion.Euler(0, 0, rotation);
transform.rotation = Quaternion.Lerp(transform.rotation, to, Time.deltaTime * 3f);
yield return new WaitForSeconds(2f);
transform.rotation = to;
left = false;
yield return null;
}
private static float WrapAngle(float angle)
{
angle %= 360;
if (angle > 180)
return angle - 360;
return angle;
}
private static float UnwrapAngle(float angle)
{
if (angle >= 0)
return angle;
angle = -angle % 360;
return 360 - angle;
}
Answer by Nazirzadeh · Oct 18, 2018 at 04:01 AM
By pressing left/right arrow always your left/right variables become true. that means it is possible to call the StartCoroutine(TurnRight()); or StartCoroutine(TurnLeft()); many times before you actually set the variables to false.
Yes but the problem is that if I don't turn it to false, the object keeps rotating and if I set up the rotation value to 60 it just turns 60 but I can't make it go to 120°-180°... :( I want it to go 60 degrees smoothly and stop, press again and go to 120° and son on...
I mean you should turn it to false before StartCoroutine and turn it to true after StartCoroutine execution. Also, you may need to make the arrows input disable until coroutine finishes its job. Is it clear?
something like this:
if(Input.Get$$anonymous$$ey($$anonymous$$eycode.LeftArrow) && readInput==true)
{
readInput=false;
StartCoroutine(TurnLeft());
}
// And inside TurnLeftCoroutine
public IEnumerator TurnLeft()
{
//your code
readInput=true;
}
Your answer
Follow this Question
Related Questions
When applying a 90 degree rotation to Euler Angles, it is over/undershooting sometimes.. 2 Answers
How to rotate an object around another 60 degrees with a keypress? 0 Answers
Use a single rotation axis of a freely rotating object 1 Answer
How to always rotate clockwise 1 Answer
Transform.Rotate producing unexpected results when being used after setting localEulerAngles 0 Answers