Creating a pathfinding agent
Overview
This tutorial will guide you through creating a pathfinding agent that patrols to a specific point using dynamic navigation meshes (NavMesh).
Building the scene
-
In Scene Explorer, click +, then choose 3D Object > Cube to add a cube to the scene.
-
Repeat this process to create multiple cubes, then modify them to form wall-like obstacles for pathfinding.
-
Add a cylinder to one end of the path. This will serve as your pathfinding agent.
-
Place another cube on the other end of the path to act as the target point.
Adding to the agent
-
Select Cylinder and open its Properties window.
-
In the Properties window, click Add Components and select the following:
-
NPC Behaviour: Allows the cylinder to act as a non-character player (NPC) and function as an agent in this case.
-
NPC Patrol: Configures the agent's patrol behavior.
-
NavMesh Dynamic Builder: Creates dynamic navigation meshes.
-
-
In the NPC Behaviour component, drag Cylinder into Default Behaviour.
-
In the NPC Patrol component, click + Item and drag Cube into Patrol Point.
Configuring the target
Select the target cube and open its Properties window. Ensure its collider is disabled to allow the agent (cylinder) to reach the target point (cube) smoothly and interact with it during pathfinding.
Setting up navigation
-
Open Game Settings, and choose Navigation.
-
Toggle Show NavMesh to make the navigable areas visible. Walkable surfaces will be highlighted in blue. Areas around obstacles will remain black, indicating impassable zones.
-
To adjust the navigable areas, modify the Size and Center values in the cylinder's NavMesh Dynamic Builder component.
Testing the pathfinding agent
Enter Play Mode to test your setup. Observe as the cylinder (agent) finds the shortest route to the patrol point (target cube).