• 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
29
Question by Cobradabest · Dec 11, 2012 at 11:29 PM · c#animator

How can I check if an animation is playing or has finished? (Using Animator/C#)

I'm using the animator component to animate my character in my game and C# to code it, and I want the game to know if an animation is playing or has finished, because they are a lot of things I intend to code into the game that would require knowing if an animation is playing of finished.

I've looked it up, and the only solutions I've found were in Javascript and for the Animation component.

If it helps, I'll put my situation in, my character pulls out a gun, I want the game to know that while it does that, the player cannot shoot, reload or change weapons until that animation is finished, how would I do that?

I'm talking about the "Animator" component, not "Animation".

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

12 Replies

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

Answer by thor_tillas · Dec 12, 2012 at 10:58 AM

If you use the animator and mecanim, then I think you should do something like that :

 if (this.animator.GetCurrentAnimatorStateInfo(0).IsName("YourAnimationName"))
 {
    // Avoid any reload.
 }

Be aware that your animation name should be similar to "Layer_Name"."State_Name" but if you use a "sub-state machine" in mechanim then the animation name must be "Sub-StateMachine_Name"."State_Name".

I hope it may help you!

Cheers, Flo

Comment
Add comment · Show 7 · 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 Cobradabest · Dec 12, 2012 at 07:37 PM 0
Share

YES! It works! Thank you so much!

avatar image Cobradabest · Dec 12, 2012 at 09:17 PM 0
Share

Is there one for when the animation has finished? Or even because I've now come across a situation where I would need to check that.

Also, What does the number of the "GetCurrentAnimationStateInfo" represent?

avatar image olivierus · Feb 28, 2014 at 10:17 AM 0
Share

The number is the layer

avatar image Lunatix · Dec 07, 2014 at 12:36 PM 1
Share

You could use AnimationState.normlizedTime to figure out the play count (decimal part) and the current progress (floating part).

2.25f would tell you that the animation has played two times and the current state is 25% (225%). Using modulo with 1.0f gives you always the progress.

AnimationState.normalizedTime

avatar image Salim · Dec 14, 2018 at 03:02 PM 0
Share

From what I understand, you would need to check this condition every frame; is there way that is more optimized, like an event that fires up when the animation finishes?

Show more comments
avatar image
44

Answer by mrSaig · Aug 27, 2014 at 09:12 AM

I check it with the time ... after i switch to another animation (like with a trigger) i have following condition:

 if (Animator.GetCurrentAnimatorStateInfo(0).normalizedTime > 1 && !Animator.IsInTransition(0))


works like a charm for me

the check for animator is in transition check is mandatory cause otherwise there will be for 1 or 2 frames the time of the last state unity resets the time after the transition is completed

Comment
Add comment · Show 2 · 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 IgorAherne · Sep 09, 2016 at 03:08 PM 2
Share

punctuation man, use it!

avatar image Straafe IgorAherne · Sep 04, 2017 at 04:30 AM 0
Share

Nice capital letter at the beginning of your sentence.

Also, the above didn't work for me, but this did:

if (this.GetComponent().GetCurrentAnimatorStateInfo(0).length > this.GetComponent().GetCurrentAnimatorStateInfo(0).normalizedTime) { //Do stuff }

avatar image
11

Answer by edu4hd0 · May 01, 2016 at 12:11 AM

To know if Animator is playing any animation:

  bool AnimatorIsPlaying(){
     return animator.GetCurrentAnimatorStateInfo(0).length >
            animator.GetCurrentAnimatorStateInfo(0).normalizedTime;
  }

To know if is playing a specific animation:

  bool AnimatorIsPlaying(string stateName){
     return AnimatorIsPlaying() && animator.GetCurrentAnimatorStateInfo(0).IsName(stateName);
  }
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 Thunderblade · Jun 01, 2016 at 08:47 PM 1
Share

Thanks this really helps me!!

avatar image jconsole · Mar 31, 2017 at 03:27 AM 5
Share

This was what I needed, too. But Just a heads up here- comparing length with normalizedTime isn't what it seems. Length is the total seconds in the animation and normalizedTime is a mix of loop number and percentage completion- see https://docs.unity3d.com/ScriptReference/AnimationState-normalizedTime.html

This will consider the animation no longer playing after it has completed one full animation:

   bool AnimatorIsPlaying(){
      return animator.GetCurrentAnimatorStateInfo(0).normalizedTime < 1;
   }

Took me a bit to figure why $$anonymous$$e were lasting so long with edu4hd0's code.

avatar image Baalhug · Jul 24, 2019 at 02:11 PM 1
Share

The method shown to know if Animator is playing any animation does not work, it actually makes no sense (tested).

avatar image
7

Answer by YasinJavaid_ · May 19, 2019 at 09:40 AM

@alok-kr-029 i just simply your solution.

just send your animation name in CurrentAnimation you want to check.

  public IEnumerator CheckAnimationCompleted(string CurrentAnim, Action Oncomplete)
     {
         while (!Animator.GetCurrentAnimatorStateInfo(0).IsName(CurrentAnim))
             yield return null;
         if (Oncomplete != null)
             Oncomplete();
     }

calling coroutine

  StartCoroutine(CheckAnimationCompleted("Shoot", () =>
          {
              Animator.SetBool("Shoot", false);
              // Your any code
          }
         ));

 
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
6

Answer by thor_tillas · Dec 13, 2012 at 02:28 PM

I cannot add some code in the comment space, so I have answered there... sorry...

There is no "StateEnded" or "TransitionEnded" event for now on the mecanim system... So the only way to do that is to pull the "CurrentAnimatorStateInfo" and check if you enter the state/leave it.

 if(this.animator.GetCurrentAnimatorStateInfo(0).IsName("YourAnimationName"))
   {
       // Avoid any reload.
       this.InMyState = true;
   }
   else if (this.InMyState)
   {
        this.InMyState = false;
        // You have just leaved your state!
   }

The number is the layer index. Basically, if you have not created a new layer in your mecanim window, you only have one layer at index 0.

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 Cobradabest · Dec 13, 2012 at 02:59 PM 0
Share

Alright, thanks anyway!

  • 1
  • 2
  • 3
  • ›

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

36 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# Acessing animator on another Object 1 Answer

How to change sprite animator speed through text? [2D] 0 Answers

Play animation after certain animation finished 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