$npx -y skills add thedivergentai/GD-Agentic-Skills --skill godot-ai-navigationExpert blueprint for AI pathfinding (tower defense, RTS, stealth) using NavigationAgent2D/3D, NavigationServer, avoidance, and dynamic navigation mesh generation. Use when implementing enemy AI, NPC movement, or obstacle avoidance. Keywords NavigationAgent2D, NavigationRegion2D,
| 1 | ## Godot 4.7 Baseline |
| 2 | |
| 3 | - Expert patterns in this skill target **Godot 4.7+** (stable, 2026-06-18). |
| 4 | - Consult the [Godot 4.7 migration guide](https://docs.godotengine.org/en/4.7/tutorials/migrating/upgrading_to_godot_4.7.html) when upgrading projects from 4.6. |
| 5 | - **NEVER** assume 4.6 defaults (stretch mode, audio area_mask, RichTextLabel percent flags) without checking 4.7 migration notes. |
| 6 | |
| 7 | # Navigation & Pathfinding |
| 8 | |
| 9 | NavigationServer-powered pathfinding with avoidance and dynamic obstacles define robust AI movement. |
| 10 | |
| 11 | ## Available Scripts |
| 12 | |
| 13 | ### [dynamic_nav_manager.gd](scripts/dynamic_nav_manager.gd) |
| 14 | Expert runtime navigation mesh updates for moving platforms. |
| 15 | |
| 16 | ### [server_navigation_setup.gd](scripts/server_navigation_setup.gd) |
| 17 | Low-level `NavigationServer3D` usage (bypassing nodes). Creates maps, regions, and registers navmeshes entirely via RID for maximum performance. |
| 18 | |
| 19 | ### [async_dynamic_baking.gd](scripts/async_dynamic_baking.gd) |
| 20 | Expert logic for `bake_from_source_geometry_data_async`. Parses geometry on main thread then bakes in background to prevent procedural-gen stutters. |
| 21 | |
| 22 | ### [memory_optimized_queries.gd](scripts/memory_optimized_queries.gd) |
| 23 | Pattern for reusing `NavigationPathQueryParameters3D` and `NavigationPathQueryResult3D` objects to prevent frame-by-frame GC allocations. |
| 24 | |
| 25 | ### [terrain_cost_manager.gd](scripts/terrain_cost_manager.gd) |
| 26 | Controlling pathfinding logic using `region_set_enter_cost` and `region_set_travel_cost` to define high-penalty areas (mud, fire, water). |
| 27 | |
| 28 | ### [low_level_avoidance.gd](scripts/low_level_avoidance.gd) |
| 29 | Direct RVO (Reciprocal Velocity Obstacles) registration using server-side agents. Uses `NavigationServer3D.agent_set_avoidance_callback` for high-performance avoidance. |
| 30 | |
| 31 | ### [moving_obstacle_server.gd](scripts/moving_obstacle_server.gd) |
| 32 | Dynamic obstacle registration (e.g. for projectiles or rolling hazards) that push RVO agents away without full navmesh baking. |
| 33 | |
| 34 | ### [nav_link_traversal.gd](scripts/nav_link_traversal.gd) |
| 35 | Advanced handling of `NavigationLink3D` for jumps, teleports, and elevators. Detects link traversal and overrides standard movement. |
| 36 | |
| 37 | ### [layer_mask_navigation.gd](scripts/layer_mask_navigation.gd) |
| 38 | Architecture for multi-type navigation (e.g. Flying vs Walking vs Swimming) using 32-bit navigation layers and bitmasks. |
| 39 | |
| 40 | ### [agent_stuck_detection.gd](scripts/agent_stuck_detection.gd) |
| 41 | Robust AI recovery logic. Detects distance-over-time stalls and triggers jitter recovery or path recalculation. |
| 42 | |
| 43 | ### [group_avoidance_formations.gd](scripts/group_avoidance_formations.gd) |
| 44 | Coordinating crowd behavior. Strategies for avoiding individual agent clumping by using leader-relative target offsets. |
| 45 | |
| 46 | ## NEVER Do in Navigation & Pathfinding |
| 47 | |
| 48 | - **NEVER set `target_position` before awaiting physics frame** — NavigationServer not ready in `_ready()`? Path fails silently. MUST `call_deferred()` then `await get_tree().physics_frame`. |
| 49 | - **NEVER use `NavigationRegion2D.bake_navigation_polygon()` at runtime** — Synchronous baking freezes game for 100+ ms. Use `NavigationServer.bake_from_source_geometry_data_async()` for stutter-free updates. |
| 50 | - **NEVER forget to check `is_navigation_finished()`** — Calling `get_next_path_position()` after reaching target = stale path, AI walks to old position. |
| 51 | - **NEVER use `avoidance_enabled` without setting radius** — Default radius = 0, agent passes through others. Set `nav_agent.radius = collision_shape.radius` for proper avoidance. |
| 52 | - **NEVER poll `target_position` every frame for chase AI** — Setting target 60x/sec = path recalculation spam. Use timer (0.2s intervals) or distance threshold for updates. |
| 53 | - **NEVER assume path exists** — Target unreachable (blocked by walls)? `get_next_path_position()` returns invalid. Check `is_target_reachable()` or validate path length. |
| 54 | - **NEVER use heavy node-based navigation for thousands of simple entities** — Use `NavigationServer3D/2D` RIDs directly to bypass node overhead. |
| 55 | - **NEVER call `get_path()` every frame** — Use `query_path()` with reused `NavigationPathQueryResult` objects to prevent massive heap allocation and GC pressure. |
| 56 | - **NEVER leave 'enter_cost' at 0 for high-penalty areas** — Use costs to make AI prefer logical paths (roads over water) instead of just shortest geometric distance. |
| 57 | - **NEVER ignore `agent_set_avoidance_callback`** — Always use the callback for safe velocity computation to avoid synchronization issues and "jittery" movement. |
| 58 | |
| 59 | --- |
| 60 | |
| 61 | ### 2D Navigation |
| 62 | |
| 63 | ```gdscript |
| 64 | # Scene structure: |
| 65 | # Node2D (Level) |
| 66 | # ├─ NavigationRegion2D |
| 67 | # │ └─ Polygon2D (draw walkable area) |
| 68 | # |