How to Make an NPC Chase the Player in Unity Using NavMesh
In this tutorial, we’ll use Unity’s NavMesh system to create an NPC that chases the Player. NavMesh provides efficient pathfinding and obstacle avoidance, making it ideal for AI behavior in 3D environments.
Step 1: Setting Up the Scene
Before implementing NavMesh, we need a proper scene setup:
- Create a new 3D project in Unity.
- Add a Plane to serve as the ground.
- Add a Cube or any 3D model to represent the Player. Name it Player.
- Add another Cube or 3D model to represent the NPC. Name it NPC.
- Place the Player and NPC at different positions on the Plane.
Step 2: Baking the NavMesh
To use Unity’s NavMesh system, we need to bake the navigation mesh:
- Select the ground Plane in the Hierarchy.
- In the Inspector, click Add Component and add a NavMeshSurface.
- In the NavMeshSurface component, ensure the Plane is marked as a walkable surface.
- Click the Bake button in the NavMeshSurface component to generate the NavMesh.
- Optional: If you have obstacles (e.g., walls), ensure they have NavMeshObstacle components to block NPC movement.
Step 3: Adding a NavMeshAgent
Now, we’ll prepare the NPC to use Unity’s pathfinding:
- Select the NPC in the Hierarchy.
- In the Inspector, click Add Component and add a NavMeshAgent.
- Adjust the NavMeshAgent properties as needed, such as speed, acceleration, and stopping distance.
Step 4: Writing the Chase Script
Next, we’ll create a script to make the NPC chase the Player:
- Right-click in the Project panel and select Create > C# Script. Name it NPCChase.
- Double-click the script to open it in your code editor.
- Replace the default code with the following:
using UnityEngine;
using UnityEngine.AI;
public class NPCChase : MonoBehaviour
{
public Transform player; // Reference to the player's position
private NavMeshAgent agent; // Reference to the NavMeshAgent
void Start()
{
// Get the NavMeshAgent component
agent = GetComponent<NavMeshAgent>();
}
void Update()
{
if (player != null)
{
// Set the agent's destination to the player's position
agent.SetDestination(player.position);
}
}
}
Step 5: Assigning the Script and Player Reference
Finally, we’ll assign the script and set up the Player reference:
- Select the NPC in the Hierarchy.
- Drag and drop the NPCChase script onto the NPC.
- In the Inspector, locate the Player field in the script.
- Drag the Player GameObject from the Hierarchy into the Player field.
Step 6: Testing the Scene
To test your setup:
- Press the Play button in the Unity Editor.
- Move the Player around (e.g., using keyboard or controller input).
- Observe the NPC dynamically pathfinding and chasing the Player, avoiding obstacles where applicable.
Optional: Customizing Behavior
You can further refine your NPC’s behavior:
- Stopping Distance: Adjust the NavMeshAgent’s Stopping Distance to make the NPC stop at a certain range.
- Obstacle Avoidance: Ensure obstacles have NavMeshObstacle components for accurate avoidance.
- Animations: Use animations to make the NPC movement more realistic by triggering animations based on speed.
Conclusion
We’ve created an NPC that dynamically chases the Player using Unity’s NavMesh system. This approach is robust and can be easily expanded for more complex AI behaviors. Experiment with different settings to tailor the NPC’s pathfinding to your game’s needs.