Hello! Welcome back.
In our last lesson, we set up a dynamic third-person camera that follows and orbits our character, giving us a great view of the action. Now that we can see our character properly, it's time to bring them to life by giving them the ability to move around the world.
This lesson focuses on a classic and intuitive control scheme for simulation games. We will enable point-and-click character movement using Unity's NavMesh system. When you're done, you'll be able to click anywhere on the ground, and your character will automatically find a path and walk there, even navigating around obstacles.
This is a foundational piece of gameplay. We'll leverage Unity's powerful built-in pathfinding tools, which handle all the complex calculations for us, allowing us to focus on the game logic.
Let's begin.
1. Preparing the World with a NavMesh
Before our character can navigate the world, we need to tell Unity which parts of the world are walkable. We do this by generating a Navigation Mesh (or NavMesh), which is a data structure that represents the traversable areas of your scene.
Step 1: Install the AI Navigation Package
Unity's NavMesh tools are part of the AI Navigation package. Let's make sure it's installed.
- Go to Window > Package Manager.
- Select Packages: Unity Registry from the dropdown at the top-left.
- In the search bar, type
AI Navigation. - Select the package from the list and click Install if it's not already installed.

Step 2: Bake the NavMesh
"Baking" is the process of generating the NavMesh data from your scene's geometry.
Build Your Diablo-Like Top Down Controller in Unity 3D with NavMesh
This video provides a clear, step-by-step guide on how to prepare your scene and bake a NavMesh. We will follow its process.
Watch from 01:02 to 03:23. Pay close attention to these three key actions: Creating a layer for your ground objects. Marking your ground GameObjects as Navigation Static. Adding a NavMesh Surface component and clicking the Bake button.
To summarize and apply the video's steps to our project:
- Create a "Ground" Layer: Go to Edit > Project Settings > Tags and Layers. Create a new Layer and name it
Ground. - Assign the Layer: Select all the GameObjects that make up your walkable surfaces (floors, terrain, etc.). In the Inspector, set their Layer to
Ground. If prompted, choose "Yes, change children". - Mark as Navigation Static: With the ground objects still selected, go to the top-right of the Inspector, click the Static dropdown, and check Navigation Static. This tells the NavMesh system to include these objects in its calculations.
- Create a NavMesh Surface: Create a new empty GameObject in your scene and name it
Navigation. Add the NavMesh Surface component to it. - Configure and Bake:
- In the
NavMesh Surfacecomponent, under Object Collection, changeInclude Layersfrom "Everything" to just theGroundlayer you created. - Click the Bake button.
- In the
You should now see a blue overlay on all your walkable surfaces in the Scene view. This is your NavMesh! The character will only be able to walk on these blue areas. If you have any static obstacles like walls or furniture, you can also mark them as Navigation Static and rebake; the NavMesh will automatically create paths around them.

