Search Unity

How do you usually handle transition animations in a custom state machine?

Discussion in 'Scripting' started by Tiernan98, Mar 11, 2021.

  1. Tiernan98

    Tiernan98

    Joined:
    Jul 11, 2017
    Posts:
    42
    Hi,

    This is more of a discussion than looking for a solution to a problem.

    A lot of times in my games, there are animations that play while transitioning to another state but don't follow the behaviour of either state. For example, a ledge climb up animation. In this case, with how the Unity Character Controller is, there would be a constant downwards velocity in the Locomotion state, but none in the climbing state (because the character would just fall to the ground). If you play the climb up animation and immediately transition to the locomotion state in this case, your character will just fall to the ground.

    What I normally do is have a flag, such as isClimbingUp in the climbing state and each frame will check if Mecanim has gone to the idle animation. If so, then it switches to the locomotion state.

    This feels quite unelegant but it works and doesn't impact performance. Other solutions I imagine would be to have a Transition class that the state machine can handle or a "Wait" state that takes as input a boolean function that checks if it should still wait and a reference to the state it should then transition to. The problem with these though is if you still want to call part of the code of your first state then you can't do that if you're no longer in the state.

    Just curious as to how everyone else was doing this.