• 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 roamcel · Aug 07, 2011 at 11:10 AM · animationplaycurrent

Retrieving the name of the animation currently playing

I thought this was trivial at start, but after looking and searching, I haven't found a solution. How can you retrieve the name of the animation currently playing on a gameobject? I certainly know of isplaying(), but there's no overload for it, and it only returns a boolean. I think it's unnecessarily expensive to cycle through the animation list to find which one is playing.

So basically i need to get the name of the animation currently playing. Any way to do that?

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

9 Replies

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

Answer by Waz · Aug 07, 2011 at 12:16 PM

You can't, because multiple clips can be playing simultaneously - i.e. there is no current. You can iterate through the animation states, look at the clips and weights, as you're mentioning:

 var bestWeight = -1.0;
 var playing : String;
 for (s : AnimationState in animation) {
     if (s.enabled && s.weight > bestWeight) {
         playing = s.name;
         bestWeight = s.weight;
     }
 }

Alternatively, record the animation you last played when you start/play/queue it.

If you say what you're ultimately trying to achieve by knowing "what is playing" (i.e. what that might mean in your game), there may be other possibilities.

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 roamcel · Aug 07, 2011 at 12:41 PM 0
Share

You've led me to understand that what I need was the 'last played' animation, rather than the heaviest one, so your suggestion to memorize the last animation I play is the perfect solution.

Thanks for the explanation, the example, and the solution :)

avatar image thesfid · Jan 24, 2014 at 03:43 PM 0
Share

This is just what I needed. I'm going to need to affect the speed of animations without necessarily knowing what the names of them will be, and the animation utility only works for debugging. +1 from me!

avatar image
1

Answer by Frescosteve · Jan 16, 2017 at 08:05 AM

I know that this post is relatively old now... For anyone else looking over this:

What you should do is assign an integer value parameter to track the current state. Then you can check against the variable to determine which state it is in. AnimationState = 0 (Idle) AnimationState = 1 (Walking) etc...

animator = GetComponent(Animator); int isPlayingWalking = animator.GetInteger("AnimationState");

So if your animation is within a certain state, whether there are multiple animations or not, you can determine whether it is playing by your parameter.

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 alok-kr-029 · Nov 17, 2014 at 12:12 PM

print (this.transform.parent.gameObject.animation.clip.name);

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 taxvi · Sep 17, 2015 at 02:49 PM 0
Share

This answer is wrong. This code will always return the default animation clip ins$$anonymous$$d of the one that is playing.

avatar image
-1

Answer by LawyerOfGod · Feb 28, 2015 at 02:43 AM

I Dont understand why everyone says that you cannot get the name.. here is an example or maybe i was missunderstood the problem ?

http://docs.unity3d.com/ScriptReference/Animation-clip.html

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 scplayer1 · Mar 10, 2016 at 02:17 AM 0
Share

This only returns the default

avatar image
0

Answer by Ali-Nagori · Aug 14, 2013 at 11:50 PM

that is one way to achieve it if you are using one animation per state method

 public string GetCurrentPlayingAnimationClip
 {
     get
      {
 
         foreach(AnimationState anim in animation)
         {
        if(animation.IsPlaying(anim.name))
            {
               return anim.name;
            }
         {
       
         return string.Empty ;
    }
 }


 void Update()
 {
       print( "Now Playing : " + GetCurrentPlayingAnimationClip );
  
 }
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
  • 1
  • 2
  • ›

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

How to get name of animation playing currently ? 1 Answer

animation play in game over 1 Answer

Play animation when 2 keys pressed? 3 Answers

Character won't play animation 8 Answers

Animation Play Problem (Att: Video Example) 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