Nerd For Tech
Published in

Nerd For Tech

Unity Guide

Point & click to move | Unity

A quick guide about how to move the player using a NavMesh agent in Unity

Objective: Move the player towards a point in the ground defined by a click of the mouse with Unity.

In the last post I covered how to synchronize audio and visuals to build a cutscene. Now, it’s time to implement the player features and we’ll start with the movement by using a NavMesh agent that will move towards a position defined with a simple click.

Current scene

To start, let’s take a look at the current scene. There are several display cases with some guards around them. Each gameobject is above the floor, which contains a mesh collider:

Baking the NavMesh

A NavMesh allows us to create a navigation mesh by analyzing the different meshes and terrains in our scene that are static. So, in order to create a NavMesh, we should select and process the gameobjects that will affect the navigation by baking them.

To do it, let’s open the Navigation window by clicking on Window > AI > Navigation. Once the window is opened, let’s click on the Bake tab to modify the properties as we wish and finally let’s click on the Bake button to do it:

Once the floor finishes baking we’ll be able to see the NavMesh in a blue tone through the Scene window. This is an approximate of the walkable surface on the floor:

If you want to know more about the NavMesh you can visit the Unity docs:

Testing the NavMesh

Now, in order to test the baked NavMesh we’ll use a simple capsule that will represent the player in our game:

To use the NavMesh to navigate the scene we need to attach a Nav Mesh Agent component to the capsule:

If you want to know more about the Nav Mesh Agent you can visit the Unity docs:

And now, in order to move the capsule, let’s create a new Player script and attach it:

Then, to be able to handle the Nav Mesh Agent, let’s open the script and include the AI library to the script:

Now, let’s create a new private NavMeshAgent to store a reference of the component in the player:

Then, in the Start method, let’s initialize the variable by using the GetComponent method and null-checking it to follow best practices:

Now, to move the player, let’s check the left click input inside the Update method by using the GetMouseButtonDown method:

When the game receives a left click input we’ll need to create a new ray using the ScreenPointToRay method from the main camera. This method will allow us to send a ray from the camera to a screen point that will be defined by the position of the mouse cursor:

If you want to know more about the ScreenPointToRay method you can visit the Unity docs:

Once the ray is created, let’s check if it hit something (a collider in 100 units of distance) by using the Physics.Raycast method. If the ray hits a collider, we’ll get the result in a RaycastHit variable. This variable will contain information about the hit and we’ll use it to set the destination of the Nav Mesh Agent by using its SetDestination method:

If you want to know more about the Physics.Raycast method you can visit the Unity docs:

This way, every time that we input a left-click, we’ll be able to move the capsule to the cursor position:

You’ll notice that the capsule can pass through the gameobjects in the scene. So, if you want to make sure that the NavMesh includes the obstacles in the scene, select all of them:

Once selected, click in the Static checkbox at the top right of the inspector. This will make the gameobjects static and its children:

Finally, to update the NavMesh, open the Navigation window again and click on the Clear button at the left. Then, click on the Bake button again and you’ll notice the new NavMesh in your Scene view:

If we run the game with Unity, we’ll notice that the capsule finds a path that doesn’t collides with the static gameobjects to reach its destination:

And that’s it, we can point and click to move the player with Unity! :D. I’ll see you in the next post, where I’ll be showing how to use the animation system of Unity to move the player with its respective 3D model.

If you want to know more about me, feel free to connect with me on LinkedIn or visit my website :D

--

--

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
Fernando Alcantara Santana

Fernando Alcantara Santana

93 Followers

A passionate computer technology engineer and Unity developer that is always looking to grow in every aspect of life :).