> 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/convai-unity-sdk/embodiment/body-animation/play-actions-and-gestures.md). Agents can also search and read this documentation through its [MCP server](https://docs.convai.com/api-docs/~gitbook/mcp).

# 播放动作和手势

从脚本中让 Convai 角色播放命名动作、锚定动作和指向手势，并读取每次调用返回的句柄。

通过脚本播放指定动作或手势、让角色走到锚点并在那里执行动作，或指向某个目标——这一切都可以通过 `ConvaiBodyAnimationController` 以及每次调用返回的句柄来完成。当角色上已运行 Convai Body Animation，且其动画集定义了你想触发的动作或指向方向时，请使用本页。

***

### 前提条件

* `ConvaiBodyAnimationController` 已添加到角色，并分配了动画集。有关如何创作动作和指向方向，请参阅 [构建动画集](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/embodiment/body-animation/build-an-animation-set.md) 。
* 对控制器的引用，可通过在角色上调用 `GetComponent<ConvaiBodyAnimationController>()` 获取。

{% hint style="info" %}
`PlayAction`, `PlayActionAt`和 `PointAt` 绝不会返回 `null`。如果调用失败——例如运行时尚未构建、动作或手势未知，或无法满足请求——每个方法都会返回一个已完成且已失败的句柄。请检查句柄上的 `Failed` 和 `FailureReason` ，而不要对句柄进行 null 检查。
{% endhint %}

***

### 播放指定动作或手势

调用 `PlayAction` 并传入动作名称或别名——匹配时不区分大小写，空格、短横线和下划线视为等价。

```csharp
using Convai.Modules.BodyAnimation;
using Convai.Modules.BodyAnimation.Components;
using UnityEngine;

public sealed class WaveOnCue : MonoBehaviour
{
    [SerializeField] private ConvaiBodyAnimationController bodyAnimation;

    public async void PlayWave()
    {
        BodyAnimationActionHandle handle = bodyAnimation.PlayAction("wave",
            new ActionPlayOptions { HoldSeconds = 3f });

        if (handle.Failed)
        {
            Debug.Log($"Wave didn't play: {handle.FailureReason}");
            return;
        }

        bool completedNaturally = await handle.Completion; // false when interrupted
    }
}
```

`ActionPlayOptions` 字段：

| 字段                 | 类型      | 默认含义               | 说明                                     |
| ------------------ | ------- | ------------------ | -------------------------------------- |
| `SpeedMultiplier`  | `float` | `<= 0` = 条目默认值     | 在条目中设定的速度基础上应用的播放速度倍率。                 |
| `HoldSeconds`      | `float` | `<= 0` = 持续保持，直到停止 | 对于持续保持直到停止的动作：经过这么多秒的主循环后，会自动请求停止。     |
| `FadeInSeconds`    | `float` | `<= 0` = 条目/配置默认值  | 图层混合淡入时长覆盖值。                           |
| `FadeOutSeconds`   | `float` | `<= 0` = 条目/配置默认值  | 图层混合淡出时长覆盖值。也用于 `StopActionImmediate`. |
| `WeightMultiplier` | `float` | `<= 0` = 保持现有行为    | 动作图层权重倍率。                              |

`BodyAnimationActionHandle` （由 `PlayAction`):

