Guard AI prepare the patrol route

Guard AI part 1

Tristan Engel
2 min readSep 19, 2021

--

In this article we will prepare to a create modular script for our enemy guards. When having individual scripts it will be harder to extend on it later if needed.

Our Guards should be moving along the Nav Mesh as well so we need to make sure they have the Nav Mesh Agent Component.

List type variable

We will create List type variable for waypoints. List is much like an Array variable only it is dynamic so it can change size depending on the content of the list. (It’s a variable type that is also useful for itemlist in an RPG.) Each guard with it’s script can have different size list and different amount of waypoints assigned.
Add the following code to create a list variable that we can access through our Inspector.

public List<Transform> waypoints;

We already have our Guards in our Scene. Duplicate them and move them to the destination for each waypoint.

Rename the duplicates to waypoints

Then set the waypoint variable size in the Inspector.
Drag in the waypoint gameobjects into script variables of the Inspector.

Don’t forget to remove the scripts and nav mesh agents from the Waypoint gameobject. We only need the transform component.

To organize we created an empty gameobject to contain all waypoints and renamed the guards and the points.

--

--

Tristan Engel

Aspiring developer that’s self-learning Unity & C# to transition to a career with Unity. I got a passion for creating interactive experiences.