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:
- set its
2D Simple Directional
- then you set the
Parametersof the newly created blend tree to
- then you add all the 8 direction motions to the blend tree.
- In terms of the
PosYvalues, imagine a simply 2D world with just x-axis and y-axis. Your character stands at
- so when he goes up, the coordinate will be
- so when he goes down, the coordinate will be
- when goes right, the coordinate will be
- when goes down left, the coordinate will be
- In terms of the
- Create a boolean parameters named
isWalkset it as the transistion
idleto your new
Then in your code, you can do something like this:
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
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.