Menu
CoddyTech

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 * delta

Every 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 icon

Challenge

Easy

The 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
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Input and Movement

Practice on your own: Godot playground