Check if gameobject is playing specific animation

So, this is basic, but weird. Essentially, I want the script to check if the Gameobject “Player” is currently playing the animation, DigUp. Just
If (Player.!anim.IsPlaying(“DigUp”))
And then the script.

But, strangely. Having some issues. But not only that, there’s two pages

Thanks for the help with this one.

There’s two pages because IsPlaying(…) and isPlaying are different (see first letter). Not to mention first is method while second is property. What issues are you having? It doesn’t correctly tell you it’s playing?

Make sure you’re looking at the right class. The Animation component is legacy - you’re probably looking for the Animator class. As far as I’m aware, the Animator class does not have a function that does what you’re looking for directly; however you could use State Machine Behaviours attached to the animation in question to do it.

Woah, this got complicated fast.

Well, to put it in blunt terms, I’m not able to have this script check if Player is currently playing the animation DigUp.
The script I posted, does not work. And I’m now more than a little confused what something being legacy or what state machine behaviors or how that plays into this.

Where did you post the script? I can’t see it.

LEGACY:
When Unity first came out, animations were done using the component named Animation. For a while, this was good and straightforward, but in Unity 4 they introduced a new system, marketed as “Mecanim” and using the component called Animator.

They left Animation in because people would have imported projects and plugins and assets which used the old system, and it’d be rude to just break all that functionality. So instead, it was marked as “Legacy”, and for newly imported objects, you have to sort of go out of your way to use Animation; everything defaults to Mecanim/Animator. So odds are, your character has imported with an Animator component, and if you’re using the class functions from the Animation class instead, You’re Gonna Have a Bad Time ™.

STATE MACHINE BEHAVIOURS:
Mecanim is based on what’s called a Finite State Machine - a set of “states”, each of which may, under certain conditions, lead to a different state. You’ve probably seen the editor window, or at the very least screenshots, that looks like this. Each of those boxes represents a state (and each contains one AnimationClip).

Unity recently added a feature whereby scripts could be attached to the State itself, and receive message when the Animator enters or exits that state (as described in the link I posted above). For your problem, you would have a StateMachineBehaviour that would set a particular variable to true on enter, and to false on exit; if that variable is true, your character is playing that animation.

Why not just keep some sort of variable around and change that when you play your animation, then just check the var and see what is the animation that is playing.

I’m guessing the animation plays for a bit, then when the animation finishes it returns to its default state. His code knows when it enters that animation, but it’ll have no idea when it ends. Hence the StateMachineBehaviour.

1 Like

OK, so, much to my embarrassment, I am still not able to do this.

Here’s the situation.
Player enters the trigger for Mound, which is a prefab.
Mound has a bool attached to it, which if active makes the Player Dig__Down__.
Then teleports to another area, then plays Dig__Up__.

Problem here is simply that while the script works, it kinda breaks if you spam the dig button.
Because there is another Mound gameobject on the other side that reacts to Player entering its trigger so you can effectively break the animation system by warping back and forth.

TL;DR
I am still stuck trying to figure out a system where all the script needs to do is check if Player is currently playing the DigUp animation.
Googling around has just confused me as there’s been some changes so older posts seem to just not work.
And I can’t really wrap my head around StateMachineBehaviors, StarManta.
I’d hate to ask, but it would help with an example of how it would work in this case.

assuming you’re sticking with the legacy then…

from the OP, this is malformed

If (Player.!anim.IsPlaying("DigUp"))

! signifys “not”, you can’t include that where you have

If (!Player.anim.IsPlaying("DigUp"))

this is where the ! goes, assuming “Player” is a reference to something, that reference has a public “anim” property that is an “animation” component and you want to check that that animation component is playing the “DigUp” animation by name.

I’d still recommend that you learn the newer animator system, probably starting with the “controlling animation” tutorials in the learn section

Well, hot damn. I got it working. :slight_smile:
And big thanks for linking me that, Lefty Righty.

I will certainly try having experienced this.
Thanks, guys. Really.