| 成员                                           | 说明                                                                    |
| -------------------------------------------- | --------------------------------------------------------------------- |
| `ActionName`                                 | `string` ——请求的名称或别名。                                                  |
| `Failed` / `FailureReason`                   | 请求是否从未启动，以及原因。                                                        |
| `IsDone`                                     | `true` 表示动作已完全结束或被中断。                                                 |
| `Completion`                                 | `Task<bool>` ——完成时返回 `true` ，表示动作正常播放完毕； `false` ，表示动作被中断。            |
| `Stop()`                                     | 请求平滑停止；如果为该条目编写了收尾动画，则会播放。可安全地重复调用。                                   |
| `StopImmediate(float blendOutSeconds = -1f)` | 立即停止，并在 `blendOutSeconds` (`<= 0` （即动作解析得到的淡出时长）内交叉淡出，不再播放剩余动作链或结尾动画。 |

可直接通过控制器停止或中断当前播放的动作，使用 `StopAction()` （平滑停止，会播放结尾动画）或 `StopActionImmediate(float blendOutSeconds = -1f)` （立即交叉淡出）。 `CurrentActionName` 读取当前播放动作的名称；没有动作播放时为空。

由后端触发的手势使用与 `PlayAction`相同的名称/别名匹配逻辑，因此 Convai 的响应中名为 `"pick_up"` 的动作会触发名为 `"Pick Up"` 或别名为 `"pick-up"` 的条目，无需额外配置。

***

### 播放锚定动作

`PlayActionAt` 会让角色走到锚点处，将根节点对齐到锚点姿势，然后播放指定动作——适用于“坐在长椅上”、拾取物品或使用道具等场景。

```csharp
using Convai.Modules.BodyAnimation;
using Convai.Modules.BodyAnimation.Components;
using UnityEngine;

public sealed class SitOnBench : MonoBehaviour
{
    [SerializeField] private ConvaiBodyAnimationController bodyAnimation;
    [SerializeField] private Transform benchAnchor;

    public async void SitDown()
    {
        PlayActionAtHandle handle = bodyAnimation.PlayActionAt(benchAnchor, "sit");
        bool completedNaturally = await handle.Completion; // false when canceled/refused
    }
}
```

| 方法                                                                                                                                     | 说明                                                                |
| -------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------- |
| `PlayActionAt(Transform anchor, string actionNameOrAlias)`                                                                             | 走到 `anchor`处、对齐并播放动作。                                             |
| `PlayActionAt(Transform anchor, string actionNameOrAlias, ActionAnchorOptions anchorOptions, ActionPlayOptions playOptions = default)` | 功能相同，但可显式调整接近方式、对齐方式和动作播放参数。显式指定的 `anchorOptions` 会覆盖动作条目中设定的默认值。 |

`ActionAnchorOptions` 没有用于在脚本中通过自定义值创建实例的公共构造函数——其字段只能在动作条目的 Inspector 中设置，位于 **锚点选项** 动画集中的字段。仅当要复用另一个条目的调校参数时，才将现有的已配置实例传给此重载；否则调用两个参数的重载，让该条目使用自身配置的默认值。

对齐时会忽略锚点的高度——只有其 XZ 位置和偏航角会起作用，因此应将锚点放在角色预期的站立位置，而不是座椅或道具的高度。

`PlayActionAtHandle` （由 `PlayActionAt`):

| 成员                         | 说明                                                                                                       |
| -------------------------- | -------------------------------------------------------------------------------------------------------- |
| `ActionName`               | `string` — 请求的动作名称。                                                                                      |
| `Phase`                    | `PlayActionAtPhase` — 请求当前所处的阶段。                                                                         |
| `Failed` / `FailureReason` | 请求是否从未启动，以及原因。                                                                                           |
| `IsDone`                   | `true` 请求完成或取消后。                                                                                         |
| `Completion`               | `Task<bool>` — 完成时 `true` 表示动作播放完毕， `false` 表示已取消。                                                       |
| `Cancel()`                 | 无论请求当前处于哪个阶段，都会取消：在 `Approaching`阶段停止移动，在 `Aligning`阶段冻结对齐插值，或在 `PlayingAction`阶段平稳地停止动作。可重复调用，不会产生额外影响。 |

***

### 指向目标或位置

`PointAt` 会将手臂抬向世界坐标中的某个位置或一个移动中的 Transform，在最高点保持一段时间，然后放下。

```csharp
using Convai.Modules.BodyAnimation;
using Convai.Modules.BodyAnimation.Components;
using UnityEngine;

public sealed class PointAtProp : MonoBehaviour
{
    [SerializeField] private ConvaiBodyAnimationController bodyAnimation;
    [SerializeField] private Transform target;

    public async void Point()
    {
        BodyAnimationPointingHandle handle = bodyAnimation.PointAt(target, holdSeconds: 3f);
        await handle.Completion;
    }
}
```

| 重载                                                          | 说明                                          |
| ----------------------------------------------------------- | ------------------------------------------- |
| `PointAt(Vector3 worldPosition, float holdSeconds = -1f)`   | 指向固定的世界坐标位置。                                |
| `PointAt(Transform target, float holdSeconds = -1f)`        | 指向一个（移动的）变换对象，并在保持期间持续调整瞄准方向。               |
| `PointAt(Transform target, in PointingPlayOptions options)` | 功能相同，但可调整播放参数：速度、淡入/淡出时长，以及经过指定时间后如何自动结束保持。 |

`holdSeconds < 0` （或 `PointingPlayOptions.HoldSeconds <= 0`）会一直保持，直到调用 `StopPointing`/`Release()` 。

{% hint style="warning" %}
**`HoldSeconds` 仅表示指向动作达到最高点时的停顿时间，不是整个动作的总时长。** 抬起和放下的过程由动画片段决定。 `PointingPlayOptions.Speed` 会对抬起和放下阶段进行速度缩放，而 `ReleaseStyle` 设置为 `Blend` 会在保持结束时直接淡出姿势，而不是播放前臂动作的收尾。若要保持约一秒，请设置 `Speed = 1.5f` 并设置 `ReleaseStyle = PointingReleaseStyle.Blend`.
{% endhint %}

`PointingPlayOptions` 字段：

