Navmesh and AI


The task was to generate a navmesh for an existing level and create some basic AI that will path around the level chasing the player.

First step was obviously to generate the navmesh. Unity makes this very easy with the inbuild navmeshing functionality. All the parameters were left default aside from all static objects having the "Navigation static" object option selected.

Once that was done, I generated (baked) the navmesh and after a couple small adjustments, it was done.

I also added a block that moves back and forth as a non-static nav object for the AI to dynamically path around as it moved. I wrote a simple script to make it move back and forth.

It also had a Navmesh Obstacle component on it as well, which had the settings left at default.

I then had to write a script to spawn enemies and have them chase the player.

This was super simple. First I wrote a function to instantiate the enemy game object on a predetermined position. Once that was done, a single line of code was put in to the start function that just targets the navmesh agent and assigns that to the agent variable. Then in the update function it was time to write the AI "steering" behaviours. First I stored the player's current position as a variable. Then, if the player clicks on the screen, store that Vector3 location data as a variable and feed that into the spawn function. Then set the agent's destination as the player's current position.

The only downside of this function is that there is a slight delay (about 5 ticks) between the playerPos vector data and the player's ACTUAL position in-game. But I don't think that's a very big deal so I just left it.

And thats it! The finished result is almost like a zombie game, where the enemies try to overwhelm the player with their sheer numbers!

Leave a comment

Log in with itch.io to leave a comment.