All lessons
Lesson 01

Top-Down Movement

Walk around using arrow keys. The starting lesson: one player, four directions, and the code that reads the keyboard.

Loading the game engine. This takes a moment the first time.

Top-down only

This is the starting lesson. All it does is let you walk around, and everything that makes that happen is in this one file.

The scenes

UI node · 2D node · % unique name · .tscn instanced scene

top_down_movement.tscn
  • TopDownMovement Node2D
  • TopDownPlayer CharacterBody2D player.tscn
  • Borders StaticBody2D
  • Top CollisionShape2D
  • Bottom CollisionShape2D
  • Left CollisionShape2D
  • Right CollisionShape2D
player.tscn
  • TopDownPlayer CharacterBody2D player.gd
  • Sprite2D
  • CollisionShape2D
extends CharacterBody2D

## How fast the player moves. "@export" puts "speed" at the top of the
## Inspector panel, so you can change it there instead of in this file.
@export var speed: float = 200.0

# "_physics_process()" runs about 60 times a second. Godot calls it for you.
func _physics_process(_delta: float) -> void:
	# "Input.get_vector(...)" checks all four keys at once and turns them
	# into one arrow pointing where you want to go. The names in quotes are
	# Godot's built-in names for the arrow keys.
	var direction: Vector2 = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
	# "direction" is only WHICH WAY (its length is always 1).
	# "direction * speed" stretches it, so "velocity" is also HOW FAST.
	velocity = direction * speed
	# Nothing moves until "move_and_slide()" runs.
	# "Slide" means we slide along walls instead of sticking to them.
	move_and_slide()

	# The warrior picture only faces right, so walking left mirrors it and
	# walking right puts it back. Neither "if" runs when you stand still,
	# which is what keeps the warrior facing the way you last walked.
	if direction.x < 0.0:
		$Sprite2D.flip_h = true
	if direction.x > 0.0:
		$Sprite2D.flip_h = false