My journey becoming a Unity game developer: Player Movement-Making the player move across the NavMesh

Rhett Haynes
3 min readOct 21, 2021

Objective: Have the player move along the NavMesh around obstacles to a point where the mouse pointer is clicked at using the Unity Navigation system.

Player moving around obstacles to the point where the mouse is clicked.

Inside of the Player script, we need to use the artificial intelligence in Unity to move the player along the NavMesh. Therefore, add the using UnityEngine.AI namespace to access all the properties used in the Navigation system. Then create a new variable which will be the handle for the NavMeshAgent character moving on the NavMesh.

In the Start() function, use the NavMeshAgent variable created to get the NavMeshAgent component of the player. Inside the IF condition checking for the Raycast, remove the previous code that was creating cubes where the mouse pointer was clicked at and have the NavMeshAgent variable set its destination to the point where the ray hits the object’s collider.

Using the Unity AI to move the player along the NavMesh where the mouse is clicked.

When we play the game, you will notice wherever we clicked at the player moves to that spot. However, the player is going through all objects in its way to that spot which isn’t good for our game.

Player moving across the navmesh where the mouse is clicked.

In the Hierarchy, we have a game object called Objects which has all the game objects that makes up the Auction Room level. Inside Objects there is a game object named DetailedModel which actually has the game objects in the level itself in it. With DetailedModel selected, go into the Inspector and turn on the Static setting next to the DetailedModel’s name. This will make all the objects in DetailedModel off limits for the characters using the NavMesh to be able to walk through.

Click on the Navigation tab and press the Clear button to remove the previous baked NavMesh. After clearing the baked NavMesh, click on the Bake button to bake the NavMesh again. The new baked NavMesh will bake around the Static objects now preventing the player from walking through it.

All objects in the room are set to Static and NavMesh rebaked.

Now when we play the game, the player will move around the objects that are Static to reach its destination point.

Player moving around the glass cases to where the mouse is clicked.

--

--