Menu
CoddyTech

Walk the Dungeon

Part of the Projects section of Coddy's Godot journey. Lesson 29 of 36.

The last project is a dungeon crawler, seen from above. The hero takes a key, opens a door, gets past a guard and reaches the stairs.

The scripts are hero.gd, enemy.gd and main.gd. The walls are a StaticBody2D called Walls, already in the scene.

The hero walks in four directions. Input.get_vector reads all four keys and gives a direction of length 1, so diagonals are not faster. move_and_slide() moves the body by its velocity and slides it along walls:

velocity = Input.get_vector("move_left", "move_right", "move_up", "move_down") * SPEED
move_and_slide()

move_and_slide uses delta itself, so you do not multiply by it.

challenge icon

Challenge

Easy

Walk the hero through the dungeon.

  1. In hero.gd, add const SPEED = 160.
  2. Every physics frame, set velocity to Input.get_vector of the four move actions times SPEED, then call move_and_slide().

Press Play and walk: the walls stop the hero. Then Check.

Try it yourself

extends CharacterBody2D

func _physics_process(delta):
	pass

All lessons in Projects

Practice on your own: Godot playground