Walk the Dungeon
Part of the Projects section of Coddy's Godot journey. Lesson 26 of 33.
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
EasyIn hero.gd, add const SPEED = 160. Every physics frame, set velocity to Input.get_vector of the four move actions times SPEED. Then call move_and_slide().
Try it yourself
extends CharacterBody2D
func _physics_process(delta):
pass
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Projects
1Project: Breakout
Breakout PaddleKeep the Paddle on Screen4Project: Dungeon Crawler
Walk the DungeonPick Up the KeyPractice on your own: Godot playground