03 Transitions

Difficulty: Beginner

Location: Assets/Plugins/Animancer/Examples/01 Basics/03 Transitions

Namespace: Animancer.Examples.Basics

This example implements exactly the same behaviour as the Quick Play example, but instead of hard-coding the animation details it uses Transitions so that they can be configured in the Inspector.

Code

There are various different Transition Types, but the most common one which we want to use here is ClipTransition to replace the direct references to AnimationClips that were used in the Quick Play example.

This leads to a few differences between the Quick Play script and the one for this example:

Quick Play Using Transitions

Both scripts start the same.

using Animancer;
using UnityEngine;

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

Just with a different class name.

using Animancer;
using UnityEngine;

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

Uses direct references to AnimationClips.

    [SerializeField]
    private AnimationClip _Idle;

    [SerializeField]
    private AnimationClip _Action;

Uses ClipTransitions instead.

    [SerializeField]
    private ClipTransition _Idle;

    [SerializeField]
    private ClipTransition _Action;

Events are Cleared Automatically whenever a new animation is played so we assign the End Event right after calling Play.

    private void OnEnable()
    {
        _Animancer.Play(_Idle);
    }

    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            var state =
                _Animancer.Play(_Action);
            state.Events.OnEnd = OnActionEnd;
        }
    }

Events still get removed from the state when something new is played, but they are stored in the transition instead of being cleared so we only need to assign the End Event once on startup.

    private void OnEnable()
    {
        _Action.Events.OnEnd = OnActionEnd;
        _Animancer.Play(_Idle);
    }

    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            _Animancer.Play(_Action);
        }
    }

Also, the Fade Duration of the _Idle transition will automatically be ignored when we first play it because there is no previous animation to Fade in from.

An AnimationClip has no inherent fade duration so we needed to hard-code it.

    private void OnActionEnd()
    {
        _Animancer.Play(_Idle, 0.25f);
    }
}

Transitions allow you to set their Fade Duration in the Inspector.

    private void OnActionEnd()
    {
        _Animancer.Play(_Idle);
    }
}

Inspector

The Inspector for a ClipTransition looks similar to an AnimationClip, but it has two key differences:

  1. The foldout arrow on the left allows you to see its other details. Each field has a tooltip and the Transitions page explains them in more detail.
  2. The button on the right opens the Transition Preview Window so you can see what the transition will actually look like while adjusting its details.

The default Fade Duration of 0.25s matches what we used in the Quick Play example and we aren't going to modify any of the other parameters here, but the Golf Events example builds upon this one to show how you can make Animancer trigger a function at a specific time during an animation to hit a ball and also how to adjust the transition back to Idle to look more natural.