0
0
Unityframework~5 mins

NavMesh baking in Unity

Choose your learning style9 modes available
Introduction

NavMesh baking creates a map that helps game characters find paths around obstacles easily.

When you want characters to walk around walls and furniture without getting stuck.
When you need enemies to chase the player smoothly through a level.
When you want to save time by automatically creating walkable areas instead of placing paths manually.
When designing levels where characters must navigate complex terrain like stairs or ramps.
Syntax
Unity
1. Open Unity Editor.
2. Select the GameObject(s) with geometry to include in navigation.
3. Go to Window > AI > Navigation.
4. In the Navigation window, mark objects as Navigation Static.
5. Adjust settings like Agent Radius, Height, and Step Height.
6. Click the Bake button to generate the NavMesh.

NavMesh baking is done inside the Unity Editor, not by writing code.

Make sure objects you want characters to walk on are marked as Navigation Static.

Examples
This bakes a NavMesh for a simple room so characters can walk inside it.
Unity
Select floor and walls > Mark as Navigation Static > Open Navigation window > Bake
Changes how close characters can get to obstacles before avoiding them.
Unity
Adjust Agent Radius to 0.5 > Bake
Allows characters to jump or climb between disconnected areas.
Unity
Add OffMeshLinks between platforms > Bake
Sample Program

This script bakes the NavMesh at the start of the game if the NavMeshSurface component is attached.

Unity
// This example shows how to trigger NavMesh baking via script in Unity
using UnityEngine;
using UnityEngine.AI;

public class NavMeshBakeTrigger : MonoBehaviour
{
    void Start()
    {
        NavMeshSurface surface = GetComponent<NavMeshSurface>();
        if (surface != null)
        {
            surface.BuildNavMesh();
            Debug.Log("NavMesh baked successfully.");
        }
        else
        {
            Debug.Log("NavMeshSurface component missing.");
        }
    }
}
OutputSuccess
Important Notes

NavMesh baking can take time for large or complex scenes.

Always test navigation after baking to ensure characters move correctly.

You can update the NavMesh at runtime using scripts if your level changes.

Summary

NavMesh baking creates a walkable map for characters in Unity.

It is done in the Unity Editor or via scripts using NavMeshSurface.

Proper baking helps characters avoid obstacles and move smoothly.