- Home /
How do I properly use Coroutines, yield and animators to sequence commands ?
I'm having a hard time understanding how yield works inside coroutines.
I'm using a coroutine to start a series of animators in a certain sequence.
When I use waitForSeconds in between animator.play commands, this works as I would expect... however, when I want to wait when an animator has finished, and then execute the next command, it does not work.
In my example below, the last command (loadScene2) should only be fired after the animator (scene1-ufos) has finished, but instead it is fired at the same time as my last animator.play command.
What am I doing wrong here ?
IEnumerator startMikeAnimation()
{
animatorMikeDigging.Play("scene1-mikefadein");
animatorMikeDirt.Play("scene1-mikedirt");
yield return new WaitForSeconds(5);
animatorVondsten.Play("scene1-vondsten");
yield return new WaitForSeconds(5);
animatorUfos.Play("scene1-ufos");
while (animatorUfos.GetCurrentAnimatorStateInfo(0).normalizedTime < 1.0f)
{
yield return null;
}
StartCoroutine(loadScene2());
}
Answer by dalessan9 · Aug 19, 2018 at 03:37 PM
yield return null; - returns nothing - and I'm pretty sure it just breaks the while loop
animatorUfos.Play("scene1-ufos");
yield return new WaitForSeconds(animatorUfos.GetCurrentAnimatorStateInfo(0).length - 0.01f);
should do the trick - I subtract the 0.01f - since I was using it for a short not really looping anim and would sometimes see it loop back to the first frame(object pooled explosion) - your mileage may vary
Answer by PeteWatch · Aug 19, 2018 at 04:02 PM
Many thanks for that snippet... this looks like a cleaner way to achieve what I am trying, but sadly, it does not work in my case..
It seems that when I play a "state" called "scene1-ufos" from my Animator animatorUfos, that this clip does not reflect my currentAnimatorState... Is that possible ?
I think that my empty "New State" is always seen as the currentState, but I'm not sure.
There's no way to debug.log the currentstate name, and I can't find a way to programmatically set the current state.
My animator is set up like this, I have added an empty new state to make sure this animation does not start automatically. I think I'm doing this wrong.
This is all awfully complicated for the simple 2D animations I'm trying to trigger.
It seems that when I play a "state" called "scene1-ufos" from my Animator animatorUfos, that this clip does not reflect my currentAnimatorState
not 100% sure what you mean by this but if you only have 1 single animation for this thing - you can simply have it start w/ a disabled animator component - then enable the component - when you want it to play
aside from that, the animator tends to be a tad more complicated using the parameters section to set up variables that can then be used to deter$$anonymous$$e what animation can link to each other -- like bool "isJumping" - w/ a link from a standing/walking/jumping animation to the jump animation on the condition that bool toggles true - that you can then reference w/ script etc
This comment was really helpfull. I had no idea I could enable the Animator component in script.
The problem with my initial script on top, is that animatorUfos.GetCurrentAnimatorStateInfo(0).length always returned 0 because the empty "New State" was my default AnimatorState. Even when starting the "scene1-ufos" clip, would still return 0 because my currentAnimatorState was still the empty "New State"... thus my script would immediatly exit and go to the next line.
You comment about enabling the component on demand means that now I can have "scene1-ufos" as my default AnimatorState, and now the script works.
I understand that many games rely on the many features in the Animator, I have to say that for simple fading in and out, and a few transform tweaks and keyframes, the Animator is overkill... It feels like I'm killing a fly with an elefant...
you can disable anything you want through script -- I disable player colliders when they get a shield for example
Yeah, the animator is incredibly powerful - pretty sure it's dev'd in a way the overhead is nowhere near what you'd imagine -- I use it for a lot -- like animating the transform when designing an enemy rather than moving them via script.
Your answer
Follow this Question
Related Questions
Waiting for a coroutine to complete before getting a return value 1 Answer
2D Animation does not start 1 Answer
Delay Jump 1 Answer
Waiting for an Animator to finish playing? 0 Answers
Coroutine not running 1 Answer