主题
HumanController
概览
| 字段 | 值 |
|---|---|
| Kind | Unit · 单位 Unit |
| Realm | common |
继承关系
- Unit(4 属性 / 25 函数 / 6 事件)
- BaseController(12 属性 / 22 函数 / 11 事件)
- [HumanController](8 属性 / 2 函数 / 8 事件)
- BaseController(12 属性 / 22 函数 / 11 事件)
继承成员
2 个来源 / 16 属性 / 47 函数 / 17 事件
- 来自 BaseController(12 属性 / 22 函数 / 11 事件)
- 属性:
Health、MaxHealth、MaxMultiJumpCount、MultiJumpCooldown、WalkSpeed、JumpPower、GravityEnabled、HPBarShowMode、DisplayDistanceType、NameDisplayDistance、HealthDisplayDistance、AutoRotate - 函数:
GetState、ChangeState、SetStateEnabled、GetStateEnabled、GetExtraStates、AddExtraState、RemoveExtraState、RegisterCustomState、RegisterExtraState、UnRegisterExtraState、UnRegisterCustomState、Move、MoveTo、Jump、IsGrounded、GetFloor、TakeDamage、Heal、Reborn、SetPhysicsActive、ApplyForce、SetMaxLinearVelocity - 事件:
StateChanged、ExtraStatesChanged、OnLanded、OnStartFalling、Died、HealthChanged、MoveToFinished、OnReborn、OnJump、OnCollisionEnter、OnCollisionExit
- 属性:
- 来自 Unit(4 属性 / 25 函数 / 6 事件)
- 属性:
AssetId、Name、Desc、Parent - 函数:
ClearAllChildren、GetChildren、GetDescendants、IsAncestorOf、IsDescendantOf、FindFirstAncestor、FindFirstAncestorWhichIsA、FindFirstAncestorOfClass、FindFirstChild、FindFirstChildWhichIsA、FindFirstChildOfClass、HasChildren、GetChildCount、GetChildAtIndex、SetAttribute、GetAttribute、GetAllProps、Clone、IsA、Destroy、GetPropertyChangedSignal、GetAttributeChangedSignal、FindFromPath、GetFullPath、GetDebugId - 事件:
ChildAdded、ChildRemoved、DescendantAdded、DescendantRemoving、AncestryChanged、Destroying
- 属性:
HumanController 是人形角色的行为控制器,负责驱动物人形单位的移动速度查询、攀爬、坐下以及人形状态机等行为。它在通用控制器能力之上补充了 ClimbEnabled、ClimbMoveDirection、SeatPart、Sit、TargetPoint、EvaluateStateMachine、InertiaEnabled、NameBarOffset 等人形专属属性,并通过 GetMoveVelocity 与 GetRelativeVelocityAtFloor 暴露速度信息,同时提供 Climbing、Running、FallingDown、GettingUp、FreeFalling、Seated、OnClimbStart、OnClimbEnd 等本地状态事件。
适用场景
当玩家角色确实是人形单位时,可用它查询当前移动速度、开关攀爬能力、设置攀爬方向与目标点,或监听攀爬、奔跑、下落、坐下等状态切换来驱动角色表现。例如在角色进入可攀爬区域时打开 ClimbEnabled 并监听 OnClimbStart 与 OnClimbEnd,在 Running 触发时按传入的速度值调整表现。
使用要点
先从 HumanUnit 取得 Controller,例如确认角色 IsA("HumanUnit") 后读取 Controller 并判空。可读取只读 ClimbMoveDirection 观察当前攀爬方向,不能直接设置它;按公开可写属性 ClimbEnabled、InertiaEnabled、EvaluateStateMachine 调整相应开关,运动操作使用公开 Controller 方法。通过 GetMoveVelocity、GetRelativeVelocityAtFloor 查询速度,连接所需本地事件,并在不再需要时断开自有连接。
注意事项
HumanController 只能从 HumanUnit.Controller 获取;Players.LocalPlayer.Character 也可能是 EggyUnit 等其它角色类型,调用 HumanController 专属成员前必须先用 IsA("HumanUnit") 判定。
代码示例
从 HumanUnit.Controller 获取控制器
lua
-- @runtime client
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
if localPlayer ~= nil and localPlayer.Character ~= nil then
local humanUnit = localPlayer.Character
if not humanUnit:IsA("HumanUnit") then return end
local controller = humanUnit.Controller
if controller ~= nil then
local velocity = controller:GetMoveVelocity()
print("移动速度:", velocity)
end
end属性 (8)
| Name | 类型 | 默认值 | 说明 |
|---|---|---|---|
ClimbEnabled | Bool | false | HumanController 的布尔属性,用于开启或关闭该控制器的攀爬能力,默认值为 false。 |
ClimbMoveDirection | Vector3 | [0, 0, 0] | Vector3 类型属性,用于表示该 HumanController 所属角色在攀爬状态下的移动方向。 |
SeatPart | Unit | - | Unit 类型属性,表示与该 HumanController 所属角色乘坐状态关联的座椅单位对象。 |
Sit | Bool | false | Sit 是 HumanController 的布尔属性,用于表示角色当前是否处于坐下状态。 |
TargetPoint | Vector3 | [0, 0, 0] | Vector3 类型属性,表示该 HumanController 所属角色的目标点位置。 |
EvaluateStateMachine | Bool | true | EvaluateStateMachine 是 HumanController 的布尔属性,标题为“启用状态机”,在 Meta 中标记为可编辑。 |
InertiaEnabled | Bool | true | InertiaEnabled 是 HumanController 的布尔属性,标题为“启用惯性”,在 Meta 中标记为可编辑。 |
NameBarOffset | Float | 0.0 | NameBarOffset 是 HumanController 的 Float 类型属性,标题为“名称显示偏移”,在 Meta 中标记为可编辑。 |
关联类型
事件 (8)
Climbing
签名:Climbing(speed: Float) 触发:local
Climbing 是 HumanController 上的本地事件,用于通知攀爬相关的状态变化,回调参数 speed 的类型为 Float。
| 回调参数 | 类型 | 说明 |
|---|---|---|
speed | Float | 当前攀爬速度 |
示例代码
监听角色攀爬速度
lua
-- @runtime client
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local humanUnit = localPlayer ~= nil and localPlayer.Character or nil
if humanUnit == nil or not humanUnit:IsA("HumanUnit") then return end
local controller = humanUnit ~= nil and humanUnit.Controller or nil
if controller ~= nil then
controller.Climbing:Once(function(speed)
print("攀爬速度:", speed)
end)
endRunning
签名:Running(speed: Float) 触发:local
Running 是 HumanController 上的本地事件,回调参数 speed 的类型为 Float,用于通知奔跑相关的速度信息。
| 回调参数 | 类型 | 说明 |
|---|---|---|
speed | Float | 当前水平移动速度 |
示例代码
监听角色奔跑速度
lua
-- @runtime client
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local humanUnit = localPlayer ~= nil and localPlayer.Character or nil
if humanUnit == nil or not humanUnit:IsA("HumanUnit") then return end
local controller = humanUnit ~= nil and humanUnit.Controller or nil
if controller ~= nil then
controller.Running:Once(function(speed)
print("水平移动速度:", speed)
end)
endFallingDown
签名:FallingDown(active: Bool) 触发:local
FallingDown 是 HumanController 上的本地事件,回调参数 active 为 Bool 类型,用于表示摔倒相关状态。
| 回调参数 | 类型 | 说明 |
|---|---|---|
active | Bool | true=进入绊倒,false=离开绊倒 |
示例代码
监听角色绊倒状态
lua
-- @runtime client
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local humanUnit = localPlayer ~= nil and localPlayer.Character or nil
if humanUnit == nil or not humanUnit:IsA("HumanUnit") then return end
local controller = humanUnit ~= nil and humanUnit.Controller or nil
if controller ~= nil then
controller.FallingDown:Once(function(active)
print("绊倒状态:", active)
end)
endGettingUp
签名:GettingUp(active: Bool) 触发:local
GettingUp 是 HumanController 上的本地事件,回调参数 active 为 Bool 类型,用于表示起身相关状态。
| 回调参数 | 类型 | 说明 |
|---|---|---|
active | Bool | true=开始起身,false=起身完成 |
示例代码
监听角色起身状态
lua
-- @runtime client
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local humanUnit = localPlayer ~= nil and localPlayer.Character or nil
if humanUnit == nil or not humanUnit:IsA("HumanUnit") then return end
local controller = humanUnit ~= nil and humanUnit.Controller or nil
if controller ~= nil then
controller.GettingUp:Once(function(active)
print("起身状态:", active)
end)
endFreeFalling
签名:FreeFalling(active: Bool) 触发:local
FreeFalling 是 HumanController 上的本地事件,回调参数 active 为 Bool 类型,用于表示自由下落相关状态。
| 回调参数 | 类型 | 说明 |
|---|---|---|
active | Bool | true=开始下落,false=落地 |
示例代码
监听角色自由下落状态
lua
-- @runtime client
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local humanUnit = localPlayer ~= nil and localPlayer.Character or nil
if humanUnit == nil or not humanUnit:IsA("HumanUnit") then return end
local controller = humanUnit ~= nil and humanUnit.Controller or nil
if controller ~= nil then
controller.FreeFalling:Once(function(active)
print("自由下落:", active)
end)
endSeated
签名:Seated(active: Bool, seat: Unit) 触发:local
HumanController 的本地事件,在角色乘坐状态相关情况发生时触发,回调提供 active(Bool)与 seat(Unit)两个参数,分别表示是否处于坐下状态以及关联的座椅单位。
| 回调参数 | 类型 | 说明 |
|---|---|---|
active | Bool | true=坐下,false=起身 |
seat | Unit | 座位 Unit |
示例代码
监听角色坐下状态
lua
-- @runtime client
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local humanUnit = localPlayer ~= nil and localPlayer.Character or nil
if humanUnit == nil or not humanUnit:IsA("HumanUnit") then return end
local controller = humanUnit ~= nil and humanUnit.Controller or nil
if controller ~= nil then
controller.Seated:Once(function(active, seat)
print("坐下状态:", active, "座位:", seat)
end)
endOnClimbStart
签名:OnClimbStart() 触发:local
HumanController 的本地事件,在角色开始进入攀爬状态时触发,用于通知外部逻辑攀爬行为已经开始。
示例代码
监听 OnClimbStart
lua
-- @runtime client
local function demo(humanController)
if humanController ~= nil then
humanController.OnClimbStart:Once(function()
print("OnClimbStart event")
end)
end
endOnClimbEnd
签名:OnClimbEnd() 触发:local
HumanController 的本地事件,在角色结束攀爬状态时触发,用于通知外部逻辑一次攀爬行为已经结束。
示例代码
监听 OnClimbEnd
lua
-- @runtime client
local function demo(humanController)
if humanController ~= nil then
humanController.OnClimbEnd:Once(function()
print("OnClimbEnd event")
end)
end
end函数 (2)
GetMoveVelocity
签名:GetMoveVelocity() -> Vector3 (当前移动速度向量)
HumanController 的函数,无参数调用,返回一个 Vector3,表示该控制器所属角色当前的移动速度向量。
⚙ 返回角色当前的移动速度向量(来自 Controller 输入),与 BasePart.AssemblyLinearVelocity(物理装配体线速度)含义不同:前者反映操控意图,后者反映实际物理运动。
返回值 Vector3 (当前移动速度向量)
示例代码
从 HumanUnit.Controller 获取移动速度
lua
-- @runtime client
-- HumanController 通过 HumanUnit.Controller 属性获取
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
if localPlayer and localPlayer.Character then
local humanUnit = localPlayer.Character
if not humanUnit:IsA("HumanUnit") then return end
local controller = humanUnit.Controller
if controller ~= nil then
local velocity = controller:GetMoveVelocity()
print(string.format('移动速度: %s', tostring(velocity)))
end
endGetRelativeVelocityAtFloor
签名:GetRelativeVelocityAtFloor() -> Vector3 (相对于站立表面的速度向量(世界空间方向))
HumanController 的函数,无参数调用,返回一个 Vector3,表示角色相对于所站立表面的速度向量,方向为世界空间方向。
返回值 Vector3 (相对于站立表面的速度向量(世界空间方向))
示例代码
从 HumanUnit.Controller 获取相对地面速度
lua
-- @runtime client
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local humanUnit = localPlayer ~= nil and localPlayer.Character or nil
if humanUnit == nil or not humanUnit:IsA("HumanUnit") then return end
local controller = humanUnit ~= nil and humanUnit.Controller or nil
if controller ~= nil then
local velocity = controller:GetRelativeVelocityAtFloor()
print("相对地面速度:", velocity)
end