Search Unity

Question When/How does GetNextAnimatorStateInfo evaluate?

Discussion in 'Animation' started by jw5019, Oct 23, 2021.

  1. jw5019

    jw5019

    Joined:
    Dec 24, 2020
    Posts:
    6
    Hi, I have an enemy with a combo system which goes through a chain of animations. I want to make sure I know when the start/end of the animation is which works perfectly fine with the code below. I then need to make it so that if combo continues I have this script on every animation state with an attack on it. Presumably, the result is it enters the first attack in the chain, evaluates the next state name to see if the tag is "attack" and then by the end decides if should still continue the attack logic. The weird thing is, even if I make a simple node like attack 1 -> attack 2 with no exit transition on attack 1 and both states have the script attached, it still reports that the 'next state' isnt tagged with "attack", when it is. How can it not be if there is no alternative? Is there something I'm missing with how NextState works? Are the variables shared between all instances of the class because the base is a scriptable object? A bit confused to be honest.

    Code (CSharp):
    1. public class EnemyAttackCheck : StateMachineBehaviour
    2. {
    3.     private EnemyCombat _enemyCombat;
    4.     private bool _attackChain;
    5.  
    6.     public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    7.     {
    8.         _enemyCombat = animator.gameObject.GetComponent<EnemyCombat>();
    9.         _enemyCombat.InAttack(true);
    10.  
    11.         if (animator.GetNextAnimatorStateInfo(0).IsTag("attack"))
    12.         {
    13.             _attackChain = true;
    14.         }
    15.         else
    16.         {
    17.             _attackChain = false;
    18.         }
    19.        
    20.     }
    21.  
    22.     public override void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    23.     {                
    24.         if (_attackChain == false)
    25.         {
    26.             Debug.Log("exit attack chain");
    27.             _enemyCombat.InAttack(false);
    28.         }      
    29.     }  
    30. }
     
  2. jw5019

    jw5019

    Joined:
    Dec 24, 2020
    Posts:
    6
    Update: I solved this by just by separating it into 2 simple scripts so there isn't even an need for comparing which I probably shouldve just done in the first place... but if anyone sees this I would still like to learn more about this class and why my approach didnt work. Thanks
     
  3. ModelOX

    ModelOX

    Joined:
    Jan 18, 2016
    Posts:
    11
    Since this is one of the first things that pops up when looking for info about this ill redirect you to this answers post about the same thing: https://answers.unity.com/questions/1040119/getnextanimatorstateinfotaghash-is-always-0.html

    The jist of it is that GetNextAnimatorStateInfo only evaluates properly while a transition is happening, so you can't use it in StateEnter/Exit since the transition is already over by that point.

    Further more if you have a transition duration of 0 it also won't return anything, since I guess there's no transition happening? Very frustrating to try and use when needing instant transitions as opposed to blending between animations...
     
    unity_TEkEHKcvu5sRAw likes this.