2. Making the Character a NavMesh Agent
Now that the world has a navigation map, we need to give our character the ability to read that map. This is done by adding the NavMesh Agent component.
- Open your main character prefab for editing.
- Select the root GameObject of the prefab.
- Click Add Component and search for
NavMesh Agent. Add it.
The NavMesh Agent component has several important properties that define how the character moves.
Build Your Diablo-Like Top Down Controller in Unity 3D with NavMesh
This segment of the video shows how to add the NavMesh Agent component and what its main properties do. It's important to match these to your character's size.
Watch from 06:34 to 07:51. Focus on how the Radius, Height, and Base Offset of the agent are adjusted to fit the character model.
Configure the agent's properties in the Inspector:
- Radius: Adjust the radius of the green cylinder to roughly match your character's width. A value like
0.3is often a good start. - Height: Adjust this to match your character's height.
- Base Offset: Use this to shift the green cylinder up or down so its bottom is aligned with your character's feet.
- Speed: This is the maximum movement speed. Let's set it to
3.5for now. - Angular Speed: The rotation speed in degrees per second. A high value like
720will make turning sharp and responsive. - Acceleration: How quickly the agent reaches its max speed. A high value like
20makes movement feel immediate.
Save your prefab. When you run the game, the character now has a NavMesh Agent ready for commands.
3. Scripting the Point-and-Click Movement
This is where we write the code to tie everything together. We'll create a script that does the following:
- Listens for a mouse click using the Input System we set up in the last lesson.
- Casts a ray from the camera to the mouse position on the screen.
- If the ray hits the "Ground" layer, it gets the 3D coordinate of that point.
- It tells the
NavMesh Agentto move to that coordinate.
Step 1: Create the Movement Script
In your Scripts folder, create a new C# Script named PlayerMovementController. Attach this script to your character prefab. Open it in your code editor.
First, we need to include the UnityEngine.AI and UnityEngine.InputSystem namespaces. We'll also define variables to hold references to our components and settings.
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.InputSystem;
[RequireComponent(typeof(NavMeshAgent))]
public class PlayerMovementController : MonoBehaviour
{
private NavMeshAgent agent;
private Camera mainCamera;
// A LayerMask is used to filter raycasts to specific layers.
[SerializeField] private LayerMask clickableLayer;
private void Awake()
{
// Get references to components on this GameObject
agent = GetComponent<NavMeshAgent>();
mainCamera = Camera.main;
}
}
The [RequireComponent(typeof(NavMeshAgent))] attribute is a good practice; it ensures that this script can only be attached to a GameObject that also has a NavMeshAgent.
Step 2: Handle Player Input
We'll use the same Input Actions asset from the last lesson. Since you already have a Player action map, we just need to add a new action for clicking.
Click To Move | Unity RPG Tutorial #1
This article demonstrates how to set up the Input System for click-to-move, which is exactly what we need. We'll follow its method for creating the input action.
Follow the steps in the video from 01:12 to 01:50. In your existing GameControls asset, open the Player action map and create a new Action. Name it ClickToMove. For the binding, add the Left Mouse Button path as shown.
After creating the ClickToMove action and saving the asset, we'll write the code to listen for it. A common approach is to create a method that is automatically called by the PlayerInput component when the action is triggered.
Add the following method to your PlayerMovementController.cs script:
public void OnClickToMove(InputValue value)
{
// Check if the input value represents a press.
if (value.isPressed)
{
// Get the mouse position from the input system.
Vector2 mousePosition = Mouse.current.position.ReadValue();
// Create a ray from the camera through the mouse position.
Ray ray = mainCamera.ScreenPointToRay(mousePosition);
// Perform the raycast.
if (Physics.Raycast(ray, out RaycastHit hitInfo, 100f, clickableLayer))
{
// If we hit a valid point, set it as the agent's destination.
agent.SetDestination(hitInfo.point);
}
}
}
Explanation:
OnClickToMove: ThePlayerInputcomponent on your character will look for methods namedOn+[ActionName]. Since our action isClickToMove, it finds this method.ScreenPointToRay: This is the core function that converts a 2D screen coordinate (the mouse position) into a 3D ray originating from the camera.Physics.Raycast: This function projects the ray into the scene. It returnstrueif it hits a collider.out RaycastHit hitInfo: If the raycast is successful, this variable is filled with information about the hit, including the exact 3D point (hitInfo.point).100f: The maximum distance the ray will travel.clickableLayer: This is crucial. It ensures our raycast only interacts with objects on the specified layer (ourGroundlayer).agent.SetDestination(hitInfo.point): This is the magic command. You give the NavMesh Agent a destination, and it handles all the pathfinding to get there.
Step 3: Connect Input in Unity Editor
Now, let's wire this up in Unity.
- Select your character prefab.
- If it doesn't already have one from the last lesson, add the
Player Inputcomponent. - Set the Actions field to your
GameControlsasset. - Set the Behavior to Invoke Unity Events.
- Expand the Events > Player foldout. You will see your
ClickToMoveaction listed. - Click the
+icon to add a new listener. - Drag the
PlayerMovementControllercomponent (from your character itself) into the object slot. - From the function dropdown, select PlayerMovementController > OnClickToMove (InputValue).
- Finally, in the
PlayerMovementControllercomponent, set the Clickable Layer property to yourGroundlayer.
Your Inspector setup should look something like this:
This setup connects the mouse click action to our script's OnClickToMove method.
You're all set! Press Play, and click anywhere on the ground. Your character should now walk to that point.
Conclusion
Excellent work! You've implemented a robust point-and-click movement system, a cornerstone of many simulation and role-playing games. By leveraging Unity's NavMesh system, you've enabled complex pathfinding with minimal code, allowing your character to navigate the world intelligently.
Key Takeaways:
- The AI Navigation package is required for pathfinding in Unity.
- A NavMesh is generated by "baking" static, walkable geometry in your scene.
- The NavMesh Agent component gives a GameObject the ability to use the NavMesh for pathfinding.
- Raycasting is the technique used to convert a 2D mouse click on the screen into a 3D point in the game world.
- The
agent.SetDestination()method is the simple yet powerful command that tells the agent where to go.
With your character now able to move around the scene, they are no longer just a static model. However, they still lack motivation and internal state. In the next module, we'll shift our focus back to the Rust backend to start building the character's "brain." Our first step will be to implement a needs system in Rust with four needs (hunger, energy, social, and fun) that decay over time, which will eventually drive their autonomous behavior.