Navmesh in Unity

In the previous article, we learn how to make a point and click system for moving our player. Today, we will learn to use the Navmesh system to make our player move to the hit point of the raycast we created.

The first thing we need to do is make all the GameObjects that are meant to be obstacles static. To do that, click on all of the GameObjects and mark them as static. In our project, all these GameObjects all in an empty GameObject called “DetailedModel”.

Click “Yes, change children” to change all the GameObject to static.

Now we set up the floor. To do that we will use the Navmesh system. Go to Window -> AI -> Navigation. A new tab will open. Click on the Bake button to bake build the NavMesh. Now, our player will know where he can walk.

Go to the Player GameObject and add the NavMeshAgent component.

Go back to the Player script. At the top, add a new namespace to access the AI system in Unity.

We need to access the NavMeshAgent component.

The last thing we need to do is set the destination of our player. We can remove the code that's creating the cube at the point of hit.



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