Working with NavMeshAgent in Unity

In Unity, NavMeshAgent is a powerful component that allows you to implement pathfinding and navigation for characters or objects in your game. It utilizes Unity's built-in navigation system to calculate paths on a NavMesh surface. In this tutorial, we'll cover the basics of setting up and using NavMeshAgent with code examples.

Prerequisites

  • Basic understanding of Unity interface and game development concepts.
  • Unity installed on your system (version 2017 or later).

Setting up NavMesh in Unity

Before we dive into using NavMeshAgent, we need to set up a NavMesh in our scene.

  1. Create a NavMesh Surface:

    • Go to 'GameObject -> 3D Object -> NavMesh Surface'. This creates a new GameObject with a 'NavMeshSurface' component attached.
    • Adjust the settings of the NavMesh Surface component as needed for your scene.
  2. Bake NavMesh:

    • Click on the 'Bake' button in the 'NavMesh Surface' component to generate the NavMesh for your scene.
    • Ensure that your environment (terrain, obstacles, etc.) is configured properly to allow for accurate pathfinding.

Using NavMeshAgent in Code

Now, let's write some code to make a GameObject move using NavMeshAgent.

Step 1: Attach NavMeshAgent to GameObject

  • Attach a NavMeshAgent component to your GameObject in the Unity editor. You can do this by selecting the GameObject, clicking 'Add Component', and then adding 'NavMeshAgent'.

Step 2: Write Script for Movement

  • Create a new C# script (e.g., 'PlayerController.cs') and attach it to the GameObject with the NavMeshAgent component. Open the script in your preferred code editor.
using UnityEngine;
using UnityEngine.AI;

public class PlayerController : MonoBehaviour
{
    private NavMeshAgent navMeshAgent;

    void Start()
    {
        navMeshAgent = GetComponent<NavMeshAgent>();
    }

    void Update()
    {
        // Check for player input or any other conditions to trigger movement
        if (Input.GetMouseButton(0)) // Example: Move towards mouse click position
        {
            MoveToClickPosition();
        }
    }

    void MoveToClickPosition()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit))
        {
            // Move NavMeshAgent towards the clicked position
            navMeshAgent.SetDestination(hit.point);
        }
    }
}

Step 3: Set Up Scene and Test

  • Ensure that your GameObject with NavMeshAgent and the attached script is present in the scene.
  • Hit Play in the Unity Editor and observe the behavior.
  • Click anywhere on the 'NavMesh' surface, and the GameObject should move toward that position.

Summary

In this tutorial, you learned how to set up and use NavMeshAgent for pathfinding and navigation in Unity. By following these steps and code examples, you can implement basic movement for characters or objects in your game using Unity's built-in navigation system.

Links
Unity 6