How to put limit for Transform.rotate in unity 2d
I know how to rotate using transform.rotate by typing transform.rotate (0, 0, 45). But if i put it in void update(), it will nonstop rotate for every 45 degrees. And if i put it in void start() the plane already turn 45 degree when the game is running. What i want is i want it rotate smoothly from 0 degree and finish at 45 degree. But i dont know how to code. Please teach.
Answer by OutOfRam · Aug 16, 2016 at 05:45 AM
This is not too hard to accomplish, you are going to want to run a co-routine here. a co-routine is like a function(update() or start()) but it runs one iteration at a time and can be paused between iterations. try something like this
private IEnumerator Rotator()
{
for (int i = 0; i = 45; i++) // will run the loop 45 times
{
transform.rotate(0, 0, i); //will increase the rotation by one every loop iteration
yield return new WaitForSeconds(0.1f); // whatever time you want between loop iterations in seconds put in brackets so 1.0 would be on for 1 second then turn off 0.1 would be a tenth of a second
}
yield break; // will stop the co-routine after all 45 iterations
}
please note that the co-routine should be called as follows
StartCoroutine("CoroutineName");
so whatever you name your coroutine should be put where coroutineName isin the case of the code above it would look like
StartCoroutine("Rotator");
I hope this helps
Your answer
Follow this Question
Related Questions
Problems With .rotate behavior 1 Answer
Cant stop object/ridgidbody from rotating 2 Answers
Rotate player with mouse. 0 Answers
Rotate Camera in Y axis without visibly changing angle in Z axis 2 Answers