Search Unity

Question GetCurrentAnimatorClipInfo when in transition- can't retrieve the proper state

Discussion in 'Animation' started by EvilKris, Jan 30, 2023.

  1. EvilKris

    EvilKris

    Joined:
    Nov 18, 2014
    Posts:
    27
    I want to get the state that the animation is going into, not the one it's transitioning from. If I wait a few frames to post-transition then try to retrieve the state I can get the result I want but I want to get it at the very start.

    According to the documentation GetNextAnimatorClipInfo should be able to help with that but it doesn't work at all on the first frame at all and gives me an OutOfIndex error.

    Anybody?

    Thanks
     
  2. Kreshi

    Kreshi

    Joined:
    Jan 12, 2015
    Posts:
    446
    Use GetNextAnimatorStateInfo
     
  3. EvilKris

    EvilKris

    Joined:
    Nov 18, 2014
    Posts:
    27
    Sorry but that seems to produce the same results
     
  4. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    Calling Play, CrossFade, SetTrigger, etc. won't actually start a transition or go to the next state until the next frame.

    SetTrigger and other parameter based transitions could take even longer than that to start the transition if there are other conditions preventing it.

    Those delays are just things you have to deal with if you use Animator Controllers.
     
  5. Kreshi

    Kreshi

    Joined:
    Jan 12, 2015
    Posts:
    446
    This is how I use AnimatorStateInfo to check if I am in a given animation or not:

    Code (CSharp):
    1. var nextAnimState = m_animator.GetNextAnimatorStateInfo(entry.Key);
    2. if (nextAnimState.shortNameHash == entry.Value && nextAnimState.normalizedTime > 0) return true;
    3.  
    4. bool hasStartedTransitionToAnotherState = nextAnimState.shortNameHash != entry.Value && nextAnimState.normalizedTime > 0;
    5. if (hasStartedTransitionToAnotherState == false && m_animator.GetCurrentAnimatorStateInfo(entry.Key).shortNameHash == entry.Value) return true;
    entry is a KeyValue<int, int> which holds the animation layer and the animation short name.

    About timings: I use the functionality inside of OnAnimatorIK but I guess using it in LateUpdate should be fine too.
    For me this solution was absolutely sufficient for my use-cases.