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
1
Question by marcin107 · Jun 27, 2016 at 07:50 AM · c#animationanimatorwaitforseconds

Animator - Wait until animation finishes

Hello! I'm writing a script which will handle monster attack. Script plays Attack animation in Animator component by trigger. I want to stop monster from moving before the animation start and resume his moves after animation ends. I wrote this so far, but agent starts before animation ends. I must add that during the fight monster may get pissed and start to move faster(animation play speed multiplier).

 anim.SetTrigger("Attack");
 agent.Stop();
 yield return new WaitForSeconds(anim.GetCurrentAnimatorStateInfo(0).length+anim.GetCurrentAnimatorStateInfo(0).normalizedTime);
 Debug.Log("Attack has ended!");
 agent.Resume();

If you need something more than this code please let me know. I would really appreciate your help

Comment
Add comment · Show 5
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 Cherno · Jun 27, 2016 at 10:45 AM 2
Share

I find it easier to just add AnimationEvents to the clip at the start and/or end which, in your case, stop and start the movement by calling those functions. It's much more reliable than using a CoRoutine because an AnimationState might get interrupted and this wouldn't be caught be the CR timer.

avatar image marcin107 Cherno · Jun 27, 2016 at 03:18 PM 0
Share

I didn't know about existance of such thing. I'll check it and let you know.

avatar image marcin107 Cherno · Jun 30, 2016 at 04:07 PM 0
Share

Hi, i've been playing with your idea for a while. This solution looks good, but i don't like the thing that i have to divide my function into 2 or 3 other functions.

avatar image DerWoDaSo · Jun 27, 2016 at 11:51 AM 0
Share

Have you checked, that the "CurrentAnimatorState" is the state you think it is? Happened to me, that I checked the previous state, not the current.

avatar image marcin107 DerWoDaSo · Jun 27, 2016 at 03:20 PM 0
Share

I'm not sure if i understand you. Is the state a current animation being played? If yes, than the State is the right one.

3 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by Eco-Editor · Jul 19, 2017 at 01:45 PM

Thanks so much @marcin107 This did the work for me: yield return new WaitForSeconds(anim.GetCurrentAnimatorStateInfo(0).length+anim.GetCurrentAnimatorStateInfo(0).normalizedTime);

Comment
Add comment · Show 1 · 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 TheRealCoract · Jul 17, 2021 at 02:04 PM 0
Share

I know this post is old but this solution worked for me. Why does this work, wouldn't this code add the total length of the animation with the length of the animation normalized between 0-1? I don't get it I've been trying to fix this for hours and I finally have but I don't understand how this works!

avatar image
2

Answer by Crimx · Jun 28, 2016 at 07:23 AM

Try this code. I was using it for my game and it work perfectly. But I was using it at Update method. Haven't try this code at IEnumerator though

            //if animation with name "Attack" finished
            if (anim.GetCurrentAnimatorStateInfo(0).IsName("Attack"))
             {
                 // do something
             }
Comment
Add comment · Show 1 · 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 vagelis199 · Jan 04 at 05:25 PM 0
Share

this works but you wrote it the wrong way. this need an exclamation mark at the beginning like so if (!anim.GetCurrentAnimatorStateInfo(0).IsName("Attack")) { and it should be commented as, if animation with name "Attack" is not playing do something

avatar image
1

Answer by petru23 · Nov 22, 2018 at 03:52 PM

@marcin107 This worked for me as well: yield return new WaitForSeconds(anim.GetCurrentAnimatorStateInfo(0).length+anim.GetCurrentAnimatorStateInfo(0).normalizedTime);,@marcin107 this worked for me as well thank you yield return new WaitForSeconds(anim.GetCurrentAnimatorStateInfo(0).length+anim.GetCurrentAnimatorStateInfo(0).normalizedTime);

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

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

12 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

Related Questions

How can i use the Animator in script to find when animation clip finished playing and then stopping the animation ? 1 Answer

Conflicting Animator SetTrigger 0 Answers

Set boolean to false when animation changes? 1 Answer

pause Animation 0 Answers

Is it possible to always make animations play differently? 0 Answers


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