Unity Basics: Animator — Part 3

Animation Layer

Karo
Supercent Blog 슈퍼센트 블로그
6 min readMay 20, 2024

--

What is an Animator?

Animator is the core component of one of the animation systems we provide, a system called Mecanim.
In Part 2, we learned how to edit the Animator Controller, and in Part 3, we’ll take a look at the Animation Layer used by the Animator Controller.

What is an Animation Layer?

Animation Layers are a feature that allows you to independently control different parts of your character. You can use them to animate only certain parts of your character.

For example, a character’s body could be dancing and have different facial expression animations depending on the situation, or the lower body could be running while the upper body is shooting a gun.

List of animation layers in the Animator view

How to Use Animation Layers?

The Animation Layer can be found by clicking on the tab in the top left corner of the Animator view. When you create an Animator Controller, it will have a Base Layer by default, and you can add new layers via the “+ button” or change the order of the layers in the following ways.

Example of adding or reordering animation layers

The added layer can define States and set Transitions just like the existing Base Layer, and will behave as an independent State Machine. In the case of Parameters, the same applies to all Layers.

Example of adding states and transitions to an added animation layer and using shared parameters

How Do Animation Layers Work?

You can edit how an animation layer should behave by clicking the “gear icon” in the top right corner of the animation layer you want to edit the behavior of.

Animation Layer Settings Screen
  • Weight
    The weight that this layer contributes. A corresponding value of 0 means 0%, and a value of 1 means 100%.
void AnimatorTest()
{
// The order of the layers is top to bottom, starting at number 0.
var baseLayerIndex = 0;
var currentWeight = anim.GetLayerWeight(baseLayerIndex);

// This method allows you to adjust the weight of a specific layer.
anim.SetLayerWeight(baseLayerIndex, 1.0f);
}
  • Mask
    The part of the character that this layer’s animations will be applied to. This setting allows you to animate only certain parts of the body, such as the upper or lower torso. If no mask is set, all parts will be animated. We will cover this in more detail below.
  • Blending
    This refers to how that layer is applied, and there are two ways to do it.
    “Override” completely replaces the animation applied by layers below it.
    “Additive” adds the layer’s animation on top of the animation applied by the layer below it.
  • Sync
    This option allows you to have the exact same state and transitions as a specific layer. We’ll cover this in more detail below.
  • Timing
    This is an option that you can select when Sync is enabled to control how long the overridden animation will play. We’ll cover this in more detail below.
  • IK Pass
    This option determines whether you want to utilize Animator’s IK functionality. We’ll cover IK in the next post.

If you’re interested in learning more about Animation Layers, click here.

What is an Avatar Mask?

This feature is used when you want to animate only certain parts of the character. The mask allows you to select parts of the character’s body, while the rest of the body is excluded from animation. Both “Humanoid” and “Generic” types of modeling can be used, and the setup is different for each type.

Edit Avatar Mask screen

You can create an Avatar Mask in the following ways.

Example of creating an avatar mask

How to edit in Humanoid

For humanoids, masking areas can be set by toggling the desired areas in the human model at the top to enable/disable them. By default, they are all colored green and can be enabled/disabled in bulk by clicking on an empty area.

Example of editing an avatar mask (Humanoid)

How to edit in Generic

For generics, you can open “Transforms” at the bottom to bring up the skeleton structure you want to edit and enable/disable parts via checkboxes. By default, they are all checked and the buttons at the bottom provide convenience features such as batching and unbatching.

Example of editing an avatar mask (Generic)

If you’re interested in learning more about Avatar Masks, click here.

How to utilize the Sync option?

This option allows you to have the exact same state and transitions as a specific layer. You can utilize this option to override only the animation clips on the target layer. Note that if the State or Transition of the synchronized layer and the target layer are modified, both layers will change. You can only override animation clips.

For example, you can utilize it in the following cases.

  • When a character is walking or running, you want to set a facial expression animation for that state.
  • You want to override the respective animations for when the character is healthy and when they are injured.
  • You want to override the upper body animation based on the weapon the character is holding.
Snyc application examples

For the “Timing” option, as mentioned above, it controls how long the overridden animation will play. The duration of the animation will be automatically adjusted regardless of whether the option is enabled or disabled, and will balance between the two animations based on weight if it is enabled, or prioritize the animation on the original layer if it is disabled.

Example of using the Timing option

Example

So far, we’ve covered all of the basics needed to use Animation Layers, and now we’re going to utilize those features to create the following examples.

  • The character moves when the left mouse button is clicked.
  • Clicking the right mouse button will injure the character.
  • If a character is injured, their motion is overridden.
public class SampleController : MonoBehaviour
{
const float CHANGE_STATE_TIME = 0.25f;
const int INJURED_LAYER_INDEX = 1;

static readonly int ANIM_MOVE = Animator.StringToHash("move");
static readonly int ANIM_HIT = Animator.StringToHash("hit");



[SerializeField] Animator _anim = null;

bool _isInjured = false;



void Update()
{
if (null == _anim)
return;
_anim.SetBool(ANIM_MOVE, Input.GetMouseButton(0));
if (_isInjured)
return;
if (!Input.GetMouseButtonDown(1))
return;
_isInjured = true;
_anim.SetTrigger(ANIM_HIT);
StartCoroutine(Co_ChangeToInjured());
}

IEnumerator Co_ChangeToInjured()
{
var elapsed = 0.0f;
while (elapsed < CHANGE_STATE_TIME)
{
yield return null;
elapsed += Time.deltaTime;
var weight = elapsed / CHANGE_STATE_TIME;
_anim.SetLayerWeight(INJURED_LAYER_INDEX, weight);
}
_anim.SetLayerWeight(INJURED_LAYER_INDEX, 1.0f);
}
}

Conclusion

So far, you’ve learned what the Animation Layer is and how you can use it to your advantage.

In the next post, we will cover Humanoid and Inverse Kinematic (IK).

Hopefully, this post has been helpful not only for those who are new to Animator but also for those who are already familiar with it.

<Related Posts>

Want more hyper-casual game development tips?
Click here to become a Supercent partner today!

[Contact]
- E-mail: help@supercent.io
- Homepage:
corp.supercent.io

--

--