Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Unity Artificial Intelligence Programming - Fifth Edition

You're reading from  Unity Artificial Intelligence Programming - Fifth Edition

Product type Book
Published in Mar 2022
Publisher Packt
ISBN-13 9781803238531
Pages 308 pages
Edition 5th Edition
Languages
Author (1):
Dr. Davide Aversa Dr. Davide Aversa
Profile icon Dr. Davide Aversa

Table of Contents (17) Chapters

Preface Part 1:Basic AI
Chapter 1: Introduction to AI Chapter 2: Finite State Machines Chapter 3: Randomness and Probability Chapter 4: Implementing Sensors Part 2:Movement and Navigation
Chapter 5: Flocking Chapter 6: Path Following and Steering Behaviors Chapter 7: A* Pathfinding Chapter 8: Navigation Mesh Part 3:Advanced AI
Chapter 9: Behavior Trees Chapter 10: Procedural Content Generation Chapter 11: Machine Learning in Unity Chapter 12: Putting It All Together Other Books You May Enjoy

Chapter 8: Navigation Mesh

As we saw in Chapter 7, A* Pathfinding, the most critical decision in pathfinding is how to represent the scene's geometry. The AI agents need to know where the obstacles are, and it is our job as AI designers to provide the best representation we can to the pathfinding algorithm. Previously, we created a custom representation by dividing the map into a 2D grid, and then we implemented a custom pathfinding algorithm by implementing A* using that representation. But wouldn't it be awesome if Unity could do all that for us?

Fortunately, Unity can do this using Navigation Meshes (NavMeshes). While in the previous 2D representation, we divided the world into perfect squares, with NavMeshes, we will divide the world using arbitrary convex polygons. This representation has two exciting advantages: first, every polygon can be different, and therefore we can use a small number of big polygons for vast open areas and many smaller polygons for very crowded...

Technical requirements

For this chapter, you just need Unity3D 2022. You can find the example project described in this chapter in the Chapter 8 folder in the book repository: https://github.com/PacktPublishing/Unity-Artificial-Intelligence-Programming-Fifth-Edition/tree/main/Chapter08.

Setting up the map

To get started, let's build a simple scene, as shown in the following screenshot. This is the first scene in the example project and is called NavMesh01-Simple.scene. You can use a plane as the ground object and several cube entities as the wall objects:

Figure 8.1 – An image of the NavMesh01-Simple scene, a plane with obstacles

In the following subsections, we will set up the walls as obstacles, bake the NavMesh, and configure the tanks.

Navigation static

Once we add the floor and the obstacles, it is essential to mark them with the Navigation Static tag so that the NavMesh generator knows that they need to be taken into account during the baking process. To do this, select all of the objects, click on the Static button, and choose Navigation Static, as shown in the following screenshot:

Figure 8.2 – The Navigation Static property

Baking the NavMesh

Now that we have completed the scene...

Setting up a scene with slopes

Let's build a scene with some slopes, like this:

Figure 8.8 – Scene with slopes-NavMesh02-Slope.scene

One important thing to note is that the slopes and the wall should be in contact. If we want to use NavMeshes, objects need to be perfectly connected. Otherwise, there'll be gaps in the NavMesh, and the Agents will not be able to find the path anymore. There's a feature called Off Mesh Link generation to solve similar problems, but we will look at Off Mesh Links in the Using Off Mesh Links section later in this chapter. For now, let's concentrate on building the slope:

  1. Make sure to connect the slope properly:

Figure 8.9 – A well-connected slope

  1. We can adjust the Max Slope property in the Navigation window's Bake tab according to the level of slope in our scenes that we want to allow the Agents to travel. We'll use 45 degrees here. If your slopes...

Baking navigation areas with different costs

In games with complex environments, we usually have areas that are harder to traverse than others. For example, crossing a lake with a bridge is less challenging than crossing it without a bridge. To simulate this, we want to make crossing the lake more costly than using a bridge. This section will look at navigation areas that define different layers with different navigation cost values.

For this, we build a scene, as shown in Figure 8.11. Three planes represent two ground planes separated by a water plane and connected by a bridge-like structure. As you can see, crossing over the water plane is the most direct way to traverse the lake; however, passing through the water costs more than using the bridge and, therefore, the pathfinding algorithm will prefer the bridge to the water:

Figure 8.11 – Scene with layers – NavMesh03-Layers.scene

Let's follow a step-by-step procedure so that we can...

Using Off Mesh Links to connect gaps between areas

Sometimes, there may be some gaps in the scene that can make the NavMeshes disconnected. For instance, the Agents do not find a path in our previous examples if we do not tightly connect the slopes to the walls, so we need to make it possible to jump over such gaps. In another example, we may want to set up points where our agents can jump off the wall onto the plane below. Unity has a feature called Off Mesh Links to connect such gaps. Off Mesh Links can be set up manually or can be automatically generated by Unity's NavMesh generator.

Here's the scene that we're going to build in this example. As you can see in Figure 8.14, there's a small gap between the two planes.

Figure 8.14 – The scene with Off Mesh Links – NavMesh04-OffMeshLinks.scene

In this section, we will learn how to connect these two planes using Off Mesh Links.

Generated Off Mesh Links...

Summary

In this chapter, we learned how to generate and use NavMeshes to implement pathfinding for our games. First, we studied how to set up different navigation layers with varying costs for pathfinding. Then, using the destination property, we used the Nav Mesh Agent component to find the path and move toward the target. Next, we set up Off Mesh Links to connect the gaps between the NavMeshes using the autogeneration feature and a manual setup with the Off Mesh Link component.

With all this information, we can now easily create simple games with a reasonably complicated AI. For example, you can try to set the destination property of AI tanks to the player's tank's position and make them follow it. Then, using simple FSMs, they can start attacking the player once they reach a certain distance. FSMs have taken us far, but they have their limits. In the next chapter, we will learn about Behavior Trees and how we can use them to make AI decisions in even the most complex...

lock icon The rest of the chapter is locked
You have been reading a chapter from
Unity Artificial Intelligence Programming - Fifth Edition
Published in: Mar 2022 Publisher: Packt ISBN-13: 9781803238531
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.
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}