Objective: Create patrolling enemies using the NavMesh system!
Patrolling enemies can sometimes be a nightmare in certain games. Wouldn’t feel nice if they were YOUR patrolling enemies? Let me show you how.
To begin we need enemies. Use whatever models you have and follow along. Our guards are going to move through the NavMesh, the same way our player did in the previous tutorial.
To begin Create/open your script for your enemies. We are going to set this as a modular script so we can use one script to control all your enemies. Select all your guards and add the NavMesh Agent components and the script is you haven’t already.
We are going to create a List of waypoints. To use a list, you need to add a new namespace to your script.
This allows you to create a List.
Lists are similar to Arrays, however, list are dynamic, meaning you can extent or shrink the list in real time. An array is a fixed length of elements.
The best way to assign waypoints is to select your enemy character and place him in his starting position.
Duplicate the character with CTRL + D. Place the duplicate at the beginning of the destination and rename the duplicate to “Point A.”
Duplicate again, naming this one “point B” and placing this one at the end destination.
Select the original guard and drag point A into element 0 and point B into element 1.
Create an empty object in your hierarchy “Waypoints” and child point A and B into it.
Select both A and B and remove their NavMesh components as well as script components.
It’s time to move, and movement for these characters is done the same way as we did the player, minus the input. The enemy is going to move towards its target, once it’s at its current target it will be assigned a new target. So, we need a variable to hold the current target. We also need a bool for revers and a bool for if our target is reached.
As mentioned before this is a modular script, meaning all your enemies will use this once script. So, the first parameter we need to check for is if the enemy has any waypoints. If they do, then we want to ensure that the waypoint is not null. If it is NOT null and the enemy does have a waypoint then we want to set the destination to that waypoints position.
Following this we need to calculate the distance between the current target position and the enemy’s position. Once the distance is small enough, we want to run through the next layer of Parameters. If the current target is zero, meaning it is in the beginning of the path, or is the target at the waypoint max count. If the guard is at the beginning of the path, or the end we want him to halt for a randomized amount of time.
Halting something for an amount of time? We both know the proper way to halt a sequence would be to use a coroutine. We need a simple routine. If the current target is at 0 we want to wait and if the target is at the waypoint count then we wait.
Also, in this coroutine we can monitor if the waypoints are at their max count and if so, we need to change the bool for reverse to true and current target will need to be minus. We will need to do the opposite for the reverse formula. We want to control the highest point and the lowest point.
Now to call this routine from the if statements from above.
Now we need to set up a formula to increment and decrement the current target integer. Using the reverse bool, we can set if statements to determine what direction to go. Also, if the reverse gets to zero then we need to set the current target to zero to force the integer to not go in the negatives.
With these parameters in place, you should now have movement in your enemies, so long as they have waypoints. Don’t be afraid to get creative with your waypoint placements. With this modular script you can set as many waypoints as you feel, and you should be able to plug and play at this point. In the next tutorial we will discuss how to use Unity’s animation system!