- Home /
Rotate GameObject using Coroutine
I want to continuously rotate my game object using coroutine. I have tried with my knowledge but didn't get success in that.
IEnumerator ContinuousRotation(){
while(true){
transform.Rotate(Vector3.forward * 10f);
yield return new WaitForSeconds(0.01f);
}
}
Above code I have used but it didn't rotate object. So please someone give suggestions to solve my problem.
Answer by siddharth3322 · Sep 07, 2014 at 01:30 PM
Following code work perfectly for me.
IEnumerator ContinuousRotation ()
{
while(true){
transform.Rotate(Vector3.forward,10);
yield return new WaitForSeconds (0.01f);
}
}
One thing I forgot to remove, I have to remove animator component from my gameobject. Also it is not necessary to write rotation code in update method only.
Here I replied to help other members. I got help from following link SPINNING CUBE
Answer by SuperMasterBlasterLaser · Sep 07, 2014 at 12:57 PM
To make something rotate you must write it inside Update. I think inside your couroutine mus be some kind of trigger to activate and then deactivate:
bool canRotate = false;
void Update() {
if (canRotate) {
transform.Rotate(Vector3.forward * 10f);
}
}
IEnumerator ContinousRotation() {
canRotate = true;
yield return new WaitForSeconds(0.01f);
canRotate = false;
}
Okay, I will check it and reply you shortly. But I don't agree with rotate doesn't work with update method. What was the reason behind this?
No it does not working, it stops at angle 10 on z axis.
Your answer
Follow this Question
Related Questions
Gradually reduce speed 1 Answer
A node in a childnode? 1 Answer
Relative Rotation of GameObject 3 Answers
C# Rotate GameObjects Regardless of List Size 2 Answers
C# GameObject Reverses Z Rotation 0 Answers