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:

  1. Create a new 3D project in Unity.
  2. Add a Plane to serve as the ground.
  3. Add a Cube or any 3D model to represent the Player. Name it Player.
  4. Add another Cube or 3D model to represent the NPC. Name it NPC.
  5. 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:

  1. Select the ground Plane in the Hierarchy.
  2. In the Inspector, click Add Component and add a NavMeshSurface.
  3. In the NavMeshSurface component, ensure the Plane is marked as a walkable surface.
  4. Click the Bake button in the NavMeshSurface component to generate the NavMesh.
  5. 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:

  1. Select the NPC in the Hierarchy.
  2. In the Inspector, click Add Component and add a NavMeshAgent.
  3. 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:

  1. Right-click in the Project panel and select Create > C# Script. Name it NPCChase.
  2. Double-click the script to open it in your code editor.
  3. 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:

  1. Select the NPC in the Hierarchy.
  2. Drag and drop the NPCChase script onto the NPC.
  3. In the Inspector, locate the Player field in the script.
  4. Drag the Player GameObject from the Hierarchy into the Player field.

Step 6: Testing the Scene

To test your setup:

  1. Press the Play button in the Unity Editor.
  2. Move the Player around (e.g., using keyboard or controller input).
  3. 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.

Links
Unity 6