Menu
CoddyTech

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 = -direction

Flipping 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 icon

Challenge

Easy

Make the guard patrol.

  1. In enemy.gd, add const SPEED = 80 and var direction = 1.
  2. Every physics frame, move the guard by SPEED * direction * delta on y.
  3. Flip direction when y is over 320 or under 70.

Press Play: the guard walks up and down. Then Check.

Try it yourself

extends Area2D

func _physics_process(delta):
	pass

All lessons in Projects

Practice on your own: Godot playground