Production AI NPC Systems in Unity (Part 2): Structured JSON Function Calling & Behavior Trees
In Part 1 of this masterclass, we established the inference pipeline comparing Local vs. Cloud LLMs in Unity.
However, having an AI character output raw conversational prose (e.g., "I'm going to walk over to the blacksmith and buy a sword") is useless for a 3D game engine. To make the AI interact with the game world, the LLM must return structured, deterministic JSON payloads that directly control Unity animations, NavMesh pathfinding, inventory transfers, and dialogue UIs.
In Part 2, we implement a production C# parser that enforces JSON Function Calling and hooks directly into Unity Behavior Trees and state machines.
1. Defining the AI NPC Action Schema
To guarantee the LLM outputs valid JSON, we inject a strict JSON Schema into the System Prompt.
System Prompt Payload Example
2. Production C# Code: JSON Parser & Behavior Tree Driver
Below is a robust C# controller (AINPCBehaviorTreeDriver.cs) that parses structured JSON output and converts it into Unity NavMesh movement and animation triggers.
3. What's Coming in Part 3
Now that our AI NPC can think (Part 1) and act physically in 3D space (Part 2), the final step is giving the AI a realistic voice.
In Part 3 of this masterclass, we cover:
- Low-Latency Text-To-Speech (TTS) Voice Streaming: Integrating ElevenLabs / OpenAI Audio APIs with Unity
AudioSource. - Zero-Allocation PCM Audio Buffering: Streaming 3D spatial voice chunks into Unity without memory spikes or audio stuttering.
š Read Part 3: Low-Latency Spatial Audio & Voice Streaming (TTS) ā š Test the Playable Unity 6 NPC AI System (FSM, Vision Cone & NavMesh Demo) ā
š” Building a Custom AI Gameplay System?
Need help structuring JSON schemas, implementing robust AI behavior trees, or preventing invalid LLM outputs in Unity?
- AI Behavior Tree Architecture: Designing deterministic state machine handlers for LLM outputs.
- Unity Engine Integration: Zero-allocation JSON parsing and NavMesh pathfinding optimization.
š Book an AI Gameplay Engineering Consultation or reach out directly to discuss your project.
š® Shipped Projects & Official Store Profiles
Explore commercial titles and technical systems built with Unity across official stores:
Featured Shipped Projects:
- Pet Hero: Animal Hospital ā App Store | Google Play
- Restaurant RUSH: Idle Tycoon ā App Store | Google Play
- Bird Sort Mania ā Google Play | Read Case Study
Planning an AI-powered title? Check out our AI Gameplay Systems Services or explore our 2026 Mobile Game Development Cost Guide.
Get Practical Unity Development Insights
Technical guides on Unity performance, AI gameplay systems, mobile monetization, and game development.
Unsubscribe at any time. Your data is never shared.
Recommended Reading
More articles in AI Gameplay Systems
Production AI NPC Systems in Unity (Part 1): Local vs. Cloud LLM Inference Architecture
Part 1 of our Unity AI NPC masterclass. Learn how to architect low-latency AI companion systems in Unity C# comparing local ONNX/Ollama models with Cloud APIs like OpenAI and Anthropic.
Production AI NPC Systems in Unity (Part 3): Low-Latency Spatial Audio & Voice Streaming (TTS)
Part 3 of our Unity AI NPC masterclass. Learn how to stream dynamic Text-To-Speech (TTS) audio chunks into 3D positional Unity AudioSources without GC allocations or audio stuttering.
How to Use AI in Unity Game Development: Smart NPCs & Adaptive Gameplay (2026)
Learn how to build adaptive NPC behavior and intelligent gameplay systems in Unity 6. Covers FSM vs Behavior Trees, sensory perception, NavMesh, and C# code.