using animation layers with 2d animation

I want to use different animation layers when using spritesheet animation. i want to use them to get away from this state-machine vomit I need, to make everything work:


i want the layers to work in this way:
whenever the animation gets to an animation clip that is empty, it should play the animation from the next layer down. in this case it should play the animation from the layer “Move”.

this is the Move layer:

i have tryed changing the layer weights and blending, but nothing seems to work. whenever the state gets to an empty clip, the player will just show up as the default/preview image of the player.

I managed to achieve what i wanted with this script:

using UnityEngine;

public class ChangeLayerWeight : StateMachineBehaviour {
    public string layerName = "Move";

    // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
        animator.SetLayerWeight(animator.GetLayerIndex(layerName), 1);
    }

    // OnStateExit is called when a transition ends and the state machine finishes evaluating this state
    override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
        animator.SetLayerWeight(animator.GetLayerIndex(layerName), 0);
    }
}

I added this script to all empty states.
the script is very simple; It changes the layer weigth of the Move layer to 0 when it exits the empty state, and to 1 when it enters the empty state. That changes which animation layer can be seen on the player object in the game.

1 Like

with that i can finally organize my state machine into 2 layers. now it looks so much better:



sooooooooo much better than this garbage: