AnimationTree: (AnimationNodeBlendSpace1D)
This set up is suitable for blending animations in a linear way. For example, if your Tree setup is idle <-> walk <-> run then your character will go from "idle" to "run" in that order (passing "walk" animation very quickly).
For my example, I will use walking "walkback" <-> "idle" <-> "walk".
You need to be familiar with set() and lerp().
Let's start
1. The set up is the same, just choose AnimationNodeBlendSpace1D as your Tree Root.
2. AnimationTree Graph area
At the bottom you will see the "value" 0 in the middle and "-1" on the left and "1" on the right. Basically you will place your animation within those value positions so animation "walkback" ,"idle" ,"walk" would be in the positions of -1 , 0 , 1
2.1 To place your animations:
-Use the 3rd Icon (dot with a pencil)
-Go to far left (-1) and Leftclick to add ("walkback") animation.
-Go to Middle (0) add ("idle").
-Go to far right (1) add ("walk").
Use the 1st icon and drag to see how your animation looks.
Use the 2nd icon (arrow pointer) to select your animation and move them.
Use Trash Icon to delete them if you need to.
3. Code your Animation
3.1 Playing animation when walking forward and backward.
-The parameters responsible for playing animation is "parameters/blend_position" and the value is between -1 to 1 (the same values you used to place your animation).
-Use set() to play that animation. Add the code to your forward input.
if Input.is_action_pressed("forward"):
input.z += 1
$AnimationTree.set("parameters/blend_position", 1)
This will play my forward "walk" animation because I place it on the (1) value in the Graph Area.
and
if Input.is_action_pressed("backward"):
input.z -= 1
$AnimationTree.set("parameters/blend_position", -1)
This will play my walking "walkback"
and
if velocity == Vector3.ZERO:
$AnimationTree.set("parameters/blend_position", 0)
This will play my "idle" when velocity = Vector3.ZERO (When I'm not pressing anything so my character has no movement).
3.2 To Blend your animations:
-You can blend it by replacing the value (-1,0,1) with lerp(). It will slowly increases/decrease to those value, therefore blending.
eg.
if Input.is_action_pressed("forward"):
input.z += 1
$AnimationTree.set("parameters/blend_position", lerp($AnimationTree.get("parameters/blend_position"),1,0.05))
This will slowly reach the value 1 (my "walk" animation).
$AnimationTree.set("parameters/blend_position", lerp($AnimationTree.get("parameters/blend_position"),-1,0.05))
This will slowly reach the value -1 (my "walkback" animation).