Challenge 4: Godot blend
- Game Development course
- Expert
- Lesson 46/47
- 5 min
Write GDScript that uses blend in an AnimationTree.
Blending with Blend2D
In Godot the Blend2D node inside AnimationTree blends between 2 to 4 animations placed in a 2D space based on position. The blend position is set by assigning a Vector2 value to the blend_position property, and as speed increases the animation transitions toward running. This value is easily updated from GDScript.
agac.set("parameters/Blend2D/blend_position", Vector2(0.3, 0.0));This allows smooth transitions between animations like walking and running.
Practice Blend by speed
In the script BlendGirdisi, assign the $AnimationTree node to a variable named agac and define a variable named hiz with the value 0.7. Assign the value Vector2(hiz, 0.0) to the blend_position path and print the text "Blend: " joined with the hiz value using print.
Starter code
extends Node
func _ready():
var agac = $AnimationTree
var hiz = 0.7
# blend_position yoluna hiz degerini kullanarak bir Vector2 ata
# "Blend: " metnini hiz degeriyle birlestirip yazdirSample solution
extends Node
func _ready():
var agac = $AnimationTree
var hiz = 0.7
agac.set("parameters/Blend2D/blend_position", Vector2(hiz, 0.0))
print("Blend: " + str(hiz))Expected output
Blend: 0.7
Practice State transition
In the script GecisGirdisi, assign the $AnimationTree node to a variable named agac. Get the state machine into a variable named playback using the "parameters/playback" path, transition with playback.travel("ziplama") and print the text "Hedef: ziplama" with print.
Starter code
extends Node
func _ready():
var agac = $AnimationTree
# playback icin "parameters/playback" yolunu kullan
# ziplama durumuna gecis yap
# "Hedef: ziplama" metnini yazdirSample solution
extends Node
func _ready():
var agac = $AnimationTree
var playback = agac.get("parameters/playback")
playback.travel("ziplama")
print("Hedef: ziplama")Expected output
Hedef: ziplama
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which value type does the blend_position property of the Blend2D node accept?
agac.set("parameters/Blend2D/blend_position", Vector2(0.2, 0.0));- int
- string
- Vector2 Correct answer
- float
Explanation: Since blend_position is a two dimensional position it takes a Vector2 value.
2 The vector value
var hizVektoru = ____(0.5, 0.0)
var hizVektoru = Vector2(0.5, 0.0)
Explanation: The two dimensional blend position is given as a Vector2 value.
3 Blending terms
| Term | Meaning |
|---|---|
Blend2D |
Blend 2-4 animations using 2D position |
blend_position |
The 2D position value used to blend |
Vector2 |
A two dimensional vector value |
GDScript |
Godot's scripting language |