Animancer v6.0 is currently available for testing.

01 Basic Movement

Difficulty: Beginner - Recommended after Quick Play

Location: Assets/Plugins/Animancer/Examples/04 Directional Sprites/01 Basic Movement

Namespace: Animancer.Examples.DirectionalSprites

This example demonstrates how you can use Directional Animation Sets to organise animations in groups of up/right/down/left, using the exact same structure for both Idle and Walk animations across various different characters. The Character Controller example extends this concept into a more complex character and shows an alternate character that uses DirectionalAnimationSet8s to allow diagonal movement.

16x16 Mage created by saint11 - CC0 Licence.

The SpriteMovementController script looks like this (with the comments removed since we're about to explain how it works):

using Animancer;
using UnityEngine;

public sealed class SpriteMovementController : MonoBehaviour
{
    [SerializeField] private AnimancerComponent _Animancer;
    [SerializeField] private DirectionalAnimationSet _Idles;
    [SerializeField] private DirectionalAnimationSet _Walks;
    [SerializeField] private Vector2 _Facing = Vector2.down;

    private void Awake()
    {
        Play(_Idles);
    }

    private void Update()
    {
        var input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
        if (input != Vector2.zero)
        {
            _Facing = input;

            Play(_Walks);

            var isRunning = Input.GetButton("Fire3");
            _Animancer.States.Current.Speed = isRunning ? 2 : 1;
        }
        else
        {
            Play(_Idles);
        }
    }

    private void Play(DirectionalAnimationSet animations)
    {
        var clip = animations.GetClip(_Facing);
        _Animancer.Play(clip);
    }

#if UNITY_EDITOR
    private void OnValidate()
    {
        if (_Idles == null)
            return;

        AnimancerUtilities.EditModePlay(_Animancer, _Idles.GetClip(_Facing), true);
    }
#endif
}

The general structure is very similar to previous examples, except that instead of referencing a single AnimationClip _Idle and _Walk, we are using Directional Animation Sets.

Fields

Where the other examples directly reference individual AnimationClips or use Transitions, this time we are using Directional Animation Sets which are ScriptableObject assets that contain references to multiple AnimationClips.

We also need to store the direction the character is facing when they move so that when they stop they can still play the correct Idle animation to continue facing in that direction. Having this as a Serialized Field allows us to determine which direction the character will be facing on startup as well as in Edit Mode.

SpriteMovementController Script DirectionalAnimationSet Asset
[SerializeField]
private AnimancerComponent _Animancer;

[SerializeField]
private DirectionalAnimationSet _Idles;

[SerializeField]
private DirectionalAnimationSet _Walks;

[SerializeField]
private Vector2 _Facing = Vector2.down;

If you have Inspector Gadgets Pro, you can use its Nested Object Drawers feature (by clicking on the foldout arrows) to see and modify the details of the referenced asset without needing to actually go and select that asset:

Playing

Since we know which direction we are _Facing, we can have a method that takes any DirectionalAnimationSet, gets the animation for that direction from it, and plays it like any other animation:

private void Play(DirectionalAnimationSet animations)
{
    var clip = animations.GetClip(_Facing);
    _Animancer.Play(clip);
}

That allows us to easily play the appropriate animation from the _Idles set on startup:

private void Awake()
{
    Play(_Idles);
}

Controls

This example controls the character movement using the WASD and Arrow Keys by default, but you can edit your input axes via Edit/Project Settings/Input Manager. We start by creating a direction Vector from the input axes named "Horizontal" and "Vertical":

private void Update()
{
    var input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));

If the player is trying to move, we store that input as the direction they want to be _Facing:

    if (input != Vector2.zero)
    {
        _Facing = input;

Then we can pass the _Walks animation set into the Play method so that it will play the appropriate animation for that direction:

        Play(_Walks);

Since this example uses simple Sprite animations, we can easily allow the character to run by increasing the animation speed when the player holds the "Fire3" key (Left Shift by default, but you can change it in the Input Manager). We could have had our Play method return the AnimancerState it gets from _Animancer.Play, but we can also just access it using _Animancer.States.Current:

        var isRunning = Input.GetButton("Fire3");
        _Animancer.States.Current.Speed = isRunning ? 2 : 1;
    }

Note that changing the AnimancerState.Speed is a Pro-Only Feature. Animancer Lite allows you to try it out in the Unity Editor, but it will do nothing in runtime builds unless you purchase Animancer Pro.

Finally, we can simply return to one of the _Idles when the player stops trying to move. Note that we are not setting the _Facing direction in this case, so it will still be facing in the last direction it moved:

    else
    {
        Play(_Idles);
    }
}

This example does not actually move the character around since is just demonstrating the basics of Directional Animation Sets, but the Character Controller example does demonstrate movement.

Edit Mode

If we were to make another character with some different DirectionalAnimationSets, it would use those animations at runtime but it would not actually show the correct sprite until then. Fortunately, Animancer allows you to easily play animations in Edit Mode. All we need to do it use MonoBehaviour.OnValidate to call AnimancerUtilities.EditModePlay:

#if UNITY_EDITOR
private void OnValidate()
{
    if (_Idles == null)
        return;

    AnimancerUtilities.EditModePlay(_Animancer, _Idles.GetClip(_Facing));
}
#endif

The #if UNITY_EDITOR isn't actually necessary because Unity will not call OnValidate at runtime anyway, but this allows us to be clearer about what the code is doing.

Also note that this did not work properly before Unity 2018.3 due to a bug.

More Characters

Now we can create more DirectionalAnimationSets and reuse the same script for more characters.

16x16 Animated Critters created by patvanmackelberg - CC0 Licence.

Antifarea's RPG sprite set 1 created by Antifarea - CCBY Licence (not included in Animancer due to Asset Store licensing restrictions).

Sub-Assets

Since the there are lots of animations in this example and they are each only used by a single DirectionalAnimationSet, the Drag and Drop Sub-Assets feature of Inspector Gadgets Pro was used to turn the animations into sub-assets of the set in order to organise them a bit better.