Reputation: 1181
I want my character to stop when I press a button on my gamepad or keyboard. The character must do a specific animation when I press the button and nothing else, so no movement at all, just the animation. I'm trying to figure out how WaitForSeconds works, but when I try to use it, it doesn't work. Here the code of the function that calls WaitForSeconds
public IEnumerator Wait()
{
yield return new WaitForSeconds (6);
}
When the bool variable animationTest is true I want the program to wait for 6 seconds
if (animationTest)
{
UnityEngine.Debug.Log ("check1");
StartCoroutine (Wait ());
UnityEngine.Debug.Log ("check2");
animationTest = false;
}
but this doesn't work! check1 and check2 are printed at the same time. I'm missing something. This runs in FixedUpdate()
.
Upvotes: 0
Views: 589
Reputation: 3422
The Coroutine does not work like this. It starts a new (parallel) execution.
In order to achieve the wait you'd have to do it in the IEnumerator
.
public IEnumerator SomethingElse() {
animationTest = false;
Debug.Log("check1");
yield return new WaidForSeconds(6f);
Debug.Log("check2");
yield return true;
}
void FixedUpdate() {
if (animationTest) {
StartCoroutine(SomethingElse());
}
}
Now when you set the animationTest
at some point, you should see the two logs with a time gap of 6 seconds in between.
Upvotes: 2