AI Basics (NavMesh) In Unity

Last Updated : 4 May, 2026

NavMesh (Navigation Mesh) is a simplified map of your scene that tells AI where it can walk. Unity's NavMesh system calculates paths and moves characters around obstacles automatically.

  • AI moves around walls and obstacles
  • Finds shortest path automatically
  • No need for complex pathfinding code
NavMesh-In-Unity
NavMesh In Unity

1. Setting Up NavMesh

Step 1: Open Navigation window: Window - AI - Navigation.

Step 2: Mark objects as walkable or not

  • Select floor/ground objects
  • Check "Navigation Static" in Inspector

Step 3: Mark obstacles (walls, trees, rocks)

  • Select obstacle objects.
  • Check "Navigation Static".
  • Set Navigation Area to "Not Walkable".

Step 4: Bake the NavMesh.

  • Navigation window - Bake tab.
  • Click "Bake" button.

The blue area appears in Scene view showing where AI can walk.

2. Adding NavMesh Agent to Enemy

The NavMesh Agent component moves the enemy along the calculated path.

Step 1: Select your enemy GameObject.

Step 2: Add Component - NavMeshAgent.

Important settings:

SettingValueWhat it does
Speed3.5Controls movement speed
Angular Speed120Controls how fast the agent turns
Acceleration8Determines how quickly it reaches full speed
Stopping Distance1Distance from target where the agent stops
Radius0.5Defines size for obstacle avoidance
Height2Sets the height of the agent

3. Making Enemy Follow Player

Example:

C#
using UnityEngine;
using UnityEngine.AI;
public class EnemyAI : MonoBehaviour
{
    public Transform player;
    private NavMeshAgent agent;
    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
        
        // Find player by tag if not assigned
        if (player == null)
        {
            player = GameObject.FindGameObjectWithTag("Player").transform;
        }
    }
    void Update()
    {
        // Set enemy destination to player position
        agent.SetDestination(player.position);
    }
}

Output:

EnemyAI-In-Unity
EnemyAI In Unity

agent.SetDestination() tells the enemy where to go. NavMeshAgent automatically finds the path and moves there.

FeatureNavMeshSimple Movement
PathfindingAutomatic pathfindingManual movement logic required
Obstacle HandlingAutomatically avoids obstaclesRequires custom handling
Use CaseBest for complex environments (AI, enemies)Best for simple/open areas
PerformanceSlightly heavierLightweight and faster
ImplementationEasier for navigation logicRequires more manual coding

Common NavMesh Issues

  • Enemy not moving check NavMeshAgent is attached and NavMesh is baked.
  • Enemy stuck on walls increase Radius in NavMeshAgent.
  • Enemy goes through obstacles re-bake NavMesh after scene changes.
  • No blue area visible open Navigation - Bake and generate NavMesh.
Comment
Article Tags:

Explore