Blend trees help smoothly mix different animations based on values like speed or direction. This makes character movements look natural and fluid.
0
0
Blend trees in Unity
Introduction
When you want a character to walk and run smoothly without sudden jumps between animations.
To blend animations based on player input, like moving forward and turning at the same time.
When creating complex animation states that depend on multiple factors, such as speed and direction.
To avoid harsh animation changes and improve the feel of character control.
When you want to combine multiple animations into one smooth transition automatically.
Syntax
Unity
1. Create a Blend Tree in the Animator window. 2. Add animations as child motions. 3. Set parameters (like float speed) to control blending. 4. Adjust thresholds to define when each animation blends.
Blend trees use parameters you define in the Animator Controller to decide how to mix animations.
You can create 1D or 2D blend trees depending on how many parameters you want to blend.
Examples
This blend tree smoothly changes from idle to walk to run as the speed parameter changes from 0 to 1.
Unity
// Example: 1D Blend Tree controlled by speed parameter Blend Tree with motions: - Idle at speed 0 - Walk at speed 0.5 - Run at speed 1.0
This blend tree blends animations based on both how fast and which direction the character moves.
Unity
// Example: 2D Blend Tree controlled by speed and direction Blend Tree with motions: - Walk Forward - Walk Backward - Walk Left - Walk Right Parameters: speed (float), direction (float)
Sample Program
This script changes the "Speed" parameter in the Animator based on arrow key input. The blend tree uses this parameter to smoothly blend between idle, walk, and run animations.
Unity
using UnityEngine; public class SimpleBlendTreeController : MonoBehaviour { public Animator animator; public float speed = 0f; void Update() { // Simulate speed change with arrow keys if (Input.GetKey(KeyCode.UpArrow)) speed += Time.deltaTime; else if (Input.GetKey(KeyCode.DownArrow)) speed -= Time.deltaTime; else speed = Mathf.MoveTowards(speed, 0, Time.deltaTime * 2); speed = Mathf.Clamp(speed, 0f, 1f); animator.SetFloat("Speed", speed); } }
OutputSuccess
Important Notes
Always set up your Animator parameters to match the blend tree controls.
Test your blend tree by changing parameters in the Animator window to see how animations blend.
Use normalized values (like 0 to 1) for parameters to keep blending predictable.
Summary
Blend trees mix animations smoothly based on parameters.
Use them to create natural movement transitions like idle to run.
Control blend trees with Animator parameters like speed or direction.