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
EasyWalk the hero through the dungeon.
- In
hero.gd, addconst SPEED = 160. - Every physics frame, set
velocitytoInput.get_vectorof the four move actions timesSPEED, then callmove_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
1Project: Breakout
Breakout PaddleKeep the Paddle on Screen4Project: Dungeon Crawler
Walk the DungeonPick Up the KeyPractice on your own: Godot playground