The Great Fleece: Unity NavMesh System for Smart AI

Brian Branch
3 min readAug 8, 2021

--

Now it’s time to get the guards moving around. I will be using the Unity NavMesh that was baked in a previous article to define where the guards can walk. Also, I’ll be setting up a waypoint system for the path of the first guard in this article.

I’ll be setting up the path for the guard on the lower right, who will walk to the left then up before turning back.

First, I created a script called GuardAI and added the script and a NavMeshAgent to the guard. The script will be modular enough to be used for each guard instead of having separate scripts for each one.

The first thing that I added to the script was a public variable for a list of waypoints.

Then I went into Unity to start setting up the waypoints for the first guard. I created an empty GameObject named Waypoints to hold the waypoints I’ll be using. I also created three waypoints to be used by the first guard. I use empty GameObjects for the waypoints named Point_A, Point_B, and Point_C. I then copied the Transform values from the guard to the GameObjects. Then I moved Point_B and Point_C to the correct places.

The guard will start at Point_A then move to Point_B followed by Point_C following the NavMesh before returning.

The script was a bit of work getting the guard to go back and forth correctly. But it works to have the guard follow the waypoints and return. The current full script is below.

That’s it for setting up the movement for the first guard.

I hope you enjoyed this article, and until next time I wish you the best in your own coding and game development journey.

--

--