Play on a Key
Part of the Game Feel section of Coddy's Godot journey. Lesson 21 of 23.
Your code can start an animation at any moment. Playing a new animation stops the one that was playing and starts the new one from its first key.
func _process(delta):
if Input.is_action_just_pressed("jump"):
$AnimationPlayer.play("hop")"hop" does not loop. It plays once, for 0.6 seconds, and stops at its last key with the robot back on the floor.
The motion is in the animation: the stretch at takeoff, the rise and the fall. The script only decides when it plays.
Challenge
EasyMake the robot hop when you press Space.
- In
_process(), check whether"jump"was just pressed. - If it was, play
"hop"on$AnimationPlayer.
Press Play and press Space: the robot hops and lands. Then Check.
Try it yourself
extends Node2D
func _ready():
$AnimationPlayer.play("bob")
func _process(delta):
pass
All lessons in Game Feel
Practice on your own: Godot playground