How do I retrieve the animation clip that animator is playing?
Option B: I can give the name of the animation and the animator can tell me if it’s being played or not?
How do I retrieve the animation clip that animator is playing?
Option B: I can give the name of the animation and the animator can tell me if it’s being played or not?
That was quite the rabbit hole, but I think I found what you need. If you just need to check if a certain animation is playing, that’s fairly easy:
if (myAnim.GetCurrentAnimatorStateInfo.IsName("Name of Animation Clip")
{
//Bunch of code, Replace myAnimator with a reference to your Animator component
}
Actually grabbing the name of the clip is a little more complicated, because Unity’s animation system can play multiple animations at once. You need to sift through an array:
int w = myAnim.GetCurrentAnimatorClipInfo(0).Length;
string[] clipName = new string[w];
for (int i = 0; i < w; i += 1)
{
clipName _= myAnim.GetCurrentAnimatorClipInfo(0)*.clip.name;*_
}
If you’re only expecting one animation to be playing, then you can replace clipName with a normal string to keep things simpler.
I highly recommend reading through the docs on the Animator component, because there’s some weird, wonderful things in there that are worth knowing about!
_*https://docs.unity3d.com/ScriptReference/Animator.html*_
Although I agree it has been answered many times - it’s not always correct, so please find the correct approach here:
if (GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName(“Attack")){
print("Playing attack anim!");
}
Keep in mind this only works with single layered animators.