Reader small image

You're reading from  Unity AI Programming Essentials

Product typeBook
Published inDec 2014
Publisher
ISBN-139781783553556
Edition1st Edition
Tools
Right arrow
Author (1)
Curtis Bennett
Curtis Bennett
author image
Curtis Bennett

Curtis Bennett has been a developer in the games and computer graphics industry for several years. He has worked on developing immersive virtual environments, published research in visual simulation, taught college courses in game development, and worked for various game studios, and he was also an engineer on early versions of the RAIN AI plugin for Unity. Currently, he is the Technical Director for Creative Services at Ideum, which focuses on creating interactive media projects.
Read more about Curtis Bennett

Right arrow

Chapter 11. Advanced NavMesh Generation

Navigation mesh generation is one of the most important topics in game AI. We have been using navigation meshes in almost all the chapters in this book, but haven't looked at them in detail. In this chapter, we will provide a more detailed overview of navigation meshes and look at the algorithm used to generate them. Then, we'll look at different options of customizing our navigation meshes better.

In this chapter, you will learn about:

  • The working of navigation mesh generation and the algorithm behind it

  • Advanced options for customizing navigation meshes

  • Creating advanced navigation meshes with RAIN

An overview of a NavMesh


To use navigation meshes effectively, also referred to as NavMeshes, the first things we need to know are what exactly navigation meshes are and how they are created. A navigation mesh is a definition of the area an AI character can travel to in a level. It is a mesh, but it is not intended to be rendered or seen by the player; instead, it is used by the AI system. A NavMesh usually does not cover all the area in a level (if it did, we wouldn't need one) as it's just the area a character can walk. The mesh is also almost always a simplified version of the geometry. For instance, you could have a cave floor in a game with thousands of polygons along the bottom that show different details in the rock; however, for the navigation mesh, the areas would just be a handful of very large polygons that give a simplified view of the level. The purpose of a navigation mesh is to provide this simplified representation to the rest of the AI system as a way to find a path between...

Advanced NavMesh parameters


Now that we know how navigation mesh generations works, let's look at the different parameters you can set to generate them in more detail.

We'll look at how to do these parameters with RAIN using the following steps:

  1. Open one of our previous scenes or create a new one with a floor and some blocks for walls.

  2. Then, go to RAIN | Create NavMesh. Also, right-click on the RAIN menu and choose Show Advanced Settings. The setup should look something like the following screenshot:

    The NavMesh setup

    Now let's look at some of the important parameters:

    • Size: This is the overall size of the navigation mesh. You'll want the navigation mesh to cover your entire level and use this parameter instead of trying to scale up the navigation mesh through the Scale transform in the Inspector window. For our demo here, set the Size parameter to 20.

    • Walkable Radius: This is an important parameter to define the character size of the mesh. Remember, each mesh will be matched to the size of a...

Culling areas


Being able to set up areas as walkable or not is an important part of creating a level. To demo this, let's divide the level into two parts and create a bridge between the two. Take our demo and duplicate the floor and pull it down. Then transform one of the walls to a bridge. Then, add two other pieces of geometry to mark areas that are dangerous to walk on, like lava.

Here is an example setup:

This is a basic scene with a bridge to cross.

If you recreate the navigation mesh now, all the geometry will be covered and the bridge won't be recognized. To fix this, you can create a new tag called Lava and tag the geometry under the bridge with it. Then, in the navigation meshes' RAIN component, add Lava to the unwalkable tags. If you then regenerate the mesh, only the bridge is walkable. This is a screenshot of a navigation mesh with polygons that are under the bridge culled out:

To see this in action, create a new ship and add a target to the scene on different sizes of the bridge...

Multiple navigation meshes


So far, we have only looked at setting up a single navigation mesh in a scene, but navigation meshes are designed to be per character and not just one for the entire scene. We need multiple navigation meshes, but there is no field to directly set which navigation mesh to use for a character. Instead, RAIN uses a field called graph tags to correlate meshes with characters. To see how this works, let's add a second bridge to our scene that is larger and a second ship with double the scale. Here is an example setup:

This is a demo scene setup with an additional larger ship and larger bridge. Regenerating the mesh gives us a path over both bridges:

This is a navigation mesh with a smaller walkable radius that can cross both bridges.

Any character using this mesh will be able to go across either bridge. For our larger character to not be able to cross the smaller bridge, we need to generate another mesh with a smaller Walkable Radius. Create a second navigation mesh in...

Summary


Navigation meshes are an important part of game AI. In this chapter, we looked at the different parameters to customize navigation meshes. We looked at things such as setting the character size and walkable slopes and discussed the importance of the cell size parameter. We then saw how to customize our mesh by tagging different areas as not walkable and how to set up multiple navigation meshes for different characters.

We now have all the essential skills we need to create AI in Unity. We've seen how to have a character move, navigate, and sense other characters in our game scenes as well as how to set up behavior trees to make decisions and integrate animation. We also looked at different AI use cases, such as crowds, driving, and had our characters attack and change behavior based on game events. This covers a lot about AI, but game AI is a huge and much studied topic and there is much more to learn. By doing some searching, you'll find that there are many online articles, textbooks...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Unity AI Programming Essentials
Published in: Dec 2014Publisher: ISBN-13: 9781783553556
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Curtis Bennett

Curtis Bennett has been a developer in the games and computer graphics industry for several years. He has worked on developing immersive virtual environments, published research in visual simulation, taught college courses in game development, and worked for various game studios, and he was also an engineer on early versions of the RAIN AI plugin for Unity. Currently, he is the Technical Director for Creative Services at Ideum, which focuses on creating interactive media projects.
Read more about Curtis Bennett