Into Game Mechanics: Guards Patrol — Part 2

Daniele Quero, PhD
Sep 24 · 3 min read

Objective: Optimize patrol, introduce random patrol, introduce guard animations

Today we’re going to make thing better for the guards. First thing is to optimize their behaviour. I thought about it and leaving all in update methods results in useless calculations and vector comparisons. So, I’m going to shove everything in a coroutine and start it in the .Start() method.

I took the liberty of extracting the checks into bool-returning methods (for example WaypointsGood()checks if the array is non-null, non-empty). Essentially, the guards would patrol until isPatrolling variable holds a true value. It is the same logic as before, but now instead of doing it every 1/60 seconds we do it every _patrolStep seconds (being this variable a WaitForSeconds object). Moreover, when the guards reaches the edges of the path, he will rest for a longer amount of time (_idleOnTarget is about 3 seconds). With this move we reduced the number of performed operations by a factor 10, or greater.

Now, if you remeber, the second guard as a differently shaped path.

Bu I don’t just want it to be different in shape, I want the guard to randomly chose whether to turn left or right and do the same path in reverse.

Trying to keep things as abstract as possible I came up with this idea:

  1. Beside the list of waypoints, I will provide the guard script with a list of new objects made of a transform and id, meaning that the transform MAY be swapped with the waypoint having that particular id. A swap method also is provided.
  2. The script will be provided with a master-switch bool variable accessible from the inspector, telling if the guard can choose a random path or not.
  3. Every time the guard is in 0-position, there will be as much coin tosses as the number of switchable waypoints. The random number will help decide if the waypoint has to be swapped or not.

The main script will be provided with the list public List<EnemyPatrolModifier> modifiers , and the bool public bool hasRandomPath . This is how the coroutine will be modified:

In the path edges there just is a call on the method RecalculateRandomPath() .

So, the recalculation is performed only when the guard is in 0-position, for each possible swap element we decide randomly (50–50) if we have to swap, then we swap.

There is very little to say about this. We just have to duplicate the logic we already used for Darren… wait a minute did I just say duplicate code? I was not in me, sorry! Let’s star all over again.

SINCE the logic of transition from idle to walk is the same both for Darren and the guards, let’s write a simple MonoBehaviour script suitable for both of them. I’m going to assume that the guard and Darren animator have been created in the exact same way.

Remove this logic from Darren movement and attach the new script to all guards and Darren objects.

Don’t duplicate code. Ever.

Geek Culture

Proud to geek out. Follow to join our +1.5M monthly readers.