Creating a Modular Waypoint System
Explaining how I created a modular waypoint system for AI
--
Objective: Create a modular waypoint system to allow our enemy AI to intelligently walk around the scene.
Setting up Waypoints
To start, I created a new script, called GuardAI. I then went ahead and gave my guards in the scene the NavMeshAgent component so they can walk along my baked NavMesh. I also attached the GuardAI script to them.
In my GuardAI script, I created a List<Transform> property named wayPoints. With this, we can give different guards their own specific set of waypoints to follow, which is a very powerful and modular setup.
With the ability to now set waypoints, I created three empty GameObjects for my first security guard. The GameObjects I created are highlighted in blue below:
One of the empty GameObjects was to act as a parent for the waypoints. The other two represent the two points the guard will travel between. Now we can get the guard moving between these points.
Move Between Waypoints
With all the setup we did in the previous step, getting the guard to move between the two points was not too hard for me. I was able to get it done in less than 50 lines.
With the above code I was able to get the following result:
Just like that, we have AI guard movement between waypoints! Still, lots to do to mark this complete overall, but I will mark an end to the article here. I’ll provide links to the final result of the game once I am done! Otherwise, have a nice rest of your day!