Press to Move
Part of the Input and Movement section of Coddy's Godot journey. Lesson 1 of 28.
Godot reads keys through input actions: names for what the player wants to do. Every game in this course has these actions:
move_left,move_right,move_up,move_down: the arrow keys and WASDjump: Space, W or Upaction: E or Enter
Input.is_action_pressed("move_right") is true while the player holds the right arrow or D. Ask it in _process, which runs every frame:
func _process(delta):
if Input.is_action_pressed("move_right"):
position.x += 200 * deltaThe line under the if starts with two tabs. It runs only in frames where the key is held, so the hero stops when you let go.
Challenge
EasyMake the hero walk right while you hold a key.
- In
_process(), replacepasswith anifthat asksInput.is_action_pressed("move_right"). - Inside it, move the hero right at
200pixels per second, withdelta.
Press Play and hold the right arrow: the hero walks right and stops when you let go. Then Check.
Try it yourself
extends Node2D
func _process(delta):
pass
All lessons in Input and Movement
5Project: Platformer Hero
Hero: RunHero: JumpPractice on your own: Godot playground