Getting Started
AI NPC Brain - Getting Started
A Unity package that gives your NPCs an LLM-powered brain. NPCs hold conversations, remember facts about the player, track relationship scores, suggest game events (open shop, offer quests, play animations), and validate everything through a configurable whitelist before anything fires.
Unity version: 6000.x (Unity 6)
Render pipeline: URP
Key dependencies: TextMesh Pro, Input System, Newtonsoft JSON (via Unity package)
1. Import the Package
- Open your Unity project (Unity 6 / URP recommended).
- Copy or import the
Assets/Runtime,Assets/Editor,Assets/Scripts,Assets/Samples, andAssets/Prefabsfolders into your project'sAssetsdirectory. - Make sure the following packages are installed via Window > Package Manager:
- TextMesh Pro (included with Unity)
- Input System (
com.unity.inputsystem) - Newtonsoft JSON (
com.unity.nuget.newtonsoft-json)
If you are using the demo scene, also import
Assets/Scenes,Assets/Mixamo Character, andAssets/Settings.
2. Create Your NPC's ScriptableObject Assets
You need four ScriptableObject assets per NPC. Create them via Right-click in Project > Create > AI NPC Brain > ...
2a. Persona Definition
Create > AI NPC Brain > Persona Definition
| Field | Description |
|---|---|
npcId |
Unique string identifier, e.g. npc_aurelia_alchemist. Used as the save-file key. |
displayName |
Name shown in the chat UI, e.g. Aurelia. |
personaJson |
Free-form JSON blob injected into the LLM prompt. Describe the NPC's role, personality traits, goals, and speech style. |
Example persona JSON:
{
"role": "Alchemist shopkeeper in a fantasy village",
"traits": ["witty", "cautious", "curious"],
"goals": ["sell potions", "find rare ingredients"],
"speech_style": "Speaks in short, precise sentences with dry humor"
}
2b. Brain Config
Create > AI NPC Brain > Brain Config
This controls which LLM provider to use, API keys, generation parameters, and safety limits. Key fields:
| Field | Default | Description |
|---|---|---|
providerMode |
OpenAI_Default |
Which LLM backend to use. Options: OpenAI_Default, Ollama_Fallback, Ollama_Only, OpenAI_Only |
openAiApiKey |
(empty) | Your OpenAI API key. Do not commit this to version control. |
openAiModel |
gpt-4o-mini |
The OpenAI model to use. |
openAiBaseUrl |
https://api.openai.com/v1 |
Base URL (change for Azure or compatible proxies). |
ollamaBaseUrl |
http://localhost:11434 |
Local Ollama instance URL. |
ollamaModel |
llama3.1:8b |
The Ollama model to use. |
temperature |
0.6 |
LLM creativity (0 = deterministic, 2 = very random). |
maxTokens |
350 |
Maximum response tokens. |
fireConfidenceThreshold |
0.65 |
Minimum confidence for an event to fire. |
maxDeltaPerStat |
3 |
Maximum relationship score change per turn. |
2c. Event Catalog
Create > AI NPC Brain > Event Catalog
The catalog is a whitelist of every game event the NPC is allowed to suggest. Each event has:
- An eventId (e.g. shop.open, anim.trigger)
- A description (shown to the LLM in the prompt)
- A list of typed arguments with optional allowed values
Common events to add:
| Event ID | Args | Description |
|---|---|---|
shop.open |
shop_id (String) |
Opens the NPC's shop panel |
quest.offer |
quest_id (String) |
Offers a quest to the player |
relationship.open_status |
relationship_id (String) |
Opens the relationship status panel |
anim.trigger |
anim_id (String) |
Triggers an NPC animation |
anim.move_to |
move_id (String) |
Moves the NPC to a target |
2d. NPC Animation Map (optional)
Create > AI NPC Brain > NPC Animation Map
Maps semantic animation IDs (like nod, wave) to actual Animator trigger parameter names (like Nod, Wave). This lets the LLM pick contextually appropriate animations.
Tip: Use the editor tool Tools > AI NPC Brain > NPC Animation Setup to auto-fill this from your Animator Controller. See Editor Tools.
3. Set Up the Scene
3a. Add the AiNpcBrain Component
- Create or select the NPC's root GameObject.
- Add the
AiNpcBraincomponent. - Assign your ScriptableObject assets:
- Persona → your Persona Definition asset
- Config → your Brain Config asset
- Event Catalog → your Event Catalog asset
- Animation Map → (optional) your NPC Animation Map asset
3b. Add a Chat UI
Two sample UIs are provided:
Simple (for prototyping): Add SimpleChatUi to a GameObject and wire up:
- brain → the AiNpcBrain component
- input → a TMP_InputField
- chatLog → a TMP_Text
Full-featured: Add NpcChatUiController to a GameObject and wire up:
- brain → the AiNpcBrain component
- inputField → a TMP_InputField
- sendButton → a UI Button
- scrollRect → a ScrollRect
- contentRoot → the Content transform inside the ScrollView
- messagePrefab → the ChatText prefab from Prefabs/UI/
3c. Wire Up Event Receivers
The AiNpcBrain component exposes an OnAnyNpcEventPayload UnityEvent that fires validated JSON payloads. Connect your receiver(s) in the Inspector:
- For animations: Add
NpcAnimationReceiver→ assign itsOnEventPayloadmethod toOnAnyNpcEventPayload. - For UI panels: Add
AureliaDemoReceiver(or your own receiver) → assign itsOnEventFiredmethod toOnAnyNpcEventPayload.
3d. Optional: UI Panel Routing
If you want events like shop.open to actually open panels:
- Create > AI NPC Brain > NPC UI Bindings — set the NPC ID and it auto-generates window IDs.
- Add
WindowRegistryRuntimeto a GameObject in the scene. - Add
WindowPanelIdcomponents to each panel root (shop panel, quest panel, etc.). - Use the editor button Auto-Populate From Scene on the
WindowRegistryRuntimeinspector to discover all panels.
4. Configure Your LLM Provider
Option A: OpenAI (cloud)
- Get an API key from platform.openai.com.
- In your Brain Config asset, set:
providerMode=OpenAI_DefaultorOpenAI_OnlyopenAiApiKey= your keyopenAiModel=gpt-4o-mini(fast and cheap) orgpt-4o(smarter)
Security: For production, set the API key at runtime via script instead of baking it into the asset:
csharp brain.config.openAiApiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY");
Option B: Ollama (local, free)
- Install Ollama on your machine.
- Pull a model:
ollama pull llama3.1:8b - In your Brain Config asset, set:
providerMode=Ollama_OnlyollamaBaseUrl=http://localhost:11434ollamaModel=llama3.1:8b
Option C: Mock (offline testing)
Set providerMode to any mode without providing an API key. The system will fall back to MockProvider, which returns canned responses based on keyword matching (useful for testing UI flow without burning API credits).
5. Press Play
- Enter Play Mode.
- Type a message in the chat input and press Enter or click Send.
- The NPC will respond in-character, and may trigger animations or open UI panels.
Check the Console for [AiNpcBrain] log messages if something isn't working.
6. Running the Demo Scene
- Open
Assets/Scenes/AI Brain Demo.unity. - The scene comes pre-configured with:
- Aurelia — a Mixamo character with an Animator Controller
- Pre-built ScriptableObject assets in
Assets/Samples/ - A chat UI with ScrollView
AureliaDemoReceiverfor panel routingNpcAnimationReceiverfor animation playbackDemoCursorUnlockto keep the mouse visible- Set your API key in the
Aurelia Brainconfig asset (or run Ollama locally). - Press Play and start chatting.
7. Troubleshooting
| Problem | Solution |
|---|---|
| "NPC brain not configured" | Assign Persona and Config assets on the AiNpcBrain component. |
| "The NPC hesitates, unsure what to say" | LLM provider failed. Check Console for HTTP errors. Verify API key / Ollama is running. |
| "The NPC seems confused" | LLM returned invalid JSON. Try a more capable model or lower temperature. |
| Events not firing | Check that your Event Catalog has the event defined with correct args. Check fireConfidenceThreshold in config. |
| Animations not playing | Verify the Animation Map has the correct Animator trigger names. Check that NpcAnimationReceiver has an Animator and Animation Map assigned. |
| Panels not opening | Verify WindowRegistryRuntime has bindings populated. Check that WindowPanelId components are on panel roots with matching IDs. |
Next Steps
- Architecture Overview — understand how all the pieces fit together
- Configuration Guide — deep dive into every setting
- Event System — how events are suggested, validated, and dispatched
- API Reference — detailed documentation for every script
- Editor Tools — automate animation setup and catalog sync