Directional Animation Sets

Directional Animation Sets are a convenient way of managing groups of up/right/down/left animations (including or excluding diagonals) with a standardised way of selecting which one you want.

Overview

1. Create an animation set 2. Use it in code

Select the animations you want, Right Click, and use Create/Animancer/Directional Animation Set/From Selection.

using Animancer;
using UnityEngine;

public class DirectionalAnimationSetExample : MonoBehaviour
{
    [SerializeField]
    private AnimancerComponent _Animancer;

    [SerializeField]
    private DirectionalAnimationSet4 _Idles;
    
    protected virtual void Update()
    {
        Vector2 direction = new Vector2(
            Input.GetAxisRaw("Horizontal"),
            Input.GetAxisRaw("Vertical"));

        AnimationClip clip = _Idles.Get(direction);
        _Animancer.Play(clip);
    }
}
  1. Assign the set in the Inspector:

The Directional Sprites samples demonstrate how to use them in more detail.

Creation

Directional Animation Sets can be created by Right Clicking in the Project window and using the menu functions in Create/Animancer/Directional Animation Set/...:

Function Effect
Directions Creates an empty DirectionalAnimationSet2 (left and right).
Directions Creates an empty DirectionalAnimationSet4 (4 directions, no diagonals).
Directions Creates an empty DirectionalAnimationSet8 (8 directions, including diagonals).
From Selection Creates a set to reference the animations you have selected, choosing an appropriate set type based on those animations. This requires the animation names to be identical except for the words "Up", "Right", "Down", and "Left".

Once you have created a set, you can assign animations to each of its fields manually or use its Find Animations context menu function to automatically assign animations with the same name (varying only by direction) in the same folder. For example, the video below shows a set called Mage-Idle which finds the animations called Mage-IdleUp, Mage-IdleRight, Mage-IdleDown, and Mage-IdleLeft because those animations have the same base name as the set:

Also note that Directional Animation Sets are not limited to only Sprite animations. For example, the Directional Mixers sample originally used a set to reference its movement animations before Mixer Transitions were implemented.

Usage

Using Directional Animation Sets in your scripts is very easy:

  1. Declare a Serialized Field with the type of set you want:
[SerializeField] private DirectionalAnimationSet4 _Idles;

That gives you an Inspector field where you can assign any DirectionalAnimationSet4 asset (or DirectionalAnimationSet8 since it Inherits from DirectionalAnimationSet4).

  1. Determine the direction you want from the set:
// Input Vector:
Vector2 direction = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));

// Or Enum (4 Directions):
Direction4 direction = Direction4.Right;

// Or Enum (8 Directions):
Direction8 direction = Direction8.UpRight;
  1. Get the set's AnimationClip corresponding to that direction and Play it normally:
// Assuming you also have a reference to an AnimancerComponent somewhere:
// [SerializeField] private AnimancerComponent _Animancer;

AnimationClip clip = _Idles.Get(direction);
_Animancer.Play(clip);

The Directional Sprites samples demonstrate how to use them in more detail.

Inheritance

All Directional Animation Sets inherit from DirectionalSet<AnimationClip> so if you want to write code that can work with any of the set types, you can use that as the field type instead:

[SerializeField] private DirectionalSet<AnimationClip> _Idles;

The Directional Sprites samples use this to demonstrate how you can use the same code to work with both DirectionalAnimationSet4 and DirectionalAnimationSet8 assets.

If you have other sets of objects that you want to use in a similar way, you can create your own set types by inheriting from DirectionalSet<T>.

Snapping

The Snap method in each Directional Animation Set type takes a given Vector and returns a copy of it pointing in the closest direction that set type has an animation for (2, 4, or 8 directions depending on which type of set you are using). This is useful for clamping an input vector to the exact direction the animation is facing if you want to limit the character to only move in exactly those directions.

How it works

The DirectionalAnimationSet4 class (and the other set types) contains quite a bit of functionality, but at its core it is just a ScriptableObject with a few AnimationClips and a method to choose whichever is closest to a given direction Vector:

public class DirectionalAnimationSet4 : ScriptableObject
{
    [SerializeField] private AnimationClip _Up;
    [SerializeField] private AnimationClip _Right;
    [SerializeField] private AnimationClip _Down;
    [SerializeField] private AnimationClip _Left;

    public virtual AnimationClip Get(Vector2 direction)
    {
        if (direction.x >= 0)
        {
            if (direction.y >= 0)
                return direction.x > direction.y ? _Right : _Up;
            else
                return direction.x > -direction.y ? _Right : _Down;
        }
        else
        {
            if (direction.y >= 0)
                return direction.x < -direction.y ? _Left : _Up;
            else
                return direction.x < direction.y ? _Left : _Down;
        }
    }
}

DirectionalAnimationSet8 Inherits from DirectionalAnimationSet4 so it has the same fields as well as another 4 for the diagonals and it overrides the Get method to choose from all 8 of its animations.

All of these scripts are included in Animancer Lite so you can read and modify them if you want to.