Unity – Animation Blend Trees

How to create infinitely varied animation with your Unity characters.

A very powerful feature in the Unity Editor is the Blend Tree. This is an extension to an Animation Controller, which allows you to ‘blend’ the joint movements of multiple animations and control the ratio of blending with one or more parameters.

I’ve created a simple example, using a dog character, where I blend together its idle, walk and run animations, controlled by a single parameter that I’ve called scalarSpeed. This will be set by a player controller script to give the character realistic movement, regardless of how fast or slow it moves.

inspector

controllerThe dog character contains a bunch of animations, but we’re currently only interested in the idle, walk and run animations for this blendtreedemo.

I edit the animation controller and set the idle animation as the default state (right-click). I didn’t bother creating transitions codeto the other states here.

Next I add a Blend Tree to the default state. Double-click to edit the Blend Tree; the default 1D (1 dimensional) Blend Tree will suffice here.

Add a parameter, scalarSpeed (which can be set from a game script as the character is moved with the SetFloat Animator method) and let Unity automatically set the thresholds for the animations (these can be tweaked later).

Click the images above to see how it’s all set up.

Blend Tree animation control in Unity can get very sophisticated, with the ability to blend animations based on angular velocity, not just linear velocity and apply root motion to your character from their animations – you can read more about Blend Trees here.