Hi, I’m Nicky! Welcome to iHeartGameDev. A channel about Game Development. This is a video in a series dedicated to learning
Unity’s animation system. In the previous video, we learned to retarget
animations using Unity’s avatar system and humanoid rigs! Today we are going to learn how we can use
the avatar system in combination with Animation Layers to control different parts of our character
body! If you wanna support, like the video, subscribe
and let’s get started! Let’s start with an example. Say we have a character who is able to stand,
walk and run. We want this character to be able to aim a
weapon while in any of the movement states. How can we solve this? When first creating a new animator controller,
we open to the base layer which is at the heart of the animation. If we do not create any other layers, the
base is all that will matter. By adding additional layers, we can create
new animation states that will either completely replace, or add to the base layer’s animations. To enhance this feature even further, we are
able to designate which bones from our new layers we actually want to use. So for our example with the equipping of the
weapon, we can say “ok, let’s keep the base layer’s legs and torso movement but
we’ll add a new layer called Arms. The “arms” layer will perform the arm
movements we are looking for!”. So to sum it up, we are layering new animations
over the original base layer which can override certain bones and perform different animations! Let’s begin today’s demo project by getting
set up. Heading to mixamo.com, we’ll download all
of the free animations we will need for today’s project and a character. To ensure everyone is on the
same page, here are our usual settings -- With Skin being optional after we have at least
one animation that includes the skin. With Skin just means it’s including our
character model. Also, let’s remember to toggle the “in-place”
checkbox for all animations that have that option. We’ll start by selecting the YBot character
and download an idle animation, a walk animation, and a run animation. Next, we’ll download the “pistol idle”
animation where the character isn’t moving, a crouch idle animation, and injured idle,
injured walking, and injured running animations. Some animations of these may not include the
“in place” option, but that’s actually not an issue in today’s lesson so we will
download it anyway. For a full breakdown of these Mixamo settings,
feel free to check out my first tutorial on the channel. Opening Unity, we will import our downloaded
files by dragging them into a project folder. Highlighting our idle animation, we will see
four tabs: model, rig, animation and materials. Let’s press the Rig tab, switch “Animation
Type” to humanoid, and press “Apply”. We will do this for each animation we just
downloaded, except with avatar definition setting, we will choose “copy from other”
avatar and select the avatar we just created for the original animation. Setting the rig type to humanoid is what we
typically do to reuse animations between our bipedal characters. This was thoroughly explained in the previous
video about animation retargeting. However, there are actually many other benefits
to using this system and today we will be exploring one of them known as “animation
layers.” Ok! Now that our character is of type humanoid,
we will start by dragging the model into the scene hierarchy placing it at the world origin. Next let’s create a new animator controller
titled layersDemoAnimator. If our character didn’t automatically add
an animator when we added it to the scene hierarchy, we can do that now and select this
animator controller from the circle selector and the avatar associated with the character. In the animator window, we will create our
first animation state. Duplicating our walking animation, we will
select the “loop time” checkbox in the inspector window so that the animation will
loop. Next we will drag the animation into the animator
grid, which will automatically set this as the default animation state. Pressing play, we will see that our y bot
character will start walking! Awesome, but nothing we haven’t seen before. Let’s get into layers! In the Layer’s tab, we will press the plus
button and rename our new layer to “aiming”. We will immediately notice that the grid has
reset to the original 3 nodes, and the title of the layer is set in a label right above
the grid. So far, this layer does absolutely nothing
because it doesn’t have any animations. Let’s add our pistolIdle animation as the
default animation state for this and press play. It seems like nothing is happening… But we can see our animation state being played. In our list of layers, we will notice a gear
icon for each layer. Pressing the icon on our “aiming” layer
will reveal a submenu with six total settings: Weight, Mask, Blending, Sync, Timing, and
IK Pass. Let’s begin to break these down. Starting from the top, weight determines the
percentage of influence a layer has on our character. That is to say, when the weight is 0, as it
currently is, the layer will have no impact our character’s animation. But as we increase the weight of the layer,
we will see that animation start to take over. When we are at a weight of 1, our character
will only be performing this animation or any animations deeper in the list. Regarding layer priority: the lower the layer
is on the list, the higher the priority the layer’s animations will have over those
above them. Ok cool! Now we know to set our weight to 1 when we
want our aiming animation to play. But this doesn’t solve our issue. As we can see, the entirety of our walking
animation is replaced when we increase the weight of the aiming layer. We only want the arms. This brings us to our next setting in the
gear menu: “avatar mask”. In our project tab, we’ll right click, select
“create” and press on “avatar mask” and rename it to aimingMask. We’ll see this new asset with a familiar
icon. This is the same icon as the avatars that
are added when converting our imported characters to humanoid. We’ll see and open two dropdowns in our
inspector: “Humanoid” and “Transform”. The “Humanoid” dropdown now displays a
green silhouette of a character and a few points reading IK. Here we are seeing the selected bones that
are to be included in the layer! If we press on an area of the body, we can
see the color change from green to red. These bones will be left out of the animations
in the layer. Let’s give this a test by deselecting the
legs. Before we enter play mode, we need to remember
that simply creating the mask isn’t enough. We need to set the mask reference by using
the circle selector in the layer settings for it to actually impact our character. Entering play mode now, we will see that as
we increase the weight, our character starts to aim and continues to walk! Awesome! As for the other settings on the avatar mask,
the IK markers, stand for “inverse kinematics”. As stated by the unity docs, toggling the
markers will either include or exclude the “IK Curves” used in the layer’s animations. To better understand this, let’s think of
a typical IK example. Proper foot placement on uneven ground. When each foot is on the ground, we would
want that foot to rotate and match the ground’s rotation and when it’s not, we want it to
lose that rotation adjustment. In order to know when the foot is on the ground,
we slowly go through the animation and modify the curve so that it’s at a higher value
when the foot is grounded and a lower value when it’s in the air. I plan on exploring IK further in the future,
but for today, we simply need to understand these animation curves are ignored when the
markers in the avatar mask are deselected. The shadow underneath the character is a toggle
for the root of the animation which can drastically change the direction your character is facing. Below the Humanoid settings, we have “Transforms”. Selecting our character avatar and pressing
“Import Skeleton”, the transform list will fill out with the entire bone hierarchy
of our humanoid character. This list allows us to select any extra bones
our character may have such as a tail or wings: allowing us to choose if we want them included
in the layer or not. Humanoid bones are not impacted by the transforms. Those bones are exclusively handled in the
humanoid settings above. Great! Taking this back to the gear menu, we’ve
now covered the weight and mask settings. Next we have Blending. Blending has two options: “Additive” and
our current setting “Override”. “Override” will prioritize the animation
over the layers above it on the list. In our example earlier our walking animation
typically includes animations for both arms. However, because we were using an override
blending setting and our aiming avatar mask had the arms selected, the arm animations
from our aiming layer were prioritized over the base layer. The additive option is a little more complex. In an animation, we store the transform data
for each bone. Additive is intended to combine this data
from all layers that have “additive” selected -- the higher the weight, the more noticeable
it will be. A basic example of additive can be seen here,
where we have three layers: the base layer where the weapon is being held in an idle
position, and two additional layers controlling the vertical and horizontal movement of the
torso and arms. This control is done by using two separate
blend trees and an upper body avatar mask. In the vertical blend tree, we have two separate
animation poses which just point the weapon up and down. And in the horizontal blend tree, the poses
are to the left and right. Testing this implementation, we can see that
changing the parameter values for our aimHorizontal and aimVertical has our character aiming in
a combination of all four directions. This is because we are adding each layer together,
hence the name “additive”. What happens if we switch to override on our
deepest layer? We’ll find that only our horizontal axis
is moving. And if we switch the vertical layer to override
and move it beneath the horizontal layer, now our character will only aim vertically. But again, with additive layers, we are able
to combine multiple layers together up until we reach an override layer. Now, additive layers can be quite tricky. Our example here works properly because our
idle position has the same upper body pose as the four other animations. What happens if the animation in our override
layer doesn’t share the same pose as the additive animations? Switching the current “weapon_idle” animation
in our override layer to a simple walking animation, we’ll see that our walking looks
fine, but the character’s arms now look a little funny. This is due to how the additive system works. Animations have what’s known as an “additive
reference pose”. By default, this is frame 0 of the animation
but it’s adjustable in the fbx animation tab and the debug inspector settings. Additive animations apply the difference in
bone transforms between this reference pose and the current frame. To explain this, let’s look at our example. The reference pose for each animation in our
two blend trees are all set to frame 0 of the weapon_idle animation. Our weapon_idle animation is looking straight
and holding the weapon in-hand. If we look at our four animations included
in our blend trees, they are nearly identical to this weapon_idle animation. The difference is that they each rotate the
torso 90 degrees in their respective directions. Up, Down, Left, and Right. So what these additive layers are doing, is
applying the differences between the reference pose and the current frame to the previous
layers. In this case, we can see the difference is
just the torso rotation. // possibly cut
Here is another example to demonstrate the power of additive animation layers. Take this crouched animation. Looking at the animation, it has over 30 frames. We can actually save some space and increase
performance with additive layers. Deleting all keyframes except for keyframe
0 in the crouched animation, and then copying keyframe 0 from the standing idle animation:
we can paste it inside our crouching animation and set that as the reference pose. Previewing this animation, our character interpolates
between the standing position and our crouched position with just two keyframes! We can then create a new animation layer,
set it the blend type to additive, and add this modified animation as the default animation
state. Vwalah -- Entering play mode and changing
the weight, we can see our character crouching up and down with an animation that’s just
two keyframes! Awesome! Returning to our gear settings menu, we see
a checkbox titled “Sync”. Pressing this toggle, the layout of our grid
is changed and we have a new dropdown menu titled “source layer” that defaults to
our base layer. This dropdown lists all of our other created
layers. The sync toggle copy and pastes the entirety
of the “source layer”’s node setup into the new layer. If we delete a node in either layer, it’s
deleted in all layers that are synced to the same source! So how is this useful? Let’s imagine a simple base layer that includes
three states: idle, walk and run. And in our imaginated game, we have some sort
of health system. We might have some UI that just shows the
current health as a number, which is pretty standard. But we can also show the player that their
health is low by using different animations. Say we have injured versions of the idle,
walk and run. We could create an “injured” layer, activate
the “sync” toggle and select the respective injured animations in each animation state. Now all we would need to do is set the layer
weight based on the normalized value of the player health. Pretty sweet, right? When Activating sync, we also make the “timing”
setting selectable. Unity’s documentation states “the timing
checkbox allows the animator to adjust how long each animation in synced layers takes,
determined by the weight. If timing is unselected then animations on
the synced layers will be adjusted”. But to demonstrate this, we can take our walking
animation and duplicate it -- calling the duplicate “fastWalk”. We will modify the playback of the animation
to play at double the speed by increasing the sample rate to 60 from the current 30. Next, we’ll place our regular walk in the
base layer as the default state, and then create a new layer called FastWalk and turn
on sync with the base layer. We’ll see the same “walking” state but
we’ll set the motion to our “fastwalk”. Entering play mode, our default base layer
plays our walk animation state. By increasing the weight of our fast walk
layer, we’ll see that nothing changes. However, pressing the timing toggle on this
layer, our character starts to play back at the faster speed. So to reword the description from the Unity
docs: “when the timing toggle is active, animations in our sync’d layers will play
normally and at their own rate. When the timing toggle inactive, Unity will
try to fit the keyframes of the sync’d layer into the same sample rate and animation timeline
length as the source layer. The last setting we’ll be covering today
is “IK Pass”. As we learned earlier, IK stands for inverse
kinematics. This toggle will activate and deactivate inverse
kinematics. More specifically it activates and deactivates
the ‘OnAnimatorIK’ function in any script’s attached to the same gameobject as the animator. Here is a basic implementation of IK provided
by the unity docs for the head and the right arm. When we activate IK Pass on our base layer,
we’ll see that our character’s head and arm immediately point in the direction of
our two sphere’s, and as we move the sphere’s around, the arm and the head follow! Awesome! This is just scratching the surface of IK,
a topic we plan to cover in the future. Amazing job today! If you liked what you learned, please consider
pressing the like button and subscribing to the channel! It really helps the channel grow and tells
me that we’re covering content you care about! In the next tutorial, we’ll learn to use
animation events and soon enough we’ll get our character moving using the rigidbody physics
system! If you would like to join our growing community,
I’d be happy to have you as a member of the channel discord! Link is in the description. But that’s all for today, thank you so much
for watching and I’ll see you in the next video!