Search Unity

Mecanim - Detecting end of state *before* transitions are evaluated

Discussion in 'Animation' started by Newcomma, Jul 14, 2016.

  1. Newcomma

    Newcomma

    Joined:
    Feb 10, 2015
    Posts:
    89
    Hi There,

    I want some code to run at the end of the state (when normalized time hits 1/ the anim clip finishes), but I want this code to run *before* any state transitions are evaluated in conjunction with Has Exit Time.

    The reason for this is that I want to be able to set some mecanim bools when my state ends, which the transitions will then use to determine the next state to go to. If the transitions are evaluated *before* my code runs it will go to the incorrect state next.

    If I do a check within State.Update I'll find that before normalizedTime >= 1, the transitions have already evaluated and fired, so I'm guessing internally the transitions evaluate earlier then update control is called

    My current understanding of Update logic is something along the lines of:

    Code (CSharp):
    1. // At this point normalized time is <= 1
    2. OnStateUpdate()
    3.  
    4. // Guess work
    5. UpdateNormalizedTime()
    6. Unity.EvaluateTransitions()
    7.  
    8. // At this point normalizedTime == 1f but the tranistions have already fired
    9. OnStateMove()
    So, does anyone know of a good way to run code at the completion of a state (i.e. when normalizedTime is equal to 1) but before the state transitions are evaluated? And ideally without introducing frames of delay.

    Thanks in advance
     
    Last edited: Jul 14, 2016
  2. Newcomma

    Newcomma

    Joined:
    Feb 10, 2015
    Posts:
    89
  3. DavidGeoffroy

    DavidGeoffroy

    Unity Technologies

    Joined:
    Sep 9, 2014
    Posts:
    542
    The only way to do what you want in a stable way is to create a SubStateMachine.

    It would look like this:

    entry --> YourState --> exit

    The transition from YourState to Exit should be Exit Time =1, and no condition
    Then, you add a StateMachineBehaviour on the sub state machine.

    In there, in OnStateMachineExit, you change whatever parameters you need to drive your conditions for your actual transitions

    and then you add transitions from the state machine to wherever you want to go, with the proper conditions.

    OnStateMachineExit is evaluated after the first transition to exit is taken, but before the transitions from the state machine are evaluated.