My journey becoming a Unity game developer: Enemy AI-Setup the guards to patrol the area (Part 1)
Objective: Using the NavMeshAgent to have the security guard move from one area to another with waypoints.
First thing we will do is select all 3 security guards in the Hierarchy. Use the Add Component button and give them a NavMeshAgent component. Also, create a new C# script name GuardAI which will control how the guards will patrol the area, and attach this script to the 3 guards as well.
Inside the GuardAI script, we’re going to use a List<> class to hold all the waypoints that will be used for the guards path. In order to use the List<> class, let’s add the Namespace of System.Collections.Generic which has the library of methods for the List<> class.
Then create a Public List variable with the type Transform name wayPoints which will hold a list of all the waypoints created transforms. These transforms will be used to position each of the waypoints that the security guards will have to walk to.
After saving the GuardAI script, go back to the Hierarchy and select each Security Guard game object. Under the GuardAI script component we will see the Way Points setting with zero in its slot. For the 1st Security Guard, we’re going to add 2 waypoints in the slot just to create a simple waypoint test just to see it working.
Next, we’re going to create 2 cube game objects that will represent the waypoints, then name them Point A and Point B. Create an empty game object name WayPoints, set the Transform positions to zero, and place Point A and Point B waypoints inside of WayPoints. Scale down the 2 cubes axis with x=2, y=1, and z=2. For the 1st waypoint position, copy the 1st Security Guard’s Transform component from the hamburger menu next to the Inspector tab. Go back to Point A game object and Paste the Component Values in the Transform using the same hamburger menu. We will see the Point A object positioned directly to the same position as the 1st Security Guard. Do the same thing with the 2nd Security Guard and Point B copying and pasting the Transform component values. Point B waypoint will be positioned now in the position of the 2nd Security Guard.
In the GuardAI script, we’re going to use the NavMeshAgent to move the security guards around the room while patrolling different areas. To do this we need to add the Namespace UnityEngine.AI to use Unity’s artificial intelligence system on the guards. Next, create a new Public Transform variable name currentTarget which will determine the next destination the security guard will move to. Also, create another variable which will be a handle for the NavMeshAgent name agent.
Inside the Start() function, use the agent variable to get the NavMeshAgent component. Then create an IF condition that will check if the List of waypoints is more than zero. If it is, we will check IF the 1st waypoint exists, and if it does set the current target the guard will go to as the 2nd waypoint in the List. After setting the next target, use the agent to tell the Security Guard that its next destination will be the current target position.
Last, select the 1st Security Guard game object in the Hierarchy, drag the Point A waypoint into the Element 0 slot, and drag Point B waypoint into the Element 1 slot under Way Points inside of the GuardAI script component. Press Play and watch the 1st Security Guard move from Point A to Point B in what is supposed to be the fastest path to their destination.
This is just a simple way of making the guard move from one area to another. Next time we will work on setting up a modular way for the 3 guards to patrol the area.