Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

State machine behavior, OnStateEnter and OnStateExit problem?

Discussion in 'Scripting' started by neno1204, Jul 11, 2015.

  1. neno1204

    neno1204

    Joined:
    Jun 23, 2015
    Posts:
    14
    Hello, I have two animations with transition between them. The OnStateEnter of the second animation calls before OnStateExit of the first animation but I need events of the first animation to be called before events of the second animation. I have noticed if I remove transition between animations then it works A1 enter -> A1 exit -> A2 enter -> A2 exit.
    How do I solve this with having transition between animations but still have right order of state events?

    Thank You

    Screen Shot 2015-07-11 at 11.45.27 AM.png Screen Shot 2015-07-11 at 11.46.18 AM.png
     
  2. Rico21745

    Rico21745

    Joined:
    Apr 25, 2012
    Posts:
    409
    Replying to let folks know I have also experienced this issue.
     
  3. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    9,682
    I think the order is correct. The transition crossfades from A1 to A2. So A2 starts fading in while A1 is fading out.

    What about this: Only use OnStateEnter. In A1.OnStateEnter, record that you're in A1. On A2.OnStateEnter, look up the recorded previous state and call a finish-up method on it.
     
  4. Rico21745

    Rico21745

    Joined:
    Apr 25, 2012
    Posts:
    409
    I think part of the problem is that while we understand that in theory the code is behaving as it should (crossfading) the whole point of using StateMachineBehaviors was to get away from that kind of logic where you program against hardcoded state machine strings thus making them reusable :)

    If we took that approach we'd have to add new code to the behavior each time you decided to throw it onto a new State on your State Machine. IMHO not really a very useful solution and may as well go back to the more reliable method of hardcoding string states in a normal monobehavior attached to the Animator
     
    Katalla likes this.
  5. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    9,682
    The logic is different, but it's just as generic. The logic handles the concept that a state machine can be in two states at the same time while it's transitioning. How you handle that logic at a higher level is of course up to you, but there's no reason at all that anything needs to be hardcoded.