Forest Stories - Unity Nav Meshes

Forest Stories will have agents moving between locations so I thought I’d learn more about Unity navigation. I’d used it with my previous project but only to move a selected agent to the tile I clicked on so which was pretty easy. For Forest Stories I wanted to learn how to have agents follow roads, so that’s what I did!

I spent a while watching the Intermediate Fundamentals: Unity Game Dev Course course course, specifically the Unity Navigation Fundamentals section. The videos could be better, they just end unexpectedly and don’t always explain why you’re doing what you’re doing, but they’re up to date and they work. Most importantly, they demonstrate how to use the Nav Mesh Components that Unity links you to when you view the regular Navigation window.

Pro Tip: Learn to watch videos at faster playback speeds. Most video players let you watch at up to 2x playback speed and once you’re used to people talking quickly you can, well, halve the time it takes to watch a video series. That’s pretty valuable when they’re several hours long.

Basic navigation

While Forest Stories won’t give the player direct control over any agents, I thought testing navigation would be easier if I could move an agent manually so I added class to enable moving an agent to where I have clicked on the terrain. I then set all of the objects I’d placed as nav mesh obstacles, tweaked the agent’s movement settings, and I had an agent that moved around the map without ghosting through things. So far so GUI.

The next step for Forest Stories is for me to have an agent move to a resource pile and carry resources to a storage site. I slapped two different coloured boxes on the map and then thought “I wonder how you make roads”.

The Nav Mesh Components scripts allow you to set objects in the world as nav mesh segments with different settings. I created two new navigation areas: grass, with a movement cost of 10, and road, with a movement cost of 1. I set the terrain to be grass and some Plane objects to road, baked the nav mesh, and got this:

I had expected the agent to follow all of the roads, but instead it took a shortcut near the end. Fine, I thought, I’ll make the roads a bit more direct and raise the cost of moving over grass to 100.

See how it takes that shortcut right before the fork? Rude.

I spoke to a friend about this and he mentioned that the nav mesh tile size could be a thing to look at. I had a look at the NavMeshSurface component on my Terrain and lo and behold:

I set the Tile Size to 128, baked the nav mesh, and finally got the result I’d been expecting:

I set the grass cost back to 10 and everything continued to work as expected. I also tried setting the voxel size to 0.125 making it 4 voxels per agent radius instead of changing the tile size and got the same result. I’m not sure which change is better so for now I’ve stuck with halving the tile size and will revisit later if I get more unexpected behaviour.

Next Tasks

  • Find a free UI asset pack of some sort
  • Add a simple UI that shows the stock levels of the resource pile and the warehouse
  • Have an agent move between them and carry resources from the pile to the warehouse