Animator has a GetCurrentAnimatorStateInfo method that receives information about the current state on the specified Animator Controller layer (AnimatorController). That is, it returns AnimatorStateInfo So they should be used.
AnimatorStateInfo , in turn, has an IsName field - which says whether the name same as the active state in the statemachine . In the end, you might get something like this:
private Animator animator; private void Start() { animator = GetComponentInChildren<Animator>(); } public bool IsAnimationPlaying(string animationName) { // Π±Π΅ΡΠ΅ΠΌ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡ ΠΎ ΡΠΎΡΡΠΎΡΠ½ΠΈΠΈ var animatorStateInfo = animator.GetCurrentAnimatorStateInfo(0); // ΡΠΌΠΎΡΡΠΈΠΌ, Π΅ΡΡΡ Π»ΠΈ Π² Π½Π΅ΠΌ ΠΈΠΌΡ ΠΊΠ°ΠΊΠΎΠΉ-ΡΠΎ Π°Π½ΠΈΠΌΠ°ΡΠΈΠΈ, ΡΠΎ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΠΌ true if (animatorStateInfo.IsName(animationName)) return true; return false; }
How to use:
if (IsAnimationPlaying("Run")) Debug.Log("Player is running");
To test several animations, you will most likely have to put them in an array and go over the loop:
foreach (var move in attackMoves) { if (IsAnimationPlaying(move.animationName)) { // do smth... } }
PS GetCurrentAnimatorStateInfo - gets information on a specific layer. Therefore, to take the info on the most basic layer is GetCurrentAnimatorStateInfo(0) . In other layers, the index will change naturally.