How to use blend tree in Unity for a 2D game
A game is all about animation, your character plays different animations according to different states. When you develop a 2D game,sometimes you might need to change the direction that your character faces, it requires different animations, and if your game is a 8 direction game, it will be 8 animations for each action (run, walk, shoot, etc). It is a nightmare to create them in the animation view and transition them in code.
Blend tree is the answer here. You can pass the in-game values to a blend tree and let the blend tree to automatic pick up the proper animation for you.
For example, when you want to render 8 direction walking animations,the whole work flow is like the following:
- you create a blend tree
- create 2 float type parameters:
moveX
andmoveY
- set its
Blend type
to2D Simple Directional
- then you set the
Parameters
of the newly created blend tree tomoveX
andmoveY
- then you add all the 8 direction motions to the blend tree.
- In terms of the
PosX
andPosY
values, imagine a simply 2D world with just x-axis and y-axis. Your character stands at(0,0)
, - so when he goes up, the coordinate will be
(0,1)
- so when he goes down, the coordinate will be
(0,-1)
- when goes right, the coordinate will be
(1,0)
- when goes down left, the coordinate will be
(1,-1)
- In terms of the
- Create a boolean parameters named
isWalk
set it as the transistionConditions
from theidle
to your newwalk
blend tree.
Then in your code, you can do something like this:
1 | void Start() |
The GetEnemyDirection
is just a helper method which normalize your destination coordinate so it will always fall into the range 1, 1
Just as same as the PosX
and PosY
you have set before, right? :)
Via this way, it is very easy to manage a 8 direction game animation. But Blend tree is more than this, it can be used to blend between 2 animations to create a smooth transition, widely used in 3D animation such as transitioin from walk to run. But the idea is the same. You can learn more in the unity official documents.
Thanks for reading!
Follow me (albertgao) on twitter, if you want to hear more about my interesting ideas.