Left and Right
Part of the Input and Movement section of Coddy's Godot journey. Lesson 2 of 28.
One if checks one action. To walk both ways, add a second if for the other action:
func _process(delta):
if Input.is_action_pressed("move_right"):
position.x += 200 * delta
if Input.is_action_pressed("move_left"):
position.x -= 200 * deltaEvery frame Godot runs both checks. Each one moves the hero only while its key is held.
Hold both keys and both lines run in the same frame. One adds 200 pixels per second, the other takes them away, so the hero stays still.
Challenge
EasyThe hero walks right. Make it walk left at 200 pixels per second too, while you hold move_left.
Try it yourself
extends Node2D
func _process(delta):
if Input.is_action_pressed("move_right"):
position.x += 200 * delta
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Input and Movement
5Project: Platformer Hero
Hero: RunHero: JumpPractice on your own: Godot playground