| 字段                 | 类型                     | 默认含义                              | 说明                                                                         |
| ------------------ | ---------------------- | --------------------------------- | -------------------------------------------------------------------------- |
| `Speed`            | `float`                | `<= 0` = 原生（`1`)                  | 抬臂/放臂速度倍率。保持时长不受影响。                                                        |
| `HoldSeconds`      | `float`                | `<= 0` = 保持直到释放                   | 在最高点保持的秒数。                                                                 |
| `BlendInSeconds`   | `float`                | `<= 0` = 配置 `PointingFadeSeconds` | 图层淡入时长（秒）。                                                                 |
| `BlendOutSeconds`  | `float`                | `<= 0` = 配置 `PointingFadeSeconds` | 图层淡出时长（秒）。                                                                 |
| `ReleaseStyle`     | `PointingReleaseStyle` | `PlayTail`                        | 经过 `HoldSeconds` 后自动释放时的行为：播放前臂动作的收尾（`PlayTail`，默认值），或立即交叉溶解淡出姿势（`Blend`). |
| `WeightMultiplier` | `float`                | `<= 0` = 保持现有行为                   | 指向图层权重倍率。                                                                  |

`BodyAnimationPointingHandle` （由每个 `PointAt` 重载返回）：

| 成员                                              | 说明                                |
| ----------------------------------------------- | --------------------------------- |
| `Failed` / `FailureReason`                      | 请求是否从未启动，以及原因。                    |
| `IsDone`                                        | `true` 指向手势完全结束（手臂放下）后。           |
| `Completion`                                    | `Task` — 手势完全结束后完成。               |
| `Release()`                                     | 立即结束保持动作；手臂放下的收尾动画会在完成前播放。        |
| `ReleaseImmediate(float blendOutSeconds = -1f)` | 立即停止并交叉淡出当前姿势，跳过手臂放下的收尾动画。        |
| `SetSpeed(float speed)`                         | 实时调整正在运行的手势抬臂/放臂速度。保持动作期间不执行任何操作。 |

可直接通过控制器停止当前的指向保持：使用 `StopPointing()` （平滑停止，会播放收尾），或使用 `StopPointingImmediate(float blendOutSeconds = -1f)` （立即交叉溶解）。

***

### 面向某个方向并设置对话锚点

`FaceTowards(Vector3 worldDirection, string reason = "FaceTowards")` 使用动画化的原地转身动作组，使角色转向指定方向——无需 `NavMeshAgent` 。请求无法执行时（功能已禁用、缺少动画片段或移动状态繁忙），返回 `false` 。

```csharp
bodyAnimation.FaceTowards(playerTransform.position - character.position, "greeting turn");
```

`SetConversationAnchor(Transform anchor)` 会覆盖社交间距、近距离表现以及环境行为抑制功能所认定的“角色正在交谈的对象”变换。默认解析链最终会回退到 `Camera.main`；对于 XR 设备、第二位本地玩家，或没有 `MainCamera` 标签的过场摄像机而言，这并不是合适的锚点。 `ClearConversationAnchor()` 会恢复默认解析链。

***

### 故障排查

#### 句柄的 `Failed` 为 `true`

**症状：** 调用立即返回，且没有任何内容播放。

**原因：** `FailureReason` 会说明具体原因 — 常见原因包括 `"runtime not built"` （图尚未就绪）、 `"unknown action"` （动画集合中没有匹配的名称或别名）、 `"an active non-interruptible action is still playing"`, `"no locomotion"` (`PlayActionAt` 但角色上没有 `ConvaiNavMeshLocomotion` ），或 `"set has no pointing clips"`.

**解决方法：** 如果是 `"runtime not built"`，请订阅 `ConvaiBodyAnimationController.RuntimeReady` ，并在处理程序中调用，而不是在 `Start()`/`Awake()` 中调用 — 过早发起的调用也会记录在单个延迟槽中，并自动重放，但该事件可确保调用成功执行。对于其他原因，请检查动画集合中配置的动作和指向方向，或添加 `ConvaiNavMeshLocomotion`.

**验证：** `handle.Failed` 为 `false` ，并且 `Completion` 会在手势播放后完成。

***

### 后续步骤

{% content-ref url="/pages/2f6d4e1228a37a16fdb14e3c1941f0d1c8cf7692" %}
[配置移动](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/embodiment/body-animation/configure-locomotion.md)
{% endcontent-ref %}

{% content-ref url="/pages/5e8d8a3b4fe5ee06381a3ed5c6ba13e6f297f706" %}
[身体动画配置参考](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/embodiment/body-animation/config-reference.md)
{% endcontent-ref %}

{% content-ref url="/pages/bfa444bd8429ef5ecce69dad819e8a8afcb74544" %}
[身体动画脚本参考](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/embodiment/body-animation/scripting-reference.md)
{% endcontent-ref %}


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/embodiment/body-animation/play-actions-and-gestures.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
