Menu
CoddyTech

A Patrolling Guard

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

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

In enemy.gd:

  1. 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.

Try it yourself

extends Area2D

func _physics_process(delta):
	pass
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Projects

Practice on your own: Godot playground