unity-navmesh
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseUnity NavMesh (AI Navigation)
Unity NavMesh(AI导航)
Give NPCs pathfinding in Unity 6: bake walkable surfaces and move agents around obstacles.
Targets Unity 6 (6000.0 LTS) with the AI Navigation package 2.x.
Version trap (Unity 2022+/6): the old built-in Navigation window (Object/Bake tabs) is gone. Baking is now component-based via the AI Navigation package (): add a NavMeshSurface to your level geometry and click Bake. The runtimecom.unity.ai.navigation/NavMeshAgentAPI stays in built-inNavMesh.UnityEngine.AI
在Unity 6中为NPC添加寻路功能:烘焙可行走表面并让智能体绕过障碍物移动。
针对 Unity 6(6000.0 LTS) 及 AI Navigation package 2.x 版本。
版本陷阱(Unity 2022+/6): 旧版内置的 Navigation窗口(Object/Bake标签页)已被移除。现在烘焙通过 AI Navigation包()以组件化方式实现:为关卡几何体添加 NavMeshSurface 组件,点击 Bake 即可。运行时的com.unity.ai.navigation/NavMeshAgentAPI仍保留在内置的NavMesh命名空间中。UnityEngine.AI
When to use
适用场景
- Use when an agent needs to walk/chase/patrol to a target, when baking a navigable surface,
adding dynamic blockers (), or checking whether a destination is reachable.
NavMeshObstacle - Use when the project has the AI Navigation package, a component, or scripts using
NavMeshSurface.UnityEngine.AI.NavMeshAgent
When not to use: the decision logic of when/where to move (FSMs, behaviour trees,
steering) → (this skill is the Unity movement/pathing mechanism). Force-driven or
kinematic movement that isn't pathfinding → .
game-aiunity-physics- 当智能体需要行走/追逐/巡逻到目标点、烘焙可导航表面、添加动态阻挡物(),或检查目标点是否可达时使用。
NavMeshObstacle - 当项目已安装AI Navigation包、包含组件,或脚本中使用
NavMeshSurface时使用。UnityEngine.AI.NavMeshAgent
不适用场景: 何时/何地移动的决策逻辑(有限状态机、行为树、转向控制)→ 请使用(本技能仅为Unity中的移动/寻路机制)。非寻路的力驱动或运动学移动→ 请使用。
game-aiunity-physicsCore workflow
核心工作流程
- Install the AI Navigation package (Package Manager → ).
com.unity.ai.navigation - Bake a surface: select the static level geometry, Add Component → Navigation → NavMesh
Surface, set the agent type/area settings, and click Bake. Re-bake whenever geometry,
s, or agent settings change.
NavMeshModifier - Add a to each moving NPC; its radius/height/speed must match the baked agent type, and it must spawn on the baked mesh.
NavMeshAgent - Drive it from script with ; the agent steers and avoids other agents automatically. Detect arrival with
SetDestination(targetPos)/remainingDistance.pathPending - Handle dynamic blockers with (carving) so closed doors/crates block paths without a full re-bake.
NavMeshObstacle - Verify with the AI Navigation overlay (the baked mesh is drawn in the Scene view) and by
watching agents path around obstacles to the goal; check for unreachable targets.
NavMeshPath.status
- 安装AI Navigation包(Package Manager中搜索)。
com.unity.ai.navigation - 烘焙导航表面: 选择静态关卡几何体,点击添加组件→导航→NavMeshSurface,设置智能体类型/区域参数,然后点击Bake。每当几何体、或智能体参数变化时,需重新烘焙。
NavMeshModifier - 为每个移动NPC添加组件:其半径/高度/速度必须与烘焙时设置的智能体类型匹配,且必须生成在已烘焙的导航网格上。
NavMeshAgent - 通过脚本驱动移动:使用方法;智能体会自动转向并避开其他智能体。可通过
SetDestination(targetPos)/remainingDistance检测是否到达目标。pathPending - 使用处理动态阻挡物(开启Carve功能),这样关闭的门/箱子无需重新烘焙即可阻挡路径。
NavMeshObstacle - 验证效果:通过AI Navigation叠加层(场景视图中会绘制已烘焙的网格)观察智能体绕过障碍物前往目标的路径;可通过检查目标点是否不可达。
NavMeshPath.status
Patterns
实现模式
1. Chase/seek with a NavMeshAgent
1. 使用NavMeshAgent实现追逐/追踪
csharp
using UnityEngine;
using UnityEngine.AI;
[RequireComponent(typeof(NavMeshAgent))]
public class Chaser : MonoBehaviour
{
[SerializeField] private Transform target;
private NavMeshAgent _agent;
private void Awake() => _agent = GetComponent<NavMeshAgent>();
private void Update()
{
if (target) _agent.SetDestination(target.position); // re-path toward the target
}
// Arrived? pathPending guards the first frame before a path exists.
private bool HasArrived() =>
!_agent.pathPending && _agent.remainingDistance <= _agent.stoppingDistance;
}csharp
using UnityEngine;
using UnityEngine.AI;
[RequireComponent(typeof(NavMeshAgent))]
public class Chaser : MonoBehaviour
{
[SerializeField] private Transform target;
private NavMeshAgent _agent;
private void Awake() => _agent = GetComponent<NavMeshAgent>();
private void Update()
{
if (target) _agent.SetDestination(target.position); // 重新规划路径朝向目标
}
// 是否到达?pathPending用于防范路径生成前的第一帧异常。
private bool HasArrived() =>
!_agent.pathPending && _agent.remainingDistance <= _agent.stoppingDistance;
}2. Check reachability before committing
2. 执行移动前检查可达性
csharp
using UnityEngine.AI;
public bool CanReach(NavMeshAgent agent, Vector3 destination)
{
var path = new NavMeshPath();
agent.CalculatePath(destination, path);
return path.status == NavMeshPathStatus.PathComplete; // vs Partial / Invalid
}csharp
using UnityEngine.AI;
public bool CanReach(NavMeshAgent agent, Vector3 destination)
{
var path = new NavMeshPath();
agent.CalculatePath(destination, path);
return path.status == NavMeshPathStatus.PathComplete; // 对比Partial/Invalid状态
}3. Bake at runtime (for procedurally built or streamed levels)
3. 运行时烘焙(适用于程序化生成或流式加载的关卡)
csharp
using Unity.AI.Navigation; // the package namespace (NavMeshSurface)
[SerializeField] private NavMeshSurface surface;
// After spawning level geometry, build the navmesh in code.
public void RebuildNav() => surface.BuildNavMesh();csharp
using Unity.AI.Navigation; // 包的命名空间(NavMeshSurface)
[SerializeField] private NavMeshSurface surface;
// 生成关卡几何体后,通过代码重建导航网格。
public void RebuildNav() => surface.BuildNavMesh();4. Dynamic obstacle that carves the mesh
4. 可切割网格的动态障碍物
csharp
// Add a NavMeshObstacle (Carve = true) to a door/crate. While present it cuts a hole in the
// navmesh so agents route around it; remove/disable it to reopen the path — no re-bake needed.csharp
// 为门/箱子添加NavMeshObstacle组件(开启Carve = true)。当组件存在时,会在导航网格上切割出孔洞,让智能体绕路;移除/禁用组件即可重新开放路径——无需重新烘焙。Pitfalls
常见陷阱
- Looking for the Navigation window — it no longer exists in Unity 6. Use the AI Navigation
package's component + Bake.
NavMeshSurface - Agent doesn't move / warps to origin — it isn't on the baked mesh, or no surface was
baked. Bake the surface and spawn the agent on it (to snap).
NavMesh.SamplePosition - Agent ignores new geometry — the navmesh is baked; runtime-spawned obstacles need a
(carving) or a
NavMeshObstaclere-bake.surface.BuildNavMesh() - every frame is wasteful — for a slow-moving target, re-path on a timer (e.g. every 0.2s) instead of each frame.
SetDestination - Agent radius/height mismatch — if the 's size differs from the baked agent type, it gets stuck in gaps or floats; keep them consistent.
NavMeshAgent - Agents jitter against each other — tune and quality, or use an obstacle for truly static blockers rather than relying on agent avoidance.
avoidancePriority
- 寻找Navigation窗口——Unity 6中已不再存在该窗口。请使用AI Navigation包的组件+Bake功能。
NavMeshSurface - 智能体不移动/瞬移到原点——智能体不在已烘焙的网格上,或未烘焙任何导航表面。请烘焙表面并将智能体生成在网格上(可使用对齐)。
NavMesh.SamplePosition - 智能体无视新增几何体——导航网格是烘焙生成的;运行时生成的障碍物需要添加(开启切割)或调用
NavMeshObstacle重新烘焙。surface.BuildNavMesh() - 每帧调用过于浪费——对于移动缓慢的目标,可设置定时器(例如每0.2秒)重新规划路径,而非每帧执行。
SetDestination - 智能体半径/高度不匹配——如果的尺寸与烘焙时的智能体类型不一致,会导致智能体卡在缝隙中或浮空;请保持两者参数一致。
NavMeshAgent - 智能体相互碰撞抖动——调整和避让质量,或为真正静态的阻挡物添加障碍物组件,而非依赖智能体自身的避让机制。
avoidancePriority
References
参考资料
- Primary docs: AI Navigation package manual
() and
https://docs.unity3d.com/Packages/com.unity.ai.navigation@2.0/manual/index.html,ScriptReference/AI.NavMeshAgent.ScriptReference/AI.NavMesh
- 官方文档:AI Navigation包手册()以及
https://docs.unity3d.com/Packages/com.unity.ai.navigation@2.0/manual/index.html、ScriptReference/AI.NavMeshAgent。ScriptReference/AI.NavMesh
Related skills
相关技能
- — engine-agnostic decision-making (FSM, behaviour trees, steering) that uses this.
game-ai - — the MonoBehaviour structure around the agent.
unity-csharp-scripting - /
tower-defense— genres that compose pathfinding with gameplay.fps-shooter
- ——与引擎无关的决策逻辑(有限状态机、行为树、转向控制),可结合本技能使用。
game-ai - ——围绕智能体的MonoBehaviour结构开发。
unity-csharp-scripting - /
tower-defense——需要将寻路与玩法结合的游戏类型。fps-shooter