What is Blend Tree in Unity: Simple Explanation and Example
blend tree in Unity is a tool inside the Animator that smoothly mixes multiple animations based on input values. It helps create natural transitions, like blending walking and running animations depending on speed.How It Works
A blend tree works like a smart mixer for animations. Imagine you have different dance moves, and you want to smoothly switch between them depending on the music's speed. Instead of jumping abruptly from one move to another, a blend tree blends them gradually based on a number you give it, like speed or direction.
In Unity, you set up a blend tree inside the Animator. You add animations as branches and define parameters that control how much each animation shows. For example, if your character's speed is low, the blend tree shows mostly the walking animation. As speed increases, it mixes in more of the running animation, creating a smooth transition.
This makes character movement look natural and responsive without needing many separate animations for every possible state.
Example
This example shows how to create a simple blend tree in Unity's Animator to blend between walking and running based on a speed parameter.
using UnityEngine; public class CharacterMovement : MonoBehaviour { public Animator animator; public float speed = 0f; void Update() { // Simulate speed change with arrow keys if (Input.GetKey(KeyCode.UpArrow)) speed += Time.deltaTime * 2f; else speed -= Time.deltaTime * 2f; speed = Mathf.Clamp(speed, 0f, 1f); // Set the speed parameter in Animator to control blend tree animator.SetFloat("Speed", speed); } }
When to Use
Use blend trees when you want smooth animation transitions based on continuous values like speed, direction, or any other parameter. They are perfect for character movement where you blend between idle, walk, and run animations depending on how fast the character moves.
Other real-world uses include blending different attack animations based on player input strength or mixing facial expressions smoothly in dialogue scenes.
Key Points
- Blend trees mix multiple animations smoothly based on parameters.
- They reduce the need for many separate animation states.
- Commonly used for character movement and natural transitions.
- Set up inside Unity's Animator with parameters controlling the blend.