How to reduce the speed of rotation?
I have a circle sprite which is constantly rotating, however it rotates way to fast. I have tried increasing the 'WaitForSeconds' but anything above 0.01 gives a choppy movement. Anyone know how to do this? Thanks
IEnumerator rotate ()
{
transform.rotation = Quaternion.Euler(0f, 0f, -4f);
yield return new WaitForSeconds(0.01f);
transform.rotation = Quaternion.Euler(0f, 0f, -14f);
yield return new WaitForSeconds(0.01f);
transform.rotation = Quaternion.Euler(0f, 0f, -24);
yield return new WaitForSeconds(0.01f);
transform.rotation = Quaternion.Euler(0f, 0f, -34f);
yield return new WaitForSeconds(0.01f);
transform.rotation = Quaternion.Euler(0f, 0f, -44f);
yield return new WaitForSeconds(0.01f);
transform.rotation = Quaternion.Euler(0f, 0f, -54f);
yield return new WaitForSeconds(0.01f);
transform.rotation = Quaternion.Euler(0f, 0f, -64f);
yield return new WaitForSeconds(0.01f);
transform.rotation = Quaternion.Euler(0f, 0f, -74f);
yield return new WaitForSeconds(0.01f);
transform.rotation = Quaternion.Euler(0f, 0f, -84f);
yield return new WaitForSeconds(0.01f);
transform.rotation = Quaternion.Euler(0f, 0f, -94f);
yield return new WaitForSeconds(0.01f);
transform.rotation = Quaternion.Euler(0f, 0f, -104f);
yield return new WaitForSeconds(0.01f);
transform.rotation = Quaternion.Euler(0f, 0f, -114f);
yield return new WaitForSeconds(0.01f);
transform.rotation = Quaternion.Euler(0f, 0f, -124f);
yield return new WaitForSeconds(0.01f);
transform.rotation = Quaternion.Euler(0f, 0f, -134f);
yield return new WaitForSeconds(0.01f);
transform.rotation = Quaternion.Euler(0f, 0f, -144f);
yield return new WaitForSeconds(0.01f);
transform.rotation = Quaternion.Euler(0f, 0f, -154f);
yield return new WaitForSeconds(0.01f);
transform.rotation = Quaternion.Euler(0f, 0f, -164f);
yield return new WaitForSeconds(0.01f);
transform.rotation = Quaternion.Euler(0f, 0f, -174f);
yield return new WaitForSeconds(0.01f);
transform.rotation = Quaternion.Euler(0f, 0f, -184f);
yield return new WaitForSeconds(0.01f);
transform.rotation = Quaternion.Euler(0f, 0f, -194f);
yield return new WaitForSeconds(0.01f);
transform.rotation = Quaternion.Euler(0f, 0f, -204f);
yield return new WaitForSeconds(0.01f);
transform.rotation = Quaternion.Euler(0f, 0f, -214f);
yield return new WaitForSeconds(0.01f);
transform.rotation = Quaternion.Euler(0f, 0f, -224f);
yield return new WaitForSeconds(0.01f);
transform.rotation = Quaternion.Euler(0f, 0f, -234f);
yield return new WaitForSeconds(0.01f);
transform.rotation = Quaternion.Euler(0f, 0f, -244f);
yield return new WaitForSeconds(0.01f);
transform.rotation = Quaternion.Euler(0f, 0f, -254f);
yield return new WaitForSeconds(0.01f);
transform.rotation = Quaternion.Euler(0f, 0f, -264f);
yield return new WaitForSeconds(0.01f);
transform.rotation = Quaternion.Euler(0f, 0f, -274f);
yield return new WaitForSeconds(0.01f);
transform.rotation = Quaternion.Euler(0f, 0f, -284f);
yield return new WaitForSeconds(0.01f);
transform.rotation = Quaternion.Euler(0f, 0f, -294f);
yield return new WaitForSeconds(0.03f);
transform.rotation = Quaternion.Euler(0f, 0f, -304f);
yield return new WaitForSeconds(0.03f);
transform.rotation = Quaternion.Euler(0f, 0f, -314f);
yield return new WaitForSeconds(0.01f);
transform.rotation = Quaternion.Euler(0f, 0f, -324f);
yield return new WaitForSeconds(0.01f);
transform.rotation = Quaternion.Euler(0f, 0f, -334f);
yield return new WaitForSeconds(0.01f);
transform.rotation = Quaternion.Euler(0f, 0f, -344f);
yield return new WaitForSeconds(0.01f);
transform.rotation = Quaternion.Euler(0f, 0f, -354f);
yield return new WaitForSeconds(0.01f);
transform.rotation = Quaternion.Euler(0f, 0f, -364f);
yield return new WaitForSeconds(0.01f);
StartCoroutine("rotate");
}
Answer by Eno-Khaon · Jan 30, 2016 at 04:35 AM
Let's condense this a little bit, shall we?
IEnumerator rotate ()
{
while(true) // Keeps it going, use yield statements to avoid infinite loop
{
transform.rotation *= Quaternion.AngleAxis(degreesPerSecond * Time.deltaTime, Vector3.forward);
yield return null;
}
}
For reference, I use "yield return null" for reasons described in another answer (This one). The rotation rate is calculated based on degrees rotated per second, which can be hard-coded or given a variable (as desired), and the rotation axis should be in world coordinates rather than local coordinates to maintain proper order of operations in Quaternion multiplication.
Yes, use a loop. The thing is, if you (the original poster) didn't already know that, get any book about computer program$$anonymous$$g (not Unity -- just a basic book.) It will have a bunch of easy stuff in it, like loops, which you didn't know.
This answer is rewriting your code a lot. It's a more normal way to slowly spin. But you could also make a loop that keeps the same 2 lines you wrote.
Answer by DroidifyDevs · Jan 30, 2016 at 03:53 PM
OK. If you have a script that is THAT REPETITIVE, YOU'RE DOING SOMETHING WRONG.
You're getting choppy results because you are spinning it only 10 degrees at a time. So, you're doing this: -4, -14, -24, -34, -44, -54 etc... meaning that it is 10 times choppier than it would be if you used a loop.
So, to save yourself the PITA of copying code 360 times, learn how to use loops. @Eno Khaon did a good job with his answer, and I clarified why it is so choppy.
Your answer
Follow this Question
Related Questions
Rotation script issue? 2 Answers
Simple Rotate Script won't work! 2 Answers
Unity 2D rotation not smooth? 0 Answers
Unity - Problems with Bone Rotations within constraints 0 Answers
how do i rotate an object? C# 1 Answer