Search Unity

Reliable Means of detecting animation state % complete

Discussion in 'Scripting' started by Lypheus, Nov 22, 2012.

  1. Lypheus

    Lypheus

    Joined:
    Apr 16, 2010
    Posts:
    664
    I'm trying to find a means of reliably (and cleanly) determining when an animation state has played through to the next transition. I see a few approaches atm, none of which are well designed or reliable.

    1. Create a curve, use it as a pseudo-bool that goes from < 1 to >= 1, detect this in code and treat that as 'done'.

    Pro: Can "tweak" when you'd expect to be done for each animation.
    Con: A hack, this is creating superflous state and bleeding that information to the client, to tell me something I should already know.

    2. Poll for a transition state or state change.

    Pro: Fairly simple to do.
    Con: Lots - "polling" for state information in a game loop? lack of encapsulation - i.e. "knowing" about a state by name that i'm in.

    3. Compare current play progress vs "total length" of that state.

    This is what i'm attempting now, it seems to work but i'm not fond of having to 'know' about the layer/animation names here. Has anyone found a cleaner means of determining the percentage of a state, kind of like when you watch the Animator window and you see the blue progress bar filling up - i'd like to know THAT value so I'm able to ignore (for some cases) layers, clips, etc... just want to know the general % done for a given state.

    Code (csharp):
    1.  
    2.         // based on animation state info, determine if we are done the 'vault' animation
    3.         AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo( 0 );
    4.         float normalizedTime = stateInfo.normalizedTime - Mathf.Floor( stateInfo.normalizedTime );
    5.         if( stateInfo.IsName( "MainLayer.Vault" )  normalizedTime >= stateInfo.length )
    6.         {
    7.             End( Inputs.Type.Default );
    8.         }
    9.  
    I'm thinking there must be a more reliable way of knowing when a state has played it's course and is ready for a transition.
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    I assume youre looking for something more complex than...

    animation["anim"].time/animation["anim"].length


    but im going to post it anyway since its a percentage of animation played :p
     
  3. Lypheus

    Lypheus

    Joined:
    Apr 16, 2010
    Posts:
    664
    Well, I'm thinking we want the same kind of calculation but at the animation state level so as to account for blending, speed, etc... You'd think normalized time would do this, perhaps it does and I am missing something here.
     
  4. Joey_Zhang

    Joey_Zhang

    Joined:
    Apr 17, 2009
    Posts:
    14
    Hi! This is exactly what I want. Do you find the more reliable way? Thanks!