Building A Stealth Game — Creating Waypoints For Your AI Guards

Photo by Catarina Sousa from Pexels

Setting up the environment and the enemy

To get started, we need to do many of the things we did to set things up for the player in the last article. First of all, we need a baked nav mesh for our guard to patrol. Check out my previous article for more information.

Setting up the waypoints

Next we want to set up our waypoints. What we’re looking for is a collection of Vector3s that represent the position we want our guard to walk to.

Here is a simple four waypoint example made with cubes. We won’t need the cube meshes though.

Setting up the script

Now in our SimpleAI script we need to do some work. For starters, much like with our player in the last article, we need our script to have access to the Nav Mesh Agent. Create a serialized field for the Nav Mesh Agent, then assign that variable in the Unity Editor.

The Waypoints variable is an example of a serialized List

Making the guard move

Back in the script, the first thing we’ll need is to create a new script level integer variable to represent which waypoint in our list we are currently targeting.

Keeping them moving

Next we need to modify the Update method to have the guard continue moving along the waypoints. To do this, we need to understand a couple of things. The first thing is that we need to be watching for when our guard has reached their destination. The second thing is that your guards or other objects using a Nav Mesh Agent will probably never be exactly at their destination.

Conclusion

So we see again that the Unity navigation features save us a lot of work. Making an AI move around is pretty easy making use of those features.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store