How to check if Animator stopped playing and restart it?

There’s a 2D sprite animation in my game, and it is played in specific conditions. In the animation state machine, there’s 2 states called “Empty” and “Effect”. Empty state is for preventing the animation from playing when the game started. The transition between “Empty” and “Effect” happens if I chance a parameter to boolean. I have 2 questions about it.

  • How can I check if the animation stopped playing?
    You may suggest Animation Events, however, I can’t use Animation Events since there’s a object pool mechanic under the hood. No need to explain the logic. I just want to know if there’s a way to check if the animation stopped? (The animation inside the “Effect” state plays once).

  • How can restart the animation?
    After the animation is played, the animation state machine will be stuck in the “Effect” state. Is there a way to start the animation state machine all over again (from the “Entry”)?

I know there are some other ways to achieve what I want, but I’m actually looking for a way to do in my way. The things in the project are a bit complex.

I would add an exit time transition from the Effect back to the Empty state. This way, when the animation has finished playing, the state machine will automatically transition back to Empty and can be triggered again (you need to reset the parameter and better use a trigger instead of a bool).

To know when it transitions back to the Empty state, you could check Animator.GetCurrentAnimatorStateInfo or add a state machine behavior and use its StateMachineBehaviour.OnStateExit message.

Can’t you just use trigger instead of bool and then add transition back to empty without any condition? Not sure how complex your animations will be tho.