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) ā
š” 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.
Looking to build a production-ready game?
See how I built Bird Sort Mania in 20 days using AI, or check out my full Mobile Games Portfolio to see my shipped titles on Android and iOS.
Join 5,000+ Game Developers
Get weekly insights on Unity performance optimization, AI gameplay architectures, and robust system design. No spam, just deep technical breakdowns.
Unsubscribe at any time. Your data is never shared.
Recommended Reading
More articles in AI Gameplay