Loading...
Loading...
Add AI navigation in Unity 6: bake a NavMesh with the AI Navigation package (NavMeshSurface), move agents with NavMeshAgent.SetDestination, and handle dynamic obstacles. Use when setting up pathfinding, making an enemy chase the player, baking navigation, or when the user mentions NavMesh, NavMeshAgent, NavMeshSurface, NavMeshObstacle, or Unity pathfinding.
npx skill4agent add gamedev-skills/awesome-gamedev-agent-skills unity-navmeshVersion 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
NavMeshObstacleNavMeshSurfaceUnityEngine.AI.NavMeshAgentgame-aiunity-physicscom.unity.ai.navigationNavMeshModifierNavMeshAgentSetDestination(targetPos)remainingDistancepathPendingNavMeshObstacleNavMeshPath.statususing 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;
}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
}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();// 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.NavMeshSurfaceNavMesh.SamplePositionNavMeshObstaclesurface.BuildNavMesh()SetDestinationNavMeshAgentavoidancePriorityhttps://docs.unity3d.com/Packages/com.unity.ai.navigation@2.0/manual/index.htmlScriptReference/AI.NavMeshAgentScriptReference/AI.NavMeshgame-aiunity-csharp-scriptingtower-defensefps-shooter