Skilllibrary ai-npc-behavior
Implements NPC AI using behavior trees, FSMs, utility AI, GOAP, steering behaviors, and perception systems. Use when building enemy AI, companion logic, NPC patrol/combat routines, pathfinding, or agent decision-making in Unity, Unreal, or Godot.
install
source · Clone the upstream repo
git clone https://github.com/merceralex397-collab/skilllibrary
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/merceralex397-collab/skilllibrary "$T" && mkdir -p ~/.claude/skills && cp -r "$T/13-game-engines-and-creative-tech/ai-npc-behavior" ~/.claude/skills/merceralex397-collab-skilllibrary-ai-npc-behavior && rm -rf "$T"
manifest:
13-game-engines-and-creative-tech/ai-npc-behavior/SKILL.mdsource content
Purpose
Implement NPC decision-making, navigation, and perception systems using proven game AI architectures: behavior trees, finite state machines, utility AI, GOAP, and steering behaviors.
When to use this skill
- Building enemy AI, companion AI, or NPC routines (patrol, combat, idle)
- Implementing pathfinding with NavMesh, A* grid search, or flow fields
- Designing perception systems (sight cones, hearing, awareness levels)
- Creating behavior trees, FSMs, or utility-based scoring for agent decisions
- Integrating AI with engine-specific systems (UE5 AI Controller, Unity NavMeshAgent, Godot NavigationAgent3D)
Do not use this skill when
- The task is purely UI/HUD work — use
insteadgame-ui-hud - The task is about player input handling — use
input-mapping-controller - The task is about general game system design without AI — use
game-design-systems - Building multiplayer netcode for player-controlled characters
Operating procedure
- Identify the AI archetype: classify the NPC role (enemy, companion, ambient, boss) and required behaviors (patrol, chase, attack, flee, interact).
- Select the decision architecture:
- Behavior Tree for complex hierarchical decisions — use Selector (fallback), Sequence (all-must-pass), Decorator (conditional guards), and Leaf (action/condition) nodes. Tick each frame or on-event.
- FSM for NPCs with clear, discrete states — define states (Idle, Patrol, Chase, Attack), transitions with enter/exit/update callbacks.
- Utility AI when NPCs must evaluate many possible actions — score each action using weighted response curves (linear, quadratic, logistic) across multiple axes (health, distance, ammo).
- GOAP for NPCs that plan multi-step sequences — define actions with preconditions and effects, use A* to find cheapest plan to satisfy a goal.
- Implement perception: sight cones via dot-product checks (
), hearing radius viaVector3.Dot(forward, toTarget) > cos(halfAngle)
, awareness levels (unaware → suspicious → alert) with decay timers.Physics.OverlapSphere - Implement navigation: use engine NavMesh (
in Unity,NavMeshAgent.SetDestination()
in UE5,UAITask_MoveTo
in Godot). For grid worlds, implement A* with Manhattan/octile heuristic. Apply path smoothing via funnel algorithm.NavigationAgent3D.target_position - Add steering behaviors for dynamic movement: seek, flee, arrive (deceleration radius), wander (random jitter on circle), flocking (separation + alignment + cohesion weighted sum).
- Wire up the blackboard: use a shared key-value store for behavior tree nodes to read/write data (target reference, last-known position, alert level). In UE5 use
; in Unity use aUBlackboardComponent
on the AI controller.Dictionary<string, object> - Profile and tune: cap behavior tree depth to ~15 nodes for readability, stagger AI ticks across frames to avoid spikes, use LOD-based AI (full logic near player, simplified at distance).
Decision rules
- Prefer behavior trees for complex NPCs with many conditional branches; prefer FSMs for simple 3-5 state actors.
- Use utility AI when the NPC must weigh competing needs simultaneously (e.g., survival games).
- Use GOAP only when NPCs need emergent multi-step planning — it has higher CPU cost.
- Always separate perception from decision-making — perception feeds data to the blackboard, decisions read from it.
- Stagger AI updates: not every NPC needs to tick every frame. Use 0.1–0.5s intervals for non-critical agents.
- Prefer NavMesh over grid A* for 3D environments; use grid A* for 2D or tile-based games.
Output requirements
— which pattern (BT/FSM/Utility/GOAP) and whyAI Architecture
— text description of states or tree structureState/Node Diagram
— sight range, angle, hearing radius, awareness thresholdsPerception Config
— NavMesh bake settings or grid config, agent radius/speedNavigation Setup
— engine-specific code with blackboard integrationImplementation Code
— max concurrent AI agents, tick interval, LOD strategyPerformance Budget
References
- UE5 AI:
,AIController
,UBehaviorTree
,UBlackboardComponentUAIPerceptionComponent - Unity:
,NavMeshAgent
, ML-Agents for learning-based AINavMesh.SamplePosition() - Godot 4:
,NavigationAgent3D
,NavigationServer3D
for perception zonesArea3D - Millington & Funge, AI for Games (behavior trees, steering, pathfinding)
- Orkin, Applying GOAP to Games (GDC, F.E.A.R. AI architecture)
Related skills
— enemy difficulty curves feed AI parametersgame-design-systems
— UE5 behavior trees are often wired in Blueprintsblueprint-patterns
— AI tick budgets and NavMesh query costsperformance-profiling-games
— player input vs AI input abstractioninput-mapping-controller
Failure handling
- If the NPC count exceeds performance budget, reduce tick frequency or switch distant NPCs to simplified FSM.
- If pathfinding fails (no valid path), implement fallback: teleport to nearest valid NavMesh point or enter idle state.
- If behavior tree grows beyond 20+ nodes and becomes unreadable, refactor into sub-trees.
- If the engine lacks a built-in behavior tree (e.g., Godot), recommend
addon or a custom lightweight BT implementation.LimboAI