角色控制器

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

ControllerManager 实例管理其分配的 RootPart 的模拟运动控制。与 ControllerPartSensors 一起,它可以用于构建物理基础的角色控制器。

核心设置

ControllerManager 需要一个 BasePart 作为根使用。运动力和零件感应将在这个部分上。

  1. 选择一个 PartMeshPart 并命名为 根部分

  2. 将零件组为组织中的Model实例,与其他组件一起。

  3. ControllerManager 实例添加到模型。如果 控制器管理器 在对象插入菜单中最初不出现,那么在菜单的 插入设置 中将“仅显示推荐对象”取消选择。

    ControllerManager and RootPart inside a model.

传感器设置

A 检测出使用相同代码检测地板和梯子的零件。

  1. ControllerPartSensor 插入 RootPart 的子级,并将其重命名为 地面传感器 以便更容易识别其目的。然后,在 属性 窗口中,将其 SearchDistance 属性设置为 2 但留下其 SensorMode 作为 地板

    GroundSensor as child of RootPart
  2. 插入另一个 ControllerPartSensor 作为 RootPart 的子,并将其重命名为 攀爬传感器 。然后,在 属性 窗口中,将其 SearchDistance 属性设置为 1 并将其 SensorMode 设置为 梯子

    ClimbSensor as child of RootPart

控制器设置

控制器实例,如 GroundControllerClimbController 告诉管理部分如何与世界互动,与你在 传感器配置 期间配置的传感器一起工作。

  1. 插入 both a GroundControllerClimbController 作为 ControllerManager 的孩子。

    GroundController and ClimbController as children of ControllerManager.
  2. 选择新的 GroundController 实例,然后,在 属性 窗口中,将其 GroundOffset 属性设置为在管理部件上方“悬停”的值。重要的是,这个值小于 值的 对于 地面传感器 ,因为该传感器如果失去对地面的感知并停止其力量在部分上,将被禁用。

链接参考

要完成核心设置,你需要将 ControllerManager 实例的各种属性链接到主 Model 中的对象。

  1. 选择 ControllerManager 实例。

    ControllerManager selected in Explorer window.
  2. 属性 窗口中,单击以下每个属性,然后,返回 Explorer 窗口,单击相应的实例以完成关联接。

    1. ActiveController属性链接到 地面控制器 实例。
    2. RootPart 属性链接到你命名的部分 RootPart
    3. ClimbSensor 属性链接到 攀爬传感器 实例。
    4. GroundSensor属性链接到 地面传感器 实例。
    ControllerManager properties linked to instances within overall model

测试

配备 传感器控制器 以及 链接的参考,您可以在 Studio 中测试控制器。

  1. 使用 运行 模式启动游戏测试(F8),因为你不需要在此场景中插入你的虚拟形象角色。

    Rapid playtest options in Studio.
  2. 根部 应在你在GroundController.GroundOffset中设置的上升于地面。它还应旋转以与 ControllerManager.FacingDirection 向矢量力对齐。

    Character levitating above ground at GroundOffset, facing in direction of FacingDirection vector.
  3. 在执行时间时更改 MovingDirectionFacingDirection 向量的 ControllerManager 以实现不同的运动和面向方向的实验。还可以尝试不同属性的 GroundController 实例,例如 AccelerationTimeDecelerationTimeGroundOffset

    ControllerManager properties for moving and facing direction are highlighted.
    控制器管理器
    GroundController properties for acceleration, deceleration, and ground offset are highlighted.
    地面控制器

自定义传感器

ControllerPartSensor.SensorMode 选项的 FloorLadder 运行准确的Humanoid传感器验证码,让你能够使用它们进行向后兼容。然而,你也可以自定义如何和何时检测到可行走和攀爬的部件,最终改变管理部件走动/攀爬时间。

  1. ControllerPartSensor.UpdateType阅读时 切换到 手动

    • 在阅读时 触发输出属性与您的 SensorMode 每次阅读相应更新。它本质上将传感器置于“仅读”模式,任何你写入这些属性的内容都会被其内部感知行为覆盖。
    • 手动 关闭内部感知行为。输出属性是免费的,你可以写你喜欢的任何内容,而 SensorMode 不做任何事情。
  2. 创建一个脚本,执行自己的感知逻辑并将输出写入您的传感器输出属性。通常你会使用空间查询,例如 WorldRoot:Raycast()WorldRoot:Blockcast() ,返回一个 RaycastResult 。您的脚本可以然后使用结果属性并将其设置为传感器等价物:

    请注意,您需要管理查询的运行频率以及更新传感器实例。

  3. ControllerManager 将继续使用已分配给它的传感器,接收你传给它的数据。