Creating 3D AI characters in Unity that can perform actions on command can significantly enhance the interactivity and immersion of your game. In this blog post, we'll guide you through the process of integrating Convai's AI capabilities with Unity to create dynamic, action-capable AI characters. We'll explore setting up actions, adding custom behaviors, and implementing advanced interactions that make your AI NPCs (Non-Playable Characters) more lifelike and responsive.
If you’re a Unity developer, check out our detailed YouTube playlist on creating different AI-powered projects using Convai in Unity.
Introduction
Creating AI characters that respond to commands in Unity can be a game-changer for developers, game designers, and virtual world builders. With Convai's tools, you can bring your NPCs to life, making them capable of performing complex actions like picking up and throwing objects, dancing, and interacting with their environment in a natural manner. This tutorial will show you how to set up these features and enhance your game's realism and engagement. Furthermore, you can check out our documentation page to learn more about the Actions integration.
You can also follow our step by step tutorial on YouTube:
Setting Up Convai in Unity
Before diving into action configurations, ensure you have the Convai plugin installed in Unity. Follow these steps to set up your project:
![](https://cdn.prod.website-files.com/63ea75dcbd94095356f1d43b/6672c543cc07e459748b9043_2.png)
1. Download and Install Convai Plugin: Visit Convai's Unity Plugin Documentation to download and install the plugin. You can also, directly download the latest version from the Unity Asset Store.
2. Create a Convai Account: Sign up at Convai to access the API and get started.
3. Import the Plugin into Unity: Import the Convai plugin into your Unity project to access its features.
Configuring Basic Actions
To begin, let's set up basic actions for our AI characters. This involves configuring the actions within the Convai NPC script in Unity.
1. Select the Convai NPC Character: From the hierarchy, select your Convai NPC character.
2. Add Action Script: Locate the Convai NPC script and click on "Add Components" to add the action script to your NPC.
3. Predefined Actions: Convai offers a range of predefined actions. Start by adding simple actions like "Move To":
- Click the plus button to add a new action element.
- Choose "Move To" from the drop-down menu and name the action for clarity.
4. Animation: Leave the animation name field empty for now. We'll revisit this later.
Example: Adding a "Move To" Action
// Adding a Move To Action
ConvaiNPC.AddComponent<MoveToAction>();
Adding Custom Actions
![](https://cdn.prod.website-files.com/63ea75dcbd94095356f1d43b/6672c573e1fa6622177ad0a9_3.png)
Custom actions allow you to add unique behaviors to your AI characters. Let's start with a simple custom action like dancing:
1. Find Dance Animation: Locate the dance animation within the Convai plugin.
2. Animator Controller: Open the animator controller and drag the dance animation to create a new node.
3. Rename Node: Rename the node to "Dancing".
4. Add Action: In the action handler script, add a new action called "Dancing" and set the enum to None.
Example: Adding a Dancing Action
// Adding a Dancing Action
public enum NPCAction {
None,
MoveTo,
Dancing
}
Implementing Advanced Actions
![](https://cdn.prod.website-files.com/63ea75dcbd94095356f1d43b/6672c59e3a3925cdbf0b9386_4.png)
Advanced actions involve more complex interactions, such as picking up and throwing objects. Here's how to implement a throw action:
1. Add Throw Animation: Find and add the throw animation to the animation controller, creating a new node named "Throwing".
2. Action Handler Script: Follow a three-step process to add the throw action:
- Add the throw enum.
- In the `DoAction` function, add a switch case for the throw action.
- Implement the throw function by copying and pasting pre-written code.
3. Add Object to Throw: Add the object (e.g., a rock) to be thrown by dragging it into the scene and including it in the Convai interactable data script.
Example: Adding a Throw Action
// Adding a Throw Action
public enum NPCAction {
None,
MoveTo,
Dancing,
Throw
}
// Implementing the Throw Function
public void DoAction(NPCAction action) {
switch(action) {
case NPCAction.Throw:
ThrowObject();
break;
// Other cases
}
}
private void ThrowObject() {
// Code to handle throwing the object
}
Testing and Debugging
With everything set up, it's time to test your AI character's actions. Hit play in Unity and give commands to your NPC to perform the actions you've configured. Watch as your NPCs respond and perform tasks such as moving to a location, picking up and throwing objects, and dancing.
Example: Testing the Setup
// Command to Throw a Rock
npc.PerformAction(NPCAction.Throw);
Note: If you’re looking to create your first AI Character using a Reallusion Avatar, you can check out our step-by-step tutorial enabling you to do so in Unity:
Conclusion
Creating AI characters in Unity that act on your command can greatly enhance the interactivity and immersion of your games. By following this guide and utilizing Convai's powerful tools, you can bring your AI NPCs to life, making them capable of performing a wide range of actions and interactions. Whether it's simple tasks like moving around or complex behaviors like picking up and throwing objects, Convai makes it easier to create engaging and dynamic AI characters.
Feel free to test out the demo we’ve built for complex actions. And reach out to us at support@convai.com. Also, stay updated with more convai content through our blogs. Happy game development!