Animation Blend Weight Selection By Animation Clip
✍ Last Updated : September 7, 2022
🚪 Prequisite Knowledge (Optional)
UploadDescribe InInitial ProgressKnowledge that reader need to know before reading this article
❓ Key Question / Problem / Issue
Find best practice to decide if a played animation clip should affect whole body or blended between upper and lower body
✅ Expected Output/Definition of Done
Define what information you really need and the definition of done
🎁 Resulting Solution
Some animation can be stationary (example: Melee attack) or can be done while moving/mobile (example: Shooting/Ranged attack). Both type (stationary and mobile animation) should be able to be accommodated into one system with minimum work.
Animator Setup : Movement Layer
For a character to be able to move while moving, it has to have at least two Animation Layers. One for legs movement (LowerBody) and one for upper body movement.
The lower body layer is responsible for movement animation (idle/walk/run), which is handled inside the Movement State. The Movement State is a 2D Freeform Directional Blend Tree with 2 parameters, SpeedX and SpeedY, which will determine which animation clips inside the blend tree will be used/blended.
For more explaination about blend tree:
⚠ Note ⚠
To blend movements animation properly, it’s required to have the animations start with the same legs.
Animator Setup : Action Layers
After the lower body layer setup, next is setting up the upper body for handling the action animations (Attack/Hit/Block/etc). The default Animator will only have one layer, and to add another layer, click on ‘+’ sign.
For more information about Animator Layer
To blend between UpperBody animations and LowerBody animation, we need to use AvatarMask to determine which UpperBody animation part will overwrite LowerBody animation. Create AvatarMask and assign the skeleton from the character’s 3D model avatar.
Uncheck bone transforms that will not override the LowerBody animation, then assign the AvatarMask to the UpperBody layer by clicking the UpperBody layer gear icon
Note that as mentioned above, some animation can use full body animation or upper-lower blended animation. The full body animation can be handled by using additional FullBody layer, to save effort on connecting the Animstion States, we can use Layer Sync and select the Source to the UpperBody layer. That way, any modification to the UpperBody or FullBody layer will be applied to both layers.
Then create another AvatarMask with everybone checked and assign to FullBody layer mask
Note that layer Sync option only applies to State Machine structures, the animation clips and the state machine behaviour inside the state stay different and need to be assigned manually.
Selecting Which Layer To Use Using Event
To determine which layer to blend when playing a specific animation, we can use Animation Event. Set Animation Event at the very start of animation (frame 0) and add Layer name to use as parameter.
Example :
sword attack animation event
bow attack animation event
Then create a script to handle the event and attach it to the gameObject. The script will iterate through all animator layers and set any layer with matching name to 1 while the others to 0.
void SetBlendLayer(string layerName)
{
for(int i = 0; i < animator.layerCount; i++)
animator.SetLayerWeight(i, string.Compare(layerName, animator.GetLayerName(i)) == 0 ? 1 : 0);
}
Note that animation layer with Blend Weight == 0 won’t fire any event. So, it’s required to set one of the Action Layer (FullBody or UpperBody) Blend Weight to 1 before playing the animation.
Also to ensure that the SetBlendLayer event will always be called, make sure that the transition start at 0 (transition offset = 0)