---
title: "Unity NavMesh"
content_type: tutorial
url: "https://learn.unity.com/course/beginner-ai-pathfinding/tutorial/unity-navmesh?language=en"
language: en
container_type: course
container_title: "Getting Started with AI Pathfinding"
container_url: "https://learn.unity.com/course/beginner-ai-pathfinding.md?language=en"
standalone_markdown_url: "https://learn.unity.com/tutorial/unity-navmesh.md?language=en"
next_title: "NavMesh Baking"
next_url: "https://learn.unity.com/course/beginner-ai-pathfinding/tutorial/navmesh-baking?language=en"
next_markdown_url: "https://learn.unity.com/course/beginner-ai-pathfinding/tutorial/navmesh-baking.md?language=en"
difficulty: beginner
duration_minutes: 35
unity_version:
  - "2017.3"
supported_unity_versions:
  - name: "2017.3"
    url: "https://learn.unity.com/tutorial/unity-navmesh.md?language=en&version=2017.3"
last_updated: "2026-08-04T14:23:02.000Z"
---

# Unity NavMesh

Learn to build AI pathfinding with Unity NavMesh components in a video tutorial produced by Brackeys.

Learn how to create AI pathfinding using the Unity NavMesh components!
This video was produced by Brackeys.

## Materials

- [NavMesh Tutorial Project Assets](https://github.com/Brackeys/NavMesh-Tutorial)
- [NavMesh-Tutorial-master.zip](https://unity-connect-prd.storage.googleapis.com/20201207/38b9b6de-97ce-4716-81a5-29b2f3669a71/NavMesh-Tutorial-master.zip)

## 1. Basics

Learn how to create AI pathfinding using the Unity NavMesh components!

This video was produced by Brackeys.

[Video](http://www.youtube.com/watch?v=CHV1ymlw-P8)

Download the Example Project [here](https://github.com/Brackeys/NavMesh-Tutorial).

## PlayerController

```csharp
using UnityEngine;
using UnityEngine.AI;

public class PlayerController : MonoBehaviour {

    public Camera cam;

    public NavMeshAgent agent;

    // Update is called once per frame
    void Update () 
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = cam.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit))
            {
                agent.SetDestination(hit.point);
            }
        }
    }
}
```

## 2. Making it Dynamic

Learn how to update your NavMesh at runtime!

This video was produced by Brackeys.

[Video](http://www.youtube.com/watch?v=FkLJ45Pt-mY)

## ObstacleAnimation

```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObstacleAnimation : MonoBehaviour {

    public float speed = .2f;
    public float strength = 9f;

    private float randomOffset;

    // Use this for initialization
    void Start () {
        randomOffset = Random.Range(0f, 2f);
    }

    // Update is called once per frame
    void Update () {
        Vector3 pos = transform.position;
        pos.x = Mathf.Sin(Time.time * speed + randomOffset) * strength;
        transform.position = pos;
    }
}
```

## LevelGenerator

```csharp
using UnityEngine;

public class LevelGenerator : MonoBehaviour {

        public NavMeshSurface surface;
    public int width = 10;
    public int height = 10;

    public GameObject wall;
    public GameObject player;

    private bool playerSpawned = false;

    // Use this for initialization
    void Start () {
        GenerateLevel();
                surface.BuildNavMesh();
    }

    // Create a grid based level
    void GenerateLevel()
    {
        // Loop over the grid
        for (int x = 0; x <= width; x+=2)
        {
            for (int y = 0; y <= height; y+=2)
            {
                // Should we place a wall?
                if (Random.value > .7f)
                {
                    // Spawn a wall
                    Vector3 pos = new Vector3(x - width / 2f, 1f, y - height / 2f);
                    Instantiate(wall, pos, Quaternion.identity, transform);
                } else if (!playerSpawned) // Should we spawn a player?
                {
                    // Spawn the player
                    Vector3 pos = new Vector3(x - width / 2f, 1.25f, y - height / 2f);
                    Instantiate(player, pos, Quaternion.identity);
                    playerSpawned = true;
                }
            }
        }
    }
}
```

## 3. Animated Character

Learn how to link together surfaces and how to add an animated character to our Agent.

This video was produced by Brackeys.

[Video](http://www.youtube.com/watch?v=blPglabGueM)

## PlayerController

```csharp
using UnityEngine;
using UnityEngine.AI;
using UnityStandardAssets.Characters.ThirdPerson;

public class PlayerController : MonoBehaviour {

    public Camera cam;

    public NavMeshAgent agent;

    public ThirdPersonCharacter character;

    void Start()
    {
        agent.updateRotation = false;
    }

    // Update is called once per frame
    void Update () 
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = cam.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit))
            {
                agent.SetDesitnation(hit.point);
            }
        }

        if (agent.remainingDistance > agent.stoppingDistance)
        {
            character.Move(agent.desiredVelocity, false, false);
        }
        else
        {
            character.Move(Vector3.zero, false, false);
        }

    }
}
```
