How to see if animation is playing? (so it doesn't play twice)

So I have a really short animation, what’s get played by trigger via Code.

But the player can repeatedly press the (in my case) Space key and the animation overlaps, at looks really weird.

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space) ==  true && birdIsAlive == true)
        {
            myRigidbody.linearVelocity =Vector2.up * flapStrength;
            if (wingAnimator) ####Here 
            wingAnimator.SetTrigger("trJump");
        }

###Here should stand something like, if current animation is XYZ when pass, else play animation

I only have one animation ( a short wing flap) that goes into empty

I’ve had this issue. Is part of a bigger problem which has unfortunately a lot of wrong solutions online like adding events for animations or callbacks. All bullocks btw because for some reason unity (and I’ve seen the same thing with godot) sometimes they will not fulfill the promise of firing the events you are waiting for. Building your code around these “when animation finished” events made me lost so many hours of trying to understand what is going on, why sometimes it doesn’t work

anyway just so I’m not loosing time writing a dissertation on this topic, the simplest solution I’ve found is to use timers, so when player clicks the button to trigger the animation then you set a bool to false and you start a timer. Before triggering the animation you check if the bool is true else you do nothing.

Once the timer ends you set the bool to true. The timer offcourse should match the animation length.

1 Like

To check the name of the current state on the first layer, you’re looking for this: Animator.GetCurrentAnimatorStateInfo(0).IsName("WingAnimationOrWhateverYourStateIsCalledHere")

I would also recommend transitioning to the state from code over using Any State and triggers. Something like Animator.CrossFade("StateName", .1f) or Animator.Play("StateName"). Any State transitions can get messy fast and triggers have a problem of sticking around if the transition didn’t happen for some reason (like if you have multiple transitions depending on different triggers, only one will trigger and get reset, but the others will stick around, so you can come across weird bugs where the character is transitining to unexpected states just because the triggers are lingering around).

1 Like