Smart NPCs Quick Start Guide
Smart NPCs Quick Start Guide
Let's get started with adding voice to your game characters. This guide will show you how to implement Smart NPCs in your Unity project.
Adding Your First Smart NPC
Adding Your First Smart NPC
You have two ways to implement a Smart NPC:
Method 1: Through Code
Method 1: Through Code
Create a new script (e.g., ExampleSmartNPC.cs
) with this basic implementation:
using SkillfulAI.SmartNPCs;
using UnityEngine;
public class ExampleSmartNPC : MonoBehaviour
{
private void Start()
{
// Add the Smart NPC component
SmartNPC myNpc = gameObject.AddComponent<SmartNPC>();
// Set a voice by category
VoiceSelector.SetVoiceByCategory(myNpc, VoiceCategories.Female);
// Set the output audio source for the NPC.
myNpc.audioSource = gameObject.AddComponent<AudioSource>();
// Make the NPC speak!
myNpc.Speak("Hello! I'm your first Smart NPC!");
}
}
Method 2: Using the Unity Inspector
Method 2: Using the Unity Inspector
Create an empty GameObject
Add the SmartNPC component

Add an AudioSource component (required for speech playback)
Create a script to interact with it:
using SkillfulAI.SmartNPCs;
using UnityEngine;
public class NPCController : MonoBehaviour
{
private SmartNPC npc;
private void Start()
{
// Get reference to existing SmartNPC component
npc = GetComponent<SmartNPC>();
// Select a voice and speak
VoiceSelector.SetVoiceByName(npc, "YourVoiceNameHere");
npc.Speak("Hello, I'm ready to talk!");
}
}
Voice Selection Options
Voice Selection Options
Smart NPCs offers several ways to select voices:
public class VoiceExample : MonoBehaviour
{
private SmartNPC npc;
void Start()
{
npc = gameObject.AddComponent<SmartNPC>();
// Option 1: Select by category
VoiceSelector.SetVoiceByCategory(npc, VoiceCategories.Male);
// Option 2: Select by specific voice name
VoiceSelector.SetVoiceByName(npc, "VoiceName");
// Option 3: Select a random voice
VoiceSelector.SetRandomVoice(npc);
}
}
Customizing Voice Settings
Customizing Voice Settings
Fine-tune your NPC's voice characteristics:
public class VoiceCustomization : MonoBehaviour
{
private SmartNPC npc;
void Start()
{
npc = gameObject.AddComponent<SmartNPC>();
// Customize voice settings
npc.voiceSettings = new VoiceSettings
{
stability = 0.5f, // Voice consistency
similarity_boost = 0.5f, // Similarity to original
use_speaker_boost = true // Enhanced clarity
};
// Test the customized voice
npc.Speak("This voice has been customized!");
}
}
Integrating with Skillful Agents
Integrating with Skillful Agents
Create an NPC that can think and speak:
public class TalkingNPC : MonoBehaviour
{
private SkillfulAgent agent;
private SmartNPC npc;
void Start()
{
// Setup both components
agent = gameObject.AddComponent<SkillfulAgent>();
npc = gameObject.AddComponent<SmartNPC>();
// Configure them
agent.SelectAgentByName("YourAgentName");
VoiceSelector.SetVoiceByCategory(npc, VoiceCategories.Female);
// Start a voiced conversation
agent.SendMessage("Introduce yourself", response => {
npc.Speak(response);
});
}
}
Managing Speech Queue
Managing Speech Queue
Handle multiple speech requests:
public class QueuedSpeechManager : MonoBehaviour
{
private SmartNPC npc;
void Start()
{
npc = gameObject.AddComponent<SmartNPC>();
VoiceSelector.SetVoiceByCategory(npc, VoiceCategories.Male);
// Queue multiple speeches
npc.Speak("First message");
npc.Speak("Second message");
npc.Speak("Third message");
// Messages will play in order automatically
}
public void StopTalking()
{
npc.StopSpeaking(); // Clears queue and stops current speech
}
}
Creating a Basic Dialogue System
Creating a Basic Dialogue System
Implement a simple dialogue system:
public class DialogueNPC : MonoBehaviour
{
private SmartNPC npc;
private SkillfulAgent agent;
private Queue<string> dialogueQueue = new Queue<string>();
void Start()
{
// Setup components
npc = gameObject.AddComponent<SmartNPC>();
agent = gameObject.AddComponent<SkillfulAgent>();
// Configure
VoiceSelector.SetVoiceByCategory(npc, VoiceCategories.Female);
agent.SelectAgentByName("DialogueAgent");
}
public void StartDialogue()
{
agent.SendMessage("Greet the player", response => {
npc.Speak(response);
});
}
public void PlayerResponse(string input)
{
agent.SendMessage(input, response => {
npc.Speak(response);
});
}
}
Next Steps
Now that you have your Smart NPC working, consider:
Experimenting with different voices and settings
Creating more complex dialogue systems
Implementing voice-based tutorials
Adding ambient NPC chatter
Creating voiced cutscenes
Check out the Service Reference for more detailed implementation options and advanced features.
Last updated