> For the complete documentation index, see [llms.txt](https://docs.convai.com/api-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/unity-plugin/adding-actions-to-your-character.md).

# 为角色添加动作

## 设置动作配置

1. 从层级视图中选择 Convai NPC 角色。
2. 向下滚动，找到附加在角色上的 ConvaiNPC 脚本。
3. 点击“添加组件”按钮。

<figure><img src="/files/452c396cce97e0009edf9cd1cb14da1871a01050" alt="" width="438"><figcaption></figcaption></figure>

4. 使用复选框将动作脚本添加到 NPC Actions。
5. 点击“应用更改”以确认。

<figure><img src="/files/1261ce5371e53d865833fac2c3e9a72501246466" alt="" width="563"><figcaption></figcaption></figure>

## 预定义动作

Convai 提供了预定义动作，便于快速上手。

1. 点击“+”按钮添加一个新动作。
2. 从下拉菜单中选择“Move To”。

<figure><img src="/files/d761c24e2289e3840f24ab6509c9920be50865fb" alt=""><figcaption></figcaption></figure>

3. 将动作名称输入为“Move To”（名称不必与动作选项名称一致）。
4. 暂时将 Animation Name 字段留空。

重复这些步骤以添加更多动作，例如“Pickup”和“Drop”等。

### 在场景中添加一个对象

1. 在场景中添加任意可交互对象——例如球体、立方体、岩石等。
2. 调整对象大小并将其放置到场景中。

<figure><img src="/files/84462f06d424781b657f8cbc98f796b5425e9925" alt=""><figcaption></figcaption></figure>

### 添加 Convai Interactables 数据脚本

* 创建一个空的 GameObject，并将其命名为“Convai Interactables”。
* 将 Convai Interactables Data 脚本附加到该 GameObject。
* 通过点击“+”按钮并附加相应的 GameObject，将角色和对象添加到脚本中。

<figure><img src="/files/f29ce050cddc3ddb0d3eb90462a5fac40ccd1432" alt=""><figcaption><p>Convai Interactables 设置</p></figcaption></figure>

* 在 Objects 列表中添加“There”对象，这样我们就可以使用 Dynamic Move Target 指示器。<br>

  <figure><img src="/files/c7a46323a136b53cd54cda457645766850d5ac70" alt=""><figcaption></figcaption></figure>
* 添加 [动态移动目标指示器](#adding-a-dynamic-move-target-indicator) 以及设置 [NavMesh 代理](#setting-up-navmesh) 到你的 NPC。

### 设置 NavMesh

为了确保你的 NPC 能在场景中导航：

1. **烘焙一个 NavMesh** 如果你还没有这样做，请为场景执行：
   * 前往 **窗口 > AI > Navigation**.
   * 在 **导航** 窗口中，在 **烘焙** 选项卡下，根据需要调整设置。
   * 点击 **“烘焙”** 以生成 NavMesh。
2. 确保 NPC 角色具有一个 **NavMeshAgent** 组件：

   * 如果尚未附加，请点击 **“添加组件”** 并搜索 **NavMeshAgent**.
   * 调整 **Agent Radius、Speed，** 以及根据你的 NPC 需求调整其他参数。

   <figure><img src="/files/c0c4e544aba82cdeef8397e580138cf93b07ea35" alt=""><figcaption></figcaption></figure>

### 添加动态移动目标指示器

为了直观显示 NPC 将要移动到的位置：

* 在场景中创建一个新的空 GameObject 并相应命名，或者使用名为 **Dynamic Move Target Indicator 预制体。**
* 将此 **移动目标指示器** 链接到 NPC 的动作脚本，这样当你将光标指向地面并让 NPC 移动到“There”时，它就会动态更新。

<figure><img src="/files/fde4697792986928098832965f9f1ed1b7af60d9" alt=""><figcaption></figcaption></figure>

### 测试设置

1. 点击“Play”以启动场景。
2. 告诉 NPC：“把盒子拿给我。”
3. 如果设置正确，NPC 应该会走到盒子旁边并把它拿给你。

{% hint style="warning" %}
此功能目前仍处于实验阶段，可能会出现异常。欢迎尝试并向我们提供任何反馈。
{% endhint %}

## 在 Convai 中为你的 Unity NPC 添加自定义动作

### 简介

让你的 NPC 执行诸如跳舞之类的自定义动作。

### 仅需要动画的动作

1. 在我们的插件中找到舞蹈动画文件。

   <figure><img src="/files/9100af484486b13315ed0171415f8e045c8c8954" alt=""><figcaption></figcaption></figure>
2. 将此动画纳入 NPC 的动作中。

### 设置 Animator Controller

1. 在 Inspector 窗口中打开 Animator Controller。
2. 将舞蹈动画拖放到控制器上，创建一个名为“Dancing”的新节点。

   <figure><img src="/files/1056f7c91d29561958a3d0530abba541e9d10142" alt=""><figcaption></figcaption></figure>

### 添加自定义动画动作

1. 转到附加在 Convai NPC 上的 Action Handler Script。
2. 添加一个名为“Dancing”的新动作。
3. 在 Animation Name 字段中输入“Dancing”（它必须与 Animator Controller 节点名称完全一致）。
4. 将枚举保留为“None”。

<figure><img src="/files/d25cb24250fea7f8ad789ef187efdf6c547f369b" alt=""><figcaption></figcaption></figure>

### 测试自定义动作

1. 点击“Play”以启动场景。
2. 让 NPC 执行“Show me a dance move,”，NPC 应该会开始跳舞。

## 使用 Convai 在 Unity 中创建复杂自定义动作：扔石头

### 简介

为你的 NPC 添加高级自定义动作，例如投掷动作。

### 动画要求

1. 获取一个 [来自 Mixamo 的投掷动画](https://www.mixamo.com/#/?page=1\&query=throw) 或者你喜欢的任何地方。
2. 将其导入 Unity。

### 设置 Animator Controller

1. 将投掷动画拖放到控制器上，创建一个名为“Throwing”的新节点。（请按照以下步骤操作 [#action-that-only-requires-an-animation](#action-that-only-requires-an-animation "mention"))

### Action Handler Script 设置

1. 将“Throw”枚举添加到脚本中。

   <figure><img src="/files/58d9270f1ef813b2b16cb1f040c6b1c65bc2f7a0" alt=""><figcaption></figcaption></figure>
2. 在“Do Action”函数中，为投掷动作添加一个 switch 分支。

   <figure><img src="/files/6d0f54f8adaf8c76c56fa1a170544527591e62eb" alt=""><figcaption></figcaption></figure>
3. 定义“Throw()”函数。

   <figure><img src="/files/bdc02a2608a40c9a109602c33a740e8818647c3e" alt=""><figcaption></figcaption></figure>

### 添加 Throw 动作

1. 添加一个名为“Throw”的新动作，并选择“Throw”枚举。
2. 将动画名称字段留空。

   <figure><img src="/files/288dd234c8f28c917cf0b521421739112168d06e" alt=""><figcaption></figcaption></figure>

### 将对象（Rock）添加到 Convai Interactables Data 脚本中

1. 将任意岩石预制体添加到场景中。
2. 将岩石添加到 Convai Interactable Data 脚本中。

### 向 Convai Interactables Data 脚本中添加一个位置

1. 在场景地面上添加一个舞台/新位置。
2. 将该新位置的 GameObject 添加到 Convai Interactable Data 中。

   <figure><img src="/files/7ed7899ce1cbab59348b1fd4f2b8c4559b351997" alt=""><figcaption></figcaption></figure>

### 测试复杂动作

1. 点击“Play”以启动场景。
2. 告诉 NPC：“捡起岩石并从舞台上把它扔出去。”
3. 如果一切设置正确，NPC 应该会捡起岩石并从舞台上将其扔出去。

***


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/unity-plugin/adding-actions-to-your-character.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
