Playing

Without Scripting

How to play a single animation on a model, without any scripting.

MecanimAnimancer
  1. Create an AnimatorController asset somewhere in your project and open it.
  2. Assign the asset to the Controller field of the Animator component on your model.
  3. Create a state and assign the desired AnimationClip to it.
  4. Enter Play Mode and your animation will play.
  1. Add a NamedAnimancerComponent to your model.
  2. Add the desired AnimationClip to the Animations array of that component.
  3. Enter Play Mode and your animation will play.

With Scripting

How to play an animation when the user clicks the mouse. This is covered in much greater detail by the Quick Play example.

MecanimAnimancer
  1. Write the script (see below).
  2. Attach it to the model and assign the _Animator reference.
  3. Create an AnimatorController asset somewhere in your project and open it.
  4. Assign the asset to the Controller field of the Animator component on your model.
  5. Create an Idle state and assign the AnimationClip to it. It will be the default state because it was created first.
  6. Create an Action state and assign the AnimationClip to it.
  7. Create a transition from Action to Idle and make sure its Exit Time is enabled.
using UnityEngine;

public class PlayOnClick : MonoBehaviour
{
  [SerializeField]
  private Animator _Animator;

  private void Update()
  {
    if (Input.GetMouseButtonDown(0))
    {
      _Animator.CrossFade("Action", 0.25f);
    }
  }
}

It's also important to note that if you tell it to play (or cross fade) multiple animations in the same frame, it will only play the first one and ignore the rest. So if a character jumps (_Animator.Play("Jump")) and then gets hit (_Animator.Play("Flinch")) in the same frame, it will end up playing the jump animation.

  1. Write the script (see below).
  2. Attach it and an AnimancerComponent to the model and assign the references.
using Animancer;
using UnityEngine;

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

  [SerializeField]
  private AnimationClip _Idle;

  [SerializeField]
  private AnimationClip _Action;

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

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

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

Here's what the Mecanim setup process looks like (about 55 seconds long):

And here's what the Animancer setup process looks like (about 25 seconds long):

The overall setup time ends up being fairly similar for both systems. Mecanim spends more time configuring things in the Unity Editor while Animancer uses a longer script to control those same things. The major differences start showing up when you look at the way they affect the development of your project. Consider the following subjects:

Understanding

What do you need to look at to understand how a character works?

Imagine you want to look at a character someone else has set up to figure out how it works or maybe you have just forgotten exactly how a character works because you haven't worked on them in a while. There are plenty of reasons you might want to do this:

  • To add a new action.
  • To track down a bug.
  • Maybe you are just curious.

Where do you need to look to figure out what the character can do?

MecanimAnimancer
  1. Select the character. The Inspector will show which Controller is assigned to the Animator.
  2. Select the Controller and open it in the Animator window.
  3. Select the Idle state. The Inspector will show which animation it is using.
  4. Select the Action state. The Inspector will show which animation it is using.
  5. Select the Action -> Idle transition. The Inspector will show its details.
  1. Select the character. The PlayOnClick Inspector will show which animations it is using.

Where do you need to look to figure out how those animations are being controlled?

MecanimAnimancer
  1. Select the character. The Inspector will show which Controller is assigned to the Animator.
  2. Select the Controller and open it in the Animator window.
  3. Open the PlayOnClick script.
  4. It refers to the "Action" state, so go back to the controller and find the state with that name.
  5. Examine the Action -> Idle transition to figure out what triggers it (Exit Time in this case).
  1. Open the PlayOnClick script.

Reuse

What do you need to do to set up the same behaviour again for another character?

MecanimAnimancer
  1. Duplicate the Animator Controller and assign it to the new character.
  2. Change the animations (if desired).
  3. Add the PlayOnClick script.
  1. Add the PlayOnClick script.
  2. Assign the animations.

That's pretty simple so far, but consider what happens after that.

What if you want to add another action?

MecanimAnimancer
  1. Modify the PlayOnClick script as necessary to trigger the new action.
  2. Find every object with that script attached and open their Animator Controller.
  3. Go through them all and manually create the new state and any transitions it requires.
  1. Modify the PlayOnClick script as necessary to trigger the new action.
  2. Find every object with that script attached and assign the new animation in the Inspector.

What if you just want to rename an action?

MecanimAnimancer
  1. Modify the PlayOnClick script to refer to the new name.
  2. Find every object with that script attached and open their Animator Controller.
  3. Go through them all and manually rename the state in question.
Animancer doesn't normally care about animation names. But let's say you just want to rename the AnimationClip reference field (maybe from _Action to _PrimaryAction so you can add a _SecondaryAction).
  1. Modify the PlayOnClick script.
  2. Add a [FormerlySerializedAs("_Action")] attribute to the field (using UnityEngine.Serialization;).

There's a clear pattern here. The Animator Controller is useless without the script, but looking at it can't tell you which script that is. The Mecanim script is also useless without the controller, but looking at it can't tell you which controller it needs either. The script simply hopes that a state called "Action" actually exists and then gives up responsibility for what happens after the animation is started without giving any indication that it expects the controller to automatically return to Idle afterwards. Every time you want to change anything to do with the Mecanim character, it involves modifying the script and then manually finding every Animator Controller it is used for and making the corresponding modifications there.

Mecanim even allows you to use parameters to control transitions instead of calling CrossFade with the state name directly. But that only makes the problem worse. More things to manually repeat for every Animator Controller. More chances for something to go wrong. More of a disconnect between what the script expects to happen and what will actually happen.

The Animancer script on the other hand dictates exactly how everything works. When you add it to an object you get to see exactly which animations it will use in the Inspector. When it plays an animation it takes responsibility for whatever it expects to happen once it is done. And when you make a modification you do not need to manually go through all the assets it touches to make corresponding modifications.