A Patrolling Guard
Part of the Projects section of Coddy's Godot journey. Lesson 32 of 36.
A guard stands behind the door: a red slime, in enemy.gd. It walks up and down without stopping. A direction of 1 means down, and -1 means up:
position.y += SPEED * direction * delta
if position.y > 320 or position.y < 70:
direction = -directionFlipping direction at either end turns the guard around.
A guard on a fixed path is fair. The player can watch it, learn its timing and get past.
Challenge
EasyMake the guard patrol.
- In
enemy.gd, addconst SPEED = 80andvar direction = 1. - Every physics frame, move the guard by
SPEED * direction * deltaony. - Flip
directionwhenyis over320or under70.
Press Play: the guard walks up and down. Then Check.
Try it yourself
extends Area2D
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