This is the old Animancer v7.4 documentation. Animancer v8.0 is now available.

01-03 Basic Action

Location: Assets/Plugins/Animancer/Examples/01 Basics/03 Basic Action

Recommended After: Basic Movement

Learning Outcomes: in this example you will learn:

How to wait for an animation to finish then play something else.

How to use End Events.

How to set an animation's time.

This example demonstrates how to make a character perform an Action (shooting a pistol) when you click the mouse then return to the Idle animation afterwards.

After this example, the Transitions example re-implements the same behaviour with smooth blending between the animations (i.e. Cross Fading).

Summary

  • Animancer's Play methods return an AnimancerState which you can use to control the animation.
  • You can restart an animation by setting its Time to 0.
  • You can use End Events to easily do things when an animation ends (such as playing a different animation).
  • The Holding a Pistol page explains how to actually put the pistol model in the character's hand.
AnimancerState state = _Animancer.Play(_Action);
state.Time = 0;
state.Events.OnEnd = OnAnimationEnd;

Overview

The code structure is the same as to the Basic Movement example, just with a different script and action animation instead of movement.

Action Animation

As mentioned in the foldout above, the fields and OnEnable method are basically the same as in previous examples so we won't go over them again.

This time we want to know when the player clicks the mouse, so we make an Update method to play the _Action animation when that happens:

private void Update()
{
    if (ExampleInput.LeftMouseUp)// Was the left mouse button released this frame?
    {
        _Animancer.Play(_Action);
    }
}

The full script now looks like this:

using Animancer;
using UnityEngine;

public class PlayAnimationOnClick : MonoBehaviour
{
    [SerializeField] private AnimancerComponent _Animancer;
    [SerializeField] private AnimationClip _Idle;
    [SerializeField] private AnimationClip _Action;
    
    private void OnEnable()
    {
        _Animancer.Play(_Idle);
    }

    private void Update()
    {
        if (ExampleInput.LeftMouseUp)
        {
            _Animancer.Play(_Action);
        }
    }
}

If you save that script and return to Unity, you can attach it to your character and set up its references in the Inspector:

Then if you enter Play Mode, you can Left Click to play the Action animation:

Remember to exit Play Mode after you are done looking.

End Event

As you can see above, the animation plays correctly but afterwards the character freezes in place which is usually not what you want to happen. In this case when the _Action animation ends we want the character to go back to the _Idle animation, which is really easy to do in Animancer.

First, we grab the AnimancerState returned by the Play method:

AnimancerState state = _Animancer.Play(_Action);

That state allows you to get and set all the playback details of the animation such as its Speed and Time.

In this case, we just want to give it an End Event:

state.Events.OnEnd = OnEnable;

In most cases you would make a new method for the End Event to call, but in this example the OnEnable method already does exactly what we want so we can just use it.

The full script now looks like this:

using Animancer;
using UnityEngine;

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

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

    private void Update()
    {
        if (ExampleInput.LeftMouseUp)
        {
            AnimancerState state = _Animancer.Play(_Action);
            state.Events.OnEnd = OnEnable;
        }
    }
}

If you save that script and take another look at it in Play Mode, now the character will return to Idle after performing the Action.

Remember to exit Play Mode after you are done looking.

There are a couple of different ways we could have done that which are listed on the AnimancerEvent.Sequence.OnEnd page. The End Events page also explains some alternative approaches instead of using the OnEnd callback.

Rapid Fire

So far the character has had to wait for the Action animation to finish before they can act again because Animancer doesn't automatically restart the animation when you call Play. This behaviour was useful in the Basic Movement example, but there are many cases where you might want to force the animation back to the beginning which can be done by simply setting its Time to 0.

AnimancerState state = _Animancer.Play(_Action);
state.Time = 0;
state.Events.OnEnd = OnEnable;

The full script now looks like this:

using Animancer;
using UnityEngine;

public sealed class PlayAnimationOnClick : MonoBehaviour
{
    [SerializeField] private AnimancerComponent _Animancer;
    [SerializeField] private AnimationClip _Idle;
    [SerializeField] private AnimationClip _Action;

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

    private void Update()
    {
        if (ExampleInput.LeftMouseUp)
        {
            AnimancerState state = _Animancer.Play(_Action);
            state.Time = 0;
            state.Events.OnEnd = OnEnable;
        }
    }
}

What Next?

Example Topic
Transitions Implementing the same behaviour as this example with smooth blending instead of instantly snappping between animations.
Basic Character Combining this example and Transitions with Basic Movement.