- Home /
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?
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.
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 :)
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!
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.
Answer by alok-kr-029 · Nov 17, 2014 at 12:12 PM
print (this.transform.parent.gameObject.animation.clip.name);
This answer is wrong. This code will always return the default animation clip ins$$anonymous$$d of the one that is playing.
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 ?
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 );
}