Setting Up a Patrol Route: A Modular Waypoint System (Game Dev Day 60)

Jack Leavey
2 min readJan 15, 2022

With player movement completed, I moved onto creating the patrol behavior for the 3 guards on the level. All of these guards operate on the GuardAI script which I added to each guard’s gameobject.

First off, I wanted to make this system modular and easy to add to, so I made one script for all 3 guards to share, using a list to set the various waypoints I needed each guard to travel to and from.

In Unity, I can now set how many waypoints each guard has. The guard closest to the player’s starting position was assigned 3 waypoints (A,B,C)

These physical waypoints are just empty gameobjects placed at the physical locations I want the guard to path to. For demonstration purposes, I have added a visual indicator of the three waypoints as cubes.

Waypoints.

To get my guard to move to each waypoint incrementally, I used the following code.

This code checks to ensure that each guard has a waypoint to be going to, then commands them to travel to that waypoint. It also turns on their walking animation when in motion, and turns it off when they stop at their outer waypoints. They do NOT stop at waypoints between the two farthest apart waypoints.

The Coroutine (WaitBeforeMoving) controls how the guards know when to stop and when not to stop.

With all that in place, lets see what the guard’s path looks like!

For demonstration purposes, this guard has been sped up.

--

--

Jack Leavey

I am a software engineer with years of experience branching into game development, specifically in Unity. Follow along for guides on creating game mechanics!