Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by PeteWatch · Aug 19, 2018 at 02:29 PM · animatoryieldcoroutines

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());
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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. alt text

This is all awfully complicated for the simple 2D animations I'm trying to trigger.


screen-shot-2018-08-19-at-175222.png (104.2 kB)
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dalessan9 · Aug 19, 2018 at 04:19 PM 0
Share

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

avatar image PeteWatch dalessan9 · Aug 19, 2018 at 05:00 PM 0
Share

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...

avatar image dalessan9 PeteWatch · Aug 20, 2018 at 04:36 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

106 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges