人形是一种特殊对象,可以给模型提供角色的功能。它授予模型以能够物理地走动并与 Roblox 体验的各个组件进行交互的能力。人形总是在 Model 内被父辈化,模型应该是由 BasePart 和 Motor6D 组成的;装配的根部分应该被命名为 HumanoidRootPart 。它还期望一个名为 Head 的部分连接到角色的躯干部分,直接或间接地。默认情况下,Roblox 提供了两种官方角色模型,每种都有自己的规则集:
R6
- 一个基本的角色装备,使用 6 部分来支持肢体。
- Head 部分必须附加到名为 Torso 的部分上,否则机器人将立即死亡。
- 身体部位的外观使用 CharacterMesh 对象进行应用。
- 某些属性,例如 Humanoid.LeftLeg 和 Humanoid.RightLeg,仅与 R6 兼容。
R15
- 比 R6 更复杂,但也更灵活和强大。
- 使用 15 个部件为肢体。
- Head部分必须附加到名为UpperTorso的部分上,否则机器人将立即死亡。
- 身体部位的外观必须直接装配。
- 可以使用特殊的 NumberValue 对象在 Humanoid 内部进行动态缩放,以便适应不同的需求。
- 人形将自动在每个肢体内创建名为 的对象。
- 如果在人形内部有一个 NumberValue 是其父辈,并且命名为以关注中/正在关注之一,它将用于控制缩放功能:
- 身体深度比例
- 身体高度比例
- 身体宽度比例
- 头部缩放
代码示例
This LocalScript makes the camera bobble as the player's character walks around, utilizing both the Humanoid's CameraOffset and MoveDirection. It should be parented inside of the StarterCharacterScripts so that it is distributed into a player's character as expected.
local RunService = game:GetService("RunService")
local playerModel = script.Parent
local humanoid = playerModel:WaitForChild("Humanoid")
local function updateBobbleEffect()
local now = tick()
if humanoid.MoveDirection.Magnitude > 0 then -- Is the character walking?
local velocity = humanoid.RootPart.Velocity
local bobble_X = math.cos(now * 9) / 5
local bobble_Y = math.abs(math.sin(now * 12)) / 5
local bobble = Vector3.new(bobble_X, bobble_Y, 0) * math.min(1, velocity.Magnitude / humanoid.WalkSpeed)
humanoid.CameraOffset = humanoid.CameraOffset:lerp(bobble, 0.25)
else
-- Scale down the CameraOffset so that it shifts back to its regular position.
humanoid.CameraOffset = humanoid.CameraOffset * 0.75
end
end
RunService.RenderStepped:Connect(updateBobbleEffect)
概要
属性
设置角色在移动设备上作为玩家时是否自动跳过障碍物。
自动旋转集合是否启用或否启用人形自动旋转以面向其移动的方向。
启用后,AutomaticScalingEnabled 会导致人形子缩放值的值对应的角色尺寸发生变化,从而响应人形子缩放值的更改。
决定是否在 Enum.HumanoidStateType.Dead 状态下,人形关节会被破坏。
对相机的主题位置应用抵消时,其相机主题设置为这个人形。
控制人形名称和生命值显示的距离行为。
设置一个人形怪物的文本,显示在他们头顶上。
描述 Enum.Material ,该 Humanoid 目前站在的。如果 Humanoid 没有站在任何东西上,这个属性的值将是 空气 .
描述范围上的人形的当前健康状况[0, Humanoid.MaxHealth ]。
与 DisplayDistanceType 属性结合使用,用于控制从哪里可以看到人形生物的生命条的距离。
控制人形健康条可以显示时。
决定 Humanoid.RootPart 从地面上应该离开的距离。
如果 true , 跳跃 Humanoid 以上向上的力。
提供控制到 Humanoid 跳跃的高度。
决定在跳跃时对 Humanoid 施加多少向上力。
人形的最大值 Health 。
机器人可以在没有滑倒的情况下走行的最大坡度角度。
描述 Humanoid 正在走的方向。
与 Humanoid.DisplayDistanceType 属性结合使用,用于控制从哪里可以看到人形名称的距离。
控制是否可以在墙或其他对象后面看到机器人的名称和生命条。
决定是否 Humanoid 目前处于 Enum.HumanoidStateType.PlatformStanding 状态。
允许开发人员禁用玩家死亡的行为,即如果颈部 被移除或暂时断开,即使玩家死亡。
描述这个 Humanoid 是否使用了遗产的 R6 角色骨架,或新的 R15 角色骨架。
对人形的 人形根部件 对象的引用。
如果有的话,指向 Humanoid 当前坐在的座位的参考。
描述是否 Humanoid 当前坐着。
决定是否使用 JumpHeight (错误) 或 Humanoid.JumpPower (真实) 属性。
描述人形的最大移动速度以每秒钟的点数。
一个指向一个部件的参考,其位置正在被人形达到。
在调用 Humanoid:MoveTo() 后,机器人人试图达到的位置。
方法
将指定的 Accessory 附加到人形的父元素。
通过附加人形角色中的 Motor6D 对象来组装一个由 Attachment 个关节组成的树。
将 Humanoid 设置为进入指定的 Enum.HumanoidStateType。
返回一个阵列的 Accessory 对象,这些对象是机器人的父亲目前穿着的。
返回人形的缓存 HumanoidDescription 的副本,描述其当前外观。
将身体部分传给此方法(身体部分应该是 Humanoid 的兄弟,模型的孩子),以获得 Enum.BodyPartR15 的 Part 。
返回人形的当前 Enum.HumanoidStateType。
返回是否启用 Enum.HumanoidStateType 对于 Humanoid 。
使 Humanoid 走在指定的方向。
导致 Humanoid 尝试走到指定位置,通过设置 Humanoid.WalkToPoint 和 Humanoid.WalkToPart 属性来实现。
移除所有由人形的父元素辈穿戴的 Accessory 对象
动态地替换一个人形身体部件为不同的部件。
设置是否启用给定的 Enum.HumanoidStateType 对 Humanoid 。
如果没有被 保护,将降低 的 对于 由给定的 量 减少。
- ApplyDescription(humanoidDescription : HumanoidDescription,assetTypeVerification : Enum.AssetTypeVerification):()
使角色的外观与传入的 HumanoidDescription 匹配。
- ApplyDescriptionReset(humanoidDescription : HumanoidDescription,assetTypeVerification : Enum.AssetTypeVerification):()
使角色的外观与传入的 HumanoidDescription 相匹配,即使在外部更改后。
播放表情并返回,如果运行成功。
活动
当 Humanoid 的速度上升时发生火焰,当速度上升时更改。
当 Humanoid 死亡时发生火灾。
当 Humanoid 进入或离开 FallingDown``Enum.HumanoidStateType 时发生火灾。
当 Humanoid 进入或离开 Freefall``Enum.HumanoidStateType 时发生火灾。
当 Humanoid 进入或离开 GettingUp``Enum.HumanoidStateType 时发生火灾。
当 Humanoid.Health 发生变化时(或当 Humanoid.MaxHealth 被设置)发生火焰。
当 Humanoid 进入并离开 Jumping Enum.HumanoidStateType 时发生火灾。
当 Humanoid 完成走向由 Humanoid:MoveTo() 声明的目标时,发生火焰。
当 Humanoid 进入或离开 PlatformStanding``Enum.HumanoidStateType 时发生火灾。
当 Humanoid 进入或离开 Ragdoll``Enum.HumanoidStateType 时发生火灾。
当 Humanoid 运行的速度发生变化时,发生火焰。
当 Humanoid 坐在 Seat 或 VehicleSeat 上时,发射。
当 Humanoid 状态发生变化时发生火焰。
在 被调用时发生火灾。
当 Humanoid 进入或离开 StrafingNoPhysics``Enum.HumanoidStateType 时发生火灾。
在 在水中游泳的速度发生变化时发生火焰。
当一个机器人肢体与另一个BasePart接触时发生火焰。
属性
AutoJumpEnabled
AutoJumpEnabled 设置是否会使 Humanoid 尝试自动跳过它正在走向的障碍。
目前,此属性仅在以下条件真实时工作:
- 人形的角色模型是 Player.Character 的 Player 。
- 问题的玩家正在使用触摸控制。
当玩家角色生成时,属性值与玩家的 Player.AutoJumpEnabled 属性匹配 - 这在一定程度上匹配了 StarterPlayer.AutoJumpEnabled 属性。
代码示例
This code sample is meant for a TextButton. It allows the player to toggle the auto-jumping behavior while on a mobile device.
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local button = script.Parent
local function update()
-- Update button text
if player.AutoJumpEnabled then
button.Text = "Auto-Jump is ON"
else
button.Text = "Auto-Jump is OFF"
end
-- Reflect the property in the player's character, if they have one
if player.Character then
local human = player.Character:FindFirstChild("Humanoid")
if human then
human.AutoJumpEnabled = player.AutoJumpEnabled
end
end
end
local function onActivated()
-- Toggle auto-jump
player.AutoJumpEnabled = not player.AutoJumpEnabled
-- Update everything else
update()
end
button.Activated:Connect(onActivated)
update()
AutoRotate
自动旋转属性描述是否 Humanoid 会自动旋转到面向他们移动的方向。当设置为真时,角色模型会逐渐转向面向其移动方向,跟随人形走动。当设置为 false 时,角色模型将继续保持在其当前旋转中,除非对 HumanoidRootPart 应用了旋转力。
如果角色模型是玩家的角色,那么人形的旋转行为受到人形旋转类型属性的影响。
当自动旋转属性设置为真时,旋转类型属性对人形的旋转有以下影响:
<th>行为</th><th>上下文</th></tr></thead><tbody><tr><td>运动相对</td><td /><td /></tr><tr><td>相机关系</td><td>角色将旋转到相镜头的方向面对。</td><td>玩家将相机放大到第一人称视角,或者他们处于换挡锁定模式。</td></tr></tbody>
旋转类型 |
---|
代码示例
This script adds the functionality of a button to a part, which switches the AutoRotate property of whoever touches it.
local button = script.Parent
local enabled = true
local ON_COLOR = BrickColor.Green()
local OFF_COLOR = BrickColor.Red()
local function touchButton(humanoid)
if enabled then
enabled = false
button.BrickColor = OFF_COLOR
if humanoid.AutoRotate then
print(humanoid:GetFullName() .. " can no longer auto-rotate!")
humanoid.AutoRotate = false
else
print(humanoid:GetFullName() .. " can now auto-rotate!")
humanoid.AutoRotate = true
end
task.wait(1)
button.BrickColor = ON_COLOR
enabled = true
end
end
local function onTouched(hit)
local char = hit:FindFirstAncestorWhichIsA("Model")
if char then
local humanoid = char:FindFirstChildOfClass("Humanoid")
if humanoid then
touchButton(humanoid)
end
end
end
button.Touched:Connect(onTouched)
button.BrickColor = ON_COLOR
AutomaticScalingEnabled
人形有六个子尺寸值,包括BodyDepthScale、BodyHeightScale、BodyProportionScale、BodyTypeScale、BodyWidthScale、HeadScale。更改这些原因之一的值会导致角色的身体部位和配件变更尺寸,但仅当 AutomaticScalingEnabled 为真时才会如此。
CameraOffset
相机偏移属性指定相镜头的主题位置偏移时其 Camera.CameraSubject 设置为此 Humanoid 。
抵消在对象空间中,相对于人形的 HumanoidRootPart 的方向。例如,一个抵消 Vector3 值 (0, 10, 0) 将玩家的相机抵消到玩家的人形上的 10 格。
代码示例
This LocalScript makes the camera bobble as the player's character walks around, utilizing both the Humanoid's CameraOffset and MoveDirection. It should be parented inside of the StarterCharacterScripts so that it is distributed into a player's character as expected.
local RunService = game:GetService("RunService")
local playerModel = script.Parent
local humanoid = playerModel:WaitForChild("Humanoid")
local function updateBobbleEffect()
local now = tick()
if humanoid.MoveDirection.Magnitude > 0 then -- Is the character walking?
local velocity = humanoid.RootPart.Velocity
local bobble_X = math.cos(now * 9) / 5
local bobble_Y = math.abs(math.sin(now * 12)) / 5
local bobble = Vector3.new(bobble_X, bobble_Y, 0) * math.min(1, velocity.Magnitude / humanoid.WalkSpeed)
humanoid.CameraOffset = humanoid.CameraOffset:lerp(bobble, 0.25)
else
-- Scale down the CameraOffset so that it shifts back to its regular position.
humanoid.CameraOffset = humanoid.CameraOffset * 0.75
end
end
RunService.RenderStepped:Connect(updateBobbleEffect)
DisplayDistanceType
显示距离类型 属性控制了人形名称和生命值显示的距离行为。这个属性使用 Enum.HumanoidDisplayDistanceType 枚举设置,有三个可用值,每个都有自己的规则集:
- 当设置为 None 时,人形的名称和生命条在任何情况下都不会出现。
请参阅角色名称/生命值显示获取关于控制角色名称和生命值条的外观的详细指南。
代码示例
This example demonstrates how to set a Humanoid's Humanoid.DisplayerDistanceType, Humanoid.HealthDisplayDistance, and Humanoid.NameDisplayDistance properties. These properties determine how a humanoid's healthbar and name are rendered for a player.
First, we change the DisplayDistanceType to Viewer using Enum.HumanoidDisplayDistanceType. When set to viewer, the humanoid's Name and healthbar will be displayed based on the distance settings of the humanoid viewing them.
Then, the humanoid's HealthDisplayDistance is set to 0. Setting the property to 0 hides the healthbar completely. It is not displayed at any distance.
Finally, the humanoid's NameDisplayDistance is set to 100. This means that the humanoid's name will be visible to other humanoid's within 100 studs.
This example should work as expected when placed inside a Script that is a child of the humanoid.
local humanoid = script.Parent
humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.Viewer
humanoid.HealthDisplayDistance = 0
humanoid.NameDisplayDistance = 100
DisplayName
DisplayName 是决定捕捉到人形时显示人形名称的属性。默认情况下,新的人形将拥有空字符串的值。如果 DisplayName 是空字符串,机器人的名称显示将默认为机器人的父元素辈名称属性。
玩家角色加载
当玩家加载他们的角色时,是否自动或通过使用 LoadCharacter() ,由引擎创建的人形将拥有其 DisplayName 属性设置为玩家的 DisplayName 属性。
新手角色和新手人形oid子
当 Humanoid 命名为 StarterHumanoid 被父辈到 StarterPlayer 时,或者当 Humanoid 存在于命名为 StarterCharacter 的模型中时,显示名称属性在游戏中玩家加载角色时将被尊重。引擎只会覆盖人形的 DisplayName 属性与玩家的 DisplayName 属性,如果 Humanoid.DisplayName 的 StarterHumanoid 是空字符串。
EvaluateStateMachine
FloorMaterial
这是一个只读属性,描述当前站在Enum.Material上的Humanoid。它与常规的 Parts 和 Terrain 体素兼容。
下面的代码示例显示了如何使用 Object:GetPropertyChangedSignal() 收听此属性何时更改。当人形所站的材料发生变化时,它会打印一个消息,指示新材料被站在上面。
local Humanoid = route.to.humanoid
Humanoid:GetPropertyChangedSignal("FloorMaterial"):Connect(function()
print("New value for FloorMaterial: " .. tostring(Humanoid.FloorMaterial))
end)
警告
- 当 Humanoid 不站在楼层上时,该属性的值将设置为 空气。
- 这发生因为枚列属性不能有空值。
- 虽然在实践中,零件不应该使用该材料,但如果零件的材料设置为空气,可能会造成一些混乱。
- Humanoid 的角色模型必须能够碰撞地板,否则不会被检测到。
- 您不能测试 Humanoid 是否具有这个属性进行游泳。您应该使用其 Humanoid:GetState() 函数。
Health
该属性代表 Humanoid 当前的健康状况。值仅限于 0 和 MaxHealth 之间的范围。如果人形已死亡,该属性将一直设置为 0。
请注意,TakeDamage() 函数可以用来从 Health 减去属性,而不是直接设置属性。
生命值恢复
默认情况下,被动生命重生脚本会自动插入到人形怪物中。这会导致非死亡玩家角色每秒恢复 1% 的 MaxHealth要禁用此重生行为,请将空的 Script 命名为 生命值 的 StarterCharacterScripts 添加到 。
生命条显示
当 Health 小于 MaxHealth 时,体验中显示健康条。生命条的显示行为取决于 HealthDisplayDistance 和 HealthDisplayType。
请参阅角色名称/生命值显示获取关于控制角色名称和生命值条的外观的详细指南。
死亡
当角色的生命值达到 0 时,Humanoid 自动转到 Enum.HumanoidStateType.Dead 状态。在这种状态下,Health被锁定为0;然而,没有错误或警告设置死机器人的Health值为正数非零值。
HealthDisplayDistance
该属性是与 DisplayDistanceType 属性一起使用的数字,用于控制从哪里可以看到人形生物的生命条的距离。
请参阅角色名称/生命值显示获取关于控制角色名称和生命值条的外观的详细指南。
HealthDisplayType
此属性控制何时允许显示一个人形怪物的生命条。默认情况下,此属性设置为 DisplayWhenDamaged ,使生命条仅在人形的 Health 小于其 MaxHealth 时显示。它还可以设置为 AlwaysOn ,使生命条始终显示,或 AlwaysOff ,使其永远不显示。
请注意,此属性与人形的 HealthDisplayDistance 属性独立,负责在特定距离处使健康条消失。如果 Humanoid.HealthDisplayType|HealthDisplayType 设置为 AlwaysOn,它仍然会依据如何配置 HealthDisplayDistance 而褪色。
请参阅角色名称/生命值显示获取关于控制角色名称和生命值条的外观的详细指南。
HipHeight
确定当人形站立时,距离地面的 RootPart 应为何距离。RigType 影响这个属性的行为方式。
对于 R15 机械人,一个合适的臀部高度已预设,以确保 RootPart 的高度正确。不使用腿的高度。人形的总高度可以用以下方式描述:
Height = (0.5 * RootPart.Size.Y) + HipHeight
对于 R6 架构,HipHeight 而不是描述相对偏移值。人形的总高度可以用以下公式描述:
Height = LeftLeg.Size.Y + (0.5 * RootPart.Size.Y) + HipHeight
Jump
如果 true , Humanoid 跳跃以相等于 Humanoid.JumpPower 或 Humanoid.JumpHeight 高度的向上力,取决于 Humanoid.UseJumpPower 的值。
JumpHeight
为 Humanoid 跳提供高度控制,以螺柱为单位。该属性的初始值由 StarterPlayer.CharacterJumpHeight 的值决定,默认值为 7.2。
虽然将此属性设置为 0 将有效防止机器人跳跃,但建议通过禁用 状态来禁用跳跃。
此属性只能在 属性窗口 中显示,如果 设置为 false ,否则无关,否则将使用 。
JumpPower
决定在跳跃时对 Humanoid 施加多少向上力。该属性的初始值由 StarterPlayer.CharacterJumpPower 的值决定,默认值为 50,并受到 0 和 1000 之间的限制。请注意,跳跃也受到重力导致的加速度影响的 Workspace.Gravity 属性。
虽然将此属性设置为 0 将有效防止机器人跳跃,但建议通过禁用 状态来禁用跳跃。
此属性只能在 属性窗口 中显示,如果 设置为 true ,否则无关,否则将使用 。
MaxHealth
人形的最大值 Health 。
该属性的值与 Health 属性一起用于确定默认健康条显示的大小当一个人形怪物的 Health 达到 MaxHealth 时,其生命条可能不会显示,取决于其 HealthDisplayType 属性。
MaxSlopeAngle
该属性决定了人形可以攀爬的最大坡度角度。如果斜坡的角度大于人形的 MaxSlopeAngle,它们将滑行到斜坡。
当角色生成时,该属性根据 StarterPlayer.CharacterMaxSlopeAngle 的值设置。
该属性的值被限制在 0° 和 89° 之间。默认为 89°,因此人形可以默认攀登任何他们想要的坡度。
代码示例
The example below demonstrates the effect of the MaxSlopAngle property by limiting the maximum slope the Players.LocalPlayer can walk up to 30°. The local player will slide down any slope greater than 30°.
This code below works as expected when placed in a LocalScript.
local player = game.Players.LocalPlayer
local char = player.CharacterAdded:wait()
local h = char:FindFirstChild("Humanoid")
h.MaxSlopeAngle = 30
MoveDirection
移动方向 是一个只读属性,描述一个单位向量或零长度向量中的方向,作为一个单位向量或零长度矢量力。方向在世界空间中被描述。
因为这个属性是只读的,所以不能由 Script 或 LocalScript 设置。
代码示例
This LocalScript makes the camera bobble as the player's character walks around, utilizing both the Humanoid's CameraOffset and MoveDirection. It should be parented inside of the StarterCharacterScripts so that it is distributed into a player's character as expected.
local RunService = game:GetService("RunService")
local playerModel = script.Parent
local humanoid = playerModel:WaitForChild("Humanoid")
local function updateBobbleEffect()
local now = tick()
if humanoid.MoveDirection.Magnitude > 0 then -- Is the character walking?
local velocity = humanoid.RootPart.Velocity
local bobble_X = math.cos(now * 9) / 5
local bobble_Y = math.abs(math.sin(now * 12)) / 5
local bobble = Vector3.new(bobble_X, bobble_Y, 0) * math.min(1, velocity.Magnitude / humanoid.WalkSpeed)
humanoid.CameraOffset = humanoid.CameraOffset:lerp(bobble, 0.25)
else
-- Scale down the CameraOffset so that it shifts back to its regular position.
humanoid.CameraOffset = humanoid.CameraOffset * 0.75
end
end
RunService.RenderStepped:Connect(updateBobbleEffect)
NameDisplayDistance
名称显示距离 属性是用于与Humanoid.DisplayDistanceType一起控制人形名称可见距离的数字。
请参阅角色名称/生命值显示获取关于控制角色名称和生命值条的外观的详细指南。
NameOcclusion
控制是否可以在墙或其他对象后面看到机器人的名称和生命条。该属性是 Enum.NameOcclusion 值,可以配置以排除所有名称、敌人名称或完全禁用封闭。
在某些情况下,如果 LocalPlayer 没有与它相关的 Humanoid,这个属性反而适用于主题 Humanoid。
请参阅角色名称/生命值显示获取关于控制角色名称和生命值条的外观的详细指南。
代码示例
In the below example, Player|Players will not be able to see each other's Player.Character names when they are obscured behind BasePart|BaseParts.
local Players = game:GetService("Players")
local function onCharacterAdded(character)
local humanoid = character:WaitForChild("Humanoid")
humanoid.NamOcclusion = Enum.NameOcclusion.OccludeAll
end
local function onPlayerAdded(player)
player.CharacterAdded:Connect(onCharacterAdded)
end
Players.PlayerAdded:Connect(onPlayerAdded)
PlatformStand
决定是否 Humanoid 目前处于 Enum.HumanoidStateType.PlatformStanding 状态。当真实时,人形机器人处于一个状态,在那里它可以自由下降并且无法移动工具。这种状态与坐下相似,除了跳跃不会释放人形状态。
RigType
装备类型 描述是否使用遗产的 R6 角色骨架,或更新的 R15 角色骨架。
R6 装备使用 6 个可见的 Parts ,而 R15 装备使用 15 个可见的 Parts 。R15 装备比 R6 装备有更多的关节,使它们在动画时更具多功能性。
请注意,如果此属性设置不正确,Humanoid将无法正常运行。例如,如果 R15 人形的 装备类型 设置为 R6,那么 将死亡,因为没有被调用的 连接到被称为 躯干 的 头部 。
RootPart
对人形的 人形根部件 对象的引用,这是控制人形在 3D 世界中移动的根驱动部分。这部分通常是隐形的。
对于 R15 角色,Model.PrimaryPart 模型的 Player.Character 设置为 人形根部件 。
对于 R6 角色,Model.PrimaryPart 设置为 Head 部分。
SeatPart
座椅部分是指当前Humanoid坐在的座椅的参考,如果有。该属性的值可以是 Seat 或 VehicleSeat 。如果机器人人没有当前坐在座位上,将会是 为零 。
注意:
- 要确定 Humanoid 是否当前坐着或不坐着,请参阅 Humanoid.Sit
Sit
坐下属性是一个 boolean,表示 Humanoid 是否当前坐下。Humanoids 可以通过将此属性值设置为真实来强制进入坐卧状态。如果 Humanoid 在坐下状态下没有附在座椅上,它的腿上会绊倒,而没有碰撞。一个 Humanoid 可以通过跳跃从坐下状态中逃脱。
注意:
- 可以通过连接到 Humanoid.Seated 事件来检测人形坐下时。
TargetPoint
不要使用 此属性仅与启用实验模式的情况下工作,已全面停止使用。
该属性描述了空间中的 3D 位置,在该位置上 Player 控制这个 Humanoid 最后点击使用了装备的 Tool。
此属性主要由经典工具使用,以确定人形在激活工具时正在瞄准什么。如果你给一个 NPC 一个经典的火箭发射器,设置其 目标点 ,然后调用工具的 Tool:Activate() 函数,你可以让 NPC 在目标点发射火箭。
UseJumpPower
当角色生成时,该属性根据 StarterPlayer.CharacterUseJumpPower 的值设置,默认为真值。
当跳跃时,将此设置为真值,Humanoid.JumpHeight 值将用于确保人形跳跃到那个高度。将此设置为 false,Humanoid.JumpPower 值用于应用向上的力量。
WalkSpeed
该属性描述了 Humanoid 的行走行速度,以每秒钟的单位计算。默认值为 StarterPlayer.CharacterWalkSpeed (16),即玩家角色每秒可以在任何方向移动 16 个钉子。
注释
WalkToPart
WalkToPart是指人形正在尝试到达的零件的参考。此属性通常在零件作为人形的 Humanoid:MoveTo() 函数的第二个参数传递时设置。
当 WalkToPart 设置并且一个人形正在努力达到零件,它将继续更新其 Vector3 目标成为零件位置,加上对象空间对零件旋转的 Humanoid.WalkToPoint 翻译。
这可以在 Luau 中描述为:
goal = humanoid.WalkToPart.CFrame:pointToObjectSpace(humanoid.WalkToPoint)
警告
- 设置 WalkToPart 的值不足以让一个人形开始跟随零件。
- 当人形被提示开始尝试达到目标时,当 WalkToPoint 的值更改时,它将被要求开始尝试达到目标。
- 这可能在未来改变。
- 如果机器人没有达到目标,其 达到目标状态 将在 8 秒后过期。
- 这是为了确保 NPC 不会被困在等待 Humanoid.MoveToFinished 发触发的状态。
- 如果你不想让这发生,你应该多次调用 MoveTo,以便时间限制继续重置。
WalkToPoint
WalkToPoint描述了机器人人试图到达的3D位置在空间中,之后由机器人的Humanoid:MoveTo()函数提示它这样做。
如果人形的 Humanoid.WalkToPart 被设置,目标通过将其与零件位置和旋转相对的 WalkToPoint 转换设置。如果没有设置 WalkToPart,那么人形将直接尝试达到由 WalkToPoint 指定的 3D 位置。
警告
- 为了启动机器人向其走动,必须将 WalkToPoint 的值更改为不同的值。
- 如果你想让一个人形走路到 0,0,0 , 你应该使用人形的 MoveTo 函数。
- 这可能在未来改变。
- 如果机器人没有达到目标,其 达到目标状态 将在 8 秒后过期。
- 这是为了确保 NPC 不会被困在等待 Humanoid.MoveToFinished 发触发的状态。
- 如果你不想让这发生,你应该多次调用 MoveTo,以便时间限制继续重置。
代码示例
This code sample includes a function that avoids the 8 second timeout on Humanoid:MoveTo() by calling Humanoid:MoveTo() again before the timeout elapses. It also includes an optional andThen parameter where developers can pass a function to be called when the humanoid reaches its destination.
local function moveTo(humanoid, targetPoint, andThen)
local targetReached = false
-- listen for the humanoid reaching its target
local connection
connection = humanoid.MoveToFinished:Connect(function(reached)
targetReached = true
connection:Disconnect()
connection = nil
if andThen then
andThen(reached)
end
end)
-- start walking
humanoid:MoveTo(targetPoint)
-- execute on a new thread so as to not yield function
task.spawn(function()
while not targetReached do
-- does the humanoid still exist?
if not (humanoid and humanoid.Parent) then
break
end
-- has the target changed?
if humanoid.WalkToPoint ~= targetPoint then
break
end
-- refresh the timeout
humanoid:MoveTo(targetPoint)
task.wait(6)
end
-- disconnect the connection if it is still connected
if connection then
connection:Disconnect()
connection = nil
end
end)
end
local function andThen(reached)
print((reached and "Destination reached!") or "Failed to reach destination!")
end
moveTo(script.Parent:WaitForChild("Humanoid"), Vector3.new(50, 0, 50), andThen)
方法
AddAccessory
这个方法将指定的 Accessory 附加到人形的父父元素。
当调用此方法时,通过搜索人形的父中找到一个 Accessory 与配饰品的 Attachment 的名称相同的 Attachment 来附加到角色,以便在人形的 处理 中找到一个 Part 。如果找到一个, 处理 部分将使用Weld与父级的Attachment,并配置焊接以使Attachments。
如果找不到必需的 Attachment ,那么 Accessory 将继续作为人形的父级,但将被解开。
通常,配件焊接在服务器上创建,但在某些情况下可以在客户端上创建。在这些情况下,客户端调用 AddAccessory() 可能不总能产生所需的行为,您可以使用 BuildRigFromAttachments() 强制产生预期的焊接作品。
参数
返回
代码示例
This script generates the "Clockwork's Shades" Accessory from scratch, and then attaches it to the player's character using Humanoid.AddAccessory You should paste this code into a regular script, and then parent it inside of the StarterPlayer's StarterCharacterScripts folder.
local playerModel = script.Parent
local humanoid = playerModel:WaitForChild("Humanoid")
local clockworksShades = Instance.new("Accessory")
clockworksShades.Name = "ClockworksShades"
local handle = Instance.new("Part")
handle.Name = "Handle"
handle.Size = Vector3.new(1, 1.6, 1)
handle.Parent = clockworksShades
local faceFrontAttachment = Instance.new("Attachment")
faceFrontAttachment.Name = "FaceFrontAttachment"
faceFrontAttachment.Position = Vector3.new(0, -0.24, -0.45)
faceFrontAttachment.Parent = handle
local mesh = Instance.new("SpecialMesh")
mesh.Name = "Mesh"
mesh.Scale = Vector3.new(1, 1.3, 1)
mesh.MeshId = "rbxassetid://1577360"
mesh.TextureId = "rbxassetid://1577349"
mesh.Parent = handle
humanoid:AddAccessory(clockworksShades)
BuildRigFromAttachments
该方法组装了一个由 Motor6D 个连接组成的树,用于 Humanoid 。 Motor6D 连接是用于播放 Animations 的必要条件。
从人形开始 RootPart , 这个方法收集所有 Attachments 在当前部分中被命名为 RigAttachment 的父辈。然后它在共享相同名称的角色中搜索匹配的附件。使用这两个附件,根据附件中的零件和附件的 Motor6D 生成一个关节,该关节基于附件中的零件和附件的 CFrame 生成。
Humanoid:BuildRigFromAttachments() 还可以缩放角色并设置身体颜色。
返回
代码示例
A Lua port of the Humanoid's BuildRigFromAttachments function, so that the recursive behavior of the function can be seen.
local function createJoint(jointName, att0, att1)
local part0, part1 = att0.Parent, att1.Parent
local newMotor = part1:FindFirstChild(jointName)
if not (newMotor and newMotor:IsA("Motor6D")) then
newMotor = Instance.new("Motor6D")
end
newMotor.Name = jointName
newMotor.Part0 = part0
newMotor.Part1 = part1
newMotor.C0 = att0.CFrame
newMotor.C1 = att1.CFrame
newMotor.Parent = part1
end
local function buildJointsFromAttachments(part, characterParts)
if not part then
return
end
-- first, loop thru all of the part's children to find attachments
for _, attachment in pairs(part:GetChildren()) do
if attachment:IsA("Attachment") then
-- only do joint build from "RigAttachments"
local attachmentName = attachment.Name
local findPos = attachmentName:find("RigAttachment")
if findPos then
-- also don't make double joints (there is the same named
-- rigattachment under two parts)
local jointName = attachmentName:sub(1, findPos - 1)
if not part:FindFirstChild(jointName) then
-- try to find other part with same rig attachment name
for _, characterPart in pairs(characterParts) do
if part ~= characterPart then
local matchingAttachment = characterPart:FindFirstChild(attachmentName)
if matchingAttachment and matchingAttachment:IsA("Attachment") then
createJoint(jointName, attachment, matchingAttachment)
buildJointsFromAttachments(characterPart, characterParts)
break
end
end
end
end
end
end
end
end
local function buildRigFromAttachments(humanoid)
local rootPart = humanoid.RootPart
assert(rootPart, "Humanoid has no HumanoidRootPart.")
local characterParts = {}
for _, descendant in ipairs(humanoid.Parent:GetDescendants()) do
if descendant:IsA("BasePart") then
table.insert(characterParts, descendant)
end
end
buildJointsFromAttachments(rootPart, characterParts)
end
local humanoid = script.Parent:WaitForChild("Humanoid")
buildRigFromAttachments(humanoid)
A script that generates an R15 character from scratch using a package's assetId.
local AssetService = game:GetService("AssetService")
local InsertService = game:GetService("InsertService")
local MarketplaceService = game:GetService("MarketplaceService")
local PACKAGE_ASSET_ID = 193700907 -- Circuit Breaker
local function addAttachment(part, name, position, orientation)
local attachment = Instance.new("Attachment")
attachment.Name = name
attachment.Parent = part
if position then
attachment.Position = position
end
if orientation then
attachment.Orientation = orientation
end
return attachment
end
local function createBaseCharacter()
local character = Instance.new("Model")
local humanoid = Instance.new("Humanoid")
humanoid.Parent = character
local rootPart = Instance.new("Part")
rootPart.Name = "HumanoidRootPart"
rootPart.Size = Vector3.new(2, 2, 1)
rootPart.Transparency = 1
rootPart.Parent = character
addAttachment(rootPart, "RootRigAttachment")
local head = Instance.new("Part")
head.Name = "Head"
head.Size = Vector3.new(2, 1, 1)
head.Parent = character
local headMesh = Instance.new("SpecialMesh")
headMesh.Scale = Vector3.new(1.25, 1.25, 1.25)
headMesh.MeshType = Enum.MeshType.Head
headMesh.Parent = head
local face = Instance.new("Decal")
face.Name = "face"
face.Texture = "rbxasset://textures/face.png"
face.Parent = head
addAttachment(head, "FaceCenterAttachment")
addAttachment(head, "FaceFrontAttachment", Vector3.new(0, 0, -0.6))
addAttachment(head, "HairAttachment", Vector3.new(0, 0.6, 0))
addAttachment(head, "HatAttachment", Vector3.new(0, 0.6, 0))
addAttachment(head, "NeckRigAttachment", Vector3.new(0, -0.5, 0))
return character, humanoid
end
local function createR15Package(packageAssetId)
local packageAssetInfo = MarketplaceService:GetProductInfo(packageAssetId)
local character, humanoid = createBaseCharacter()
character.Name = packageAssetInfo.Name
local assetIds = AssetService:GetAssetIdsForPackage(packageAssetId)
for _, assetId in pairs(assetIds) do
local limb = InsertService:LoadAsset(assetId)
local r15 = limb:FindFirstChild("R15")
if r15 then
for _, part in pairs(r15:GetChildren()) do
part.Parent = character
end
else
for _, child in pairs(limb:GetChildren()) do
child.Parent = character
end
end
end
humanoid:BuildRigFromAttachments()
return character
end
local r15Package = createR15Package(PACKAGE_ASSET_ID)
r15Package.Parent = workspace
ChangeState
这种方法使 Humanoid 进入指定的 Enum.HumanoidStateType ,描述了 Humanoid 目前正在进行的活动。
请查看 Enum.HumanoidStateType 页面,了解特定状态的更多信息,因为一些具有不直观的名称。例如,Enum.HumanoidStateType.Running 描述了一个状态,在那里机器人的腿在地面上,包括静止时。
由于 Humanoid 的默认行为,一些状态在设置时会自动更改。例如:
- 将状态设置为Enum.HumanoidStateType.Swimming时,当机器人不在水中时,将自动设置为Enum.HumanoidStateType.GettingUp。
- 由于未使用,将状态设置为 Enum.HumanoidStateType.PlatformStanding 将导致机器人状态自动设置为 Enum.HumanoidStateType.Running。
请注意,为了使用此方法设置 Humanoid 状态,您必须从 LocalScript 进行,客户端必须拥有 网络所有权 的 Player.Character 。或者,您可以从服务器端调用此方法 Script ,但服务器必须拥有玩家角色的网络所有权。
还见 Humanoid:SetStateEnabled() 启用或禁用特定状态,以及 Humanoid:GetState() 获取当前的人形状态。
参数
该 Enum.HumanoidStateType 由 Humanoid 执行。
返回
代码示例
This code, when placed inside a LocalScript in StarterPlayer.StarterCharacterScripts, will allow the player's character to perform a double jump.
local UserInputService = game:GetService("UserInputService")
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local doubleJumpEnabled = false
humanoid.StateChanged:Connect(function(_oldState, newState)
if newState == Enum.HumanoidStateType.Jumping then
if not doubleJumpEnabled then
task.wait(0.2)
if humanoid:GetState() == Enum.HumanoidStateType.Freefall then
doubleJumpEnabled = true
end
end
elseif newState == Enum.HumanoidStateType.Landed then
doubleJumpEnabled = false
end
end)
UserInputService.InputBegan:Connect(function(inputObject)
if inputObject.KeyCode == Enum.KeyCode.Space then
if doubleJumpEnabled then
if humanoid:GetState() ~= Enum.HumanoidStateType.Jumping then
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
task.spawn(function()
doubleJumpEnabled = false
end)
end
end
end
end)
EquipTool
以下示例将导致一个 Player 装备名为 Workspace 的工具 ** 。
local Players = game:GetService("Players")local Workspace = game:GetService("Workspace")local player = Players:FindFirstChildOfClass("Player")if player and player.Character thenlocal humanoid = player.Character:FindFirstChildWhichIsA("Humanoid")if humanoid thenlocal tool = Workspace:FindFirstChild("Tool")if tool thenhumanoid:EquipTool(tool)endendend
当此方法调用时,Humanoid 将首先自动卸下当前已装备的所有Tools。
尽管他们会被装备,Tools 对于Tool.RequiresHandle 是true 将不会功手柄,无论这种方法是用于装备他们还是不是。
还见:
- 要卸下工具,请使用 Humanoid:UnequipTools()
参数
返回
GetAccessories
该方法返回一个阵列 Accessory 对象,这些对象是机器人的父辈目前穿着的。所有这样的 Accessory 对象将被包含,无论是否附加。
如果 Humanoid 没有 Accessory 对象,将返回一个空列表。
还请参阅Humanoid:AddAccessory()附加一个Accessory到人形的父元素。
返回
代码示例
This code sample will wait for accessories to fully load, print out how many there are, and then destroy them all.
local Players = game:GetService("Players")
local function onPlayerAddedAsync(player)
local connection = player.CharacterAppearanceLoaded:Connect(function(character)
-- All accessories have loaded at this point
local humanoid = character:FindFirstChildOfClass("Humanoid")
local numAccessories = #humanoid:GetAccessories()
print(("Destroying %d accessories for %s"):format(numAccessories, player.Name))
humanoid:RemoveAccessories()
end)
-- Make sure we disconnect our connection to the player after they leave
-- to allow the player to get garbage collected
player.AncestryChanged:Wait()
connection:Disconnect()
end
for _, player in Players:GetPlayers() do
task.spawn(onPlayerAddedAsync, player)
end
Players.PlayerAdded:Connect(onPlayerAddedAsync)
GetAppliedDescription
该方法返回人形的缓存 HumanoidDescription 的副本,描述其当前外观。这可以用来快速确定角色的外观,并使用 Humanoid:ApplyDescription() 方法将其外观分配给其他角色。
还看到也看到
- Players:GetHumanoidDescriptionFromUserId() 返回描述传入用户的虚拟形象的 HumanoidDescription
- Players:GetHumanoidDescriptionFromOutfitId() 返回一个 HumanoidDescription 其参数被初始化匹配传入服务器端装备资产的参数。
- Player:LoadCharacterWithHumanoidDescription() 生成一个拥有传递到的 HumanoidDescription 外观的玩家。
返回
GetBodyPartR15
这个方法返回什么 Enum.BodyPartR15 是 Part 或 Enum.BodyPartR15.Unknown 如果零件不是 R15 身体部分。这种方法允许开发人员无视实际身体部分名称,独立地检索玩家身体部分,而不是返回枚列。
它可以与 Humanoid:ReplaceBodyPartR15() 结合使用。例如,如果玩家的身体部分触碰到某物,这个函数将返回获取零件实例。开发者可以查看身体的哪一部分,例如头部或手臂。然后根据那部分是什么,开发者可以执行一些游戏行动或替换那部分为其他部分 - 也许显示伤害。
这个方法可能对那些需要命中位置的游戏有用。例如,它可以用于确定玩家是否在腿部受伤,然后根据受伤情况减慢他们的速度。
参数
指定的部件正在被检查,看看它是否是 R15 身体部件。
返回
指定零件的 R15 身体部分类型或未知,如果零件不是身体部分。
GetLimb
该方法返回与给定 Enum.Limb 相关的枚举 Part 。它适用于 R15 和 R6 装备,例如:
-- 对于 R15print(humanoid:GetLimb(character.LeftUpperLeg)) -- Enum.Limb.左腿print(humanoid:GetLimb(character.LeftLowerLeg)) -- Enum.Limb.左腿print(humanoid:GetLimb(character.LeftFoot)) -- Enum.Limb.左腿-- 对于 R6print(humanoid:GetLimb(character:FindFirstChild("Left Leg"))) -- Enum.Limb.LeftLeg
请注意,如果零件的父不设置为机器人的父元素,那么 Humanoid:GetLimb() 将抛出错误。
参数
返回
代码示例
Put this in a LocalScript. The output will vary based on if the humanoid is R6 or R15.
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
for _, child in pairs(character:GetChildren()) do
local limb = humanoid:GetLimb(child)
if limb ~= Enum.Limb.Unknown then
print(child.Name .. " is part of limb " .. limb.Name)
end
end
GetState
该方法返回人形的当前 ,描述人形目前正在进行的活动,例如跳跃或游泳。
还见 Humanoid:SetStateEnabled() 启用或禁用特定状态,以及 Humanoid:ChangeState() 改变当前的人形状态。
返回
代码示例
This code, when placed inside a LocalScript in StarterPlayer.StarterCharacterScripts, will allow the player's character to perform a double jump.
local UserInputService = game:GetService("UserInputService")
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local doubleJumpEnabled = false
humanoid.StateChanged:Connect(function(_oldState, newState)
if newState == Enum.HumanoidStateType.Jumping then
if not doubleJumpEnabled then
task.wait(0.2)
if humanoid:GetState() == Enum.HumanoidStateType.Freefall then
doubleJumpEnabled = true
end
end
elseif newState == Enum.HumanoidStateType.Landed then
doubleJumpEnabled = false
end
end)
UserInputService.InputBegan:Connect(function(inputObject)
if inputObject.KeyCode == Enum.KeyCode.Space then
if doubleJumpEnabled then
if humanoid:GetState() ~= Enum.HumanoidStateType.Jumping then
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
task.spawn(function()
doubleJumpEnabled = false
end)
end
end
end
end)
GetStateEnabled
获取状态启用方法返回是否启用 Enum.HumanoidStateType 对于 Humanoid 。
人形状态描述了人形目前正在进行的活动。
当特定的 Enum.HumanoidStateType 被禁用时,人形永远无法进入那个状态。无论使用 Humanoid:ChangeState() 或 Roblox 内部人形验证码尝试更改状态,这都是真实的。
还见:
- 对于启用或禁用人形状态时触发的事件,请参阅 Humanoid.StateEnabledChanged
- 要启用或禁用 Humanoid 状态使用 Humanoid:SetStateEnabled()
参数
给定的 Enum.HumanoidStateType 。
返回
是否启用给定的 Enum.HumanoidStateType。
代码示例
The code below sets the value of the humanoid jumping state to false using Humanoid:SetStateEnabled() and then retrieves and prints the value of this state (false) using Humanoid:GetStateEnabled().
local humanoid = script.Parent:WaitForChild("Humanoid")
-- Set state
humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
-- Get state
print(humanoid:GetStateEnabled(Enum.HumanoidStateType.Jumping)) -- false
Move
这种方法使 Humanoid 走在指定的 Vector3 方向。
默认情况下,方向是以世界单条款表示的,但如果 relativeToCamera 参数是 true ,方向是相对于 CFrame 的 CurrentCamera 。由于在 Roblox 中将负向 Z 方向视为“向前”,以下代码将使机器人在 CurrentCamera 方向走行。
humanoid:Move(Vector3.new(0, 0, -1), true)
当调用此方法时,Humanoid将移动,直到方法再次调用。然而,此方法在下一帧中将被 Roblox 的默认角色控制脚本覆盖。这可以通过每帧调用此函数 RunService:BindToRenderStep() (见例子) 或覆盖控制脚本在 StarterPlayerScripts 来避免。
该方法可以在服务器上调用,但仅当服务器拥有人形机器人的装配网络所有权时才应该这样做。
还看到 Humanoid:MoveTo() 将一个 Humanoid 步行到一个点,以及 Player:Move() 有效调用此函数。
参数
要走的方向。
如果 moveDirection 参数应与 CurrentCamera 相对,设置为 true 。
返回
代码示例
This code sample uses the Humanoid:Move() function to make the player's Character walk in the direction of the Camera. RunService:BindToRenderStep() is required here as the default control scripts will overwrite the player's movement every frame.
To run this sample, place it inside a LocalScript parented to StarterCharacterScripts.
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
RunService:BindToRenderStep("move", Enum.RenderPriority.Character.Value + 1, function()
if player.Character then
local humanoid = player.Character:FindFirstChild("Humanoid")
if humanoid then
humanoid:Move(Vector3.new(0, 0, -1), true)
end
end
end)
MoveTo
这种方法使 Humanoid 尝试走到指定位置,通过设置 Humanoid.WalkToPoint 和 Humanoid.WalkToPart 属性来实现。
位置和零件参数与Humanoid.WalkToPoint和Humanoid.WalkToPart将设置的内容相对应。
如果 部分 参数被指定,那么 Humanoid 仍然会尝试走到那一点。然而,如果零件移动,那么指向 的点将移至与零件相同的位置 。如果 部分 参数未指定,那么走向 Humanoid 的位置不会改变。
如果机器人没有达到目标,其 达到目标状态 将在 8 秒后过期。这是为了确保 NPC 不会被困在等待 Humanoid.MoveToFinished 发触发的状态。如果你不想让这发生,你应该多次调用 MoveTo,以便时间限制继续重置。
MoveTo() 结束,如果以下任何条件适用:
角色到达目的地。存在一个 ~1 格间隙,用于抵消各种人形速度和帧率。
角色卡住并且八秒计时器到期了。
值 Humanoid.WalkToPoint 或 Humanoid.WalkToPart 发生变化。
一个脚本调用 Humanoid:Move() 使用新的 moveDirection 参数。
参数
设置 Humanoid.WalkToPoint 的位置。
将 BasePart 设置为 Humanoid.WalkToPart 。
返回
代码示例
This code sample includes a function that avoids the 8 second timeout on Humanoid:MoveTo() by calling Humanoid:MoveTo() again before the timeout elapses. It also includes an optional andThen parameter where developers can pass a function to be called when the humanoid reaches its destination.
local function moveTo(humanoid, targetPoint, andThen)
local targetReached = false
-- listen for the humanoid reaching its target
local connection
connection = humanoid.MoveToFinished:Connect(function(reached)
targetReached = true
connection:Disconnect()
connection = nil
if andThen then
andThen(reached)
end
end)
-- start walking
humanoid:MoveTo(targetPoint)
-- execute on a new thread so as to not yield function
task.spawn(function()
while not targetReached do
-- does the humanoid still exist?
if not (humanoid and humanoid.Parent) then
break
end
-- has the target changed?
if humanoid.WalkToPoint ~= targetPoint then
break
end
-- refresh the timeout
humanoid:MoveTo(targetPoint)
task.wait(6)
end
-- disconnect the connection if it is still connected
if connection then
connection:Disconnect()
connection = nil
end
end)
end
local function andThen(reached)
print((reached and "Destination reached!") or "Failed to reach destination!")
end
moveTo(script.Parent:WaitForChild("Humanoid"), Vector3.new(50, 0, 50), andThen)
RemoveAccessories
这个方法移除所有由人形的父父元素穿戴的 Accessory 对象。对于玩家 Characters ,这将移除所有帽子和其他配件。
这种方法通过调用 Accessory 来移除对象 Instance:Destroy() ,意味着配件的 Parent 被设置为 nil 并被锁定。
还请参阅 附加一个 ,以及 获得所有属于一个 的对象。
返回
代码示例
This code sample will wait for accessories to fully load, print out how many there are, and then destroy them all.
local Players = game:GetService("Players")
local function onPlayerAddedAsync(player)
local connection = player.CharacterAppearanceLoaded:Connect(function(character)
-- All accessories have loaded at this point
local humanoid = character:FindFirstChildOfClass("Humanoid")
local numAccessories = #humanoid:GetAccessories()
print(("Destroying %d accessories for %s"):format(numAccessories, player.Name))
humanoid:RemoveAccessories()
end)
-- Make sure we disconnect our connection to the player after they leave
-- to allow the player to get garbage collected
player.AncestryChanged:Wait()
connection:Disconnect()
end
for _, player in Players:GetPlayers() do
task.spawn(onPlayerAddedAsync, player)
end
Players.PlayerAdded:Connect(onPlayerAddedAsync)
ReplaceBodyPartR15
动态地在一个人形身体中替换 R15/Rthro 肢体部分为不同的部分。该部分的尺寸会自动调整为正常。
这个方法对于在游戏中修改角色或从基础骨架中构建角色有用。相关方法 GetBodyPartR15 可以在使用此方法时很有用。
传入的零件名称应与传入的 BodyPartR15 Enum 名称匹配。
参数
要替换的身体部分。Enum.BodyPartR15.Unknown将失败。
返回
SetStateEnabled
该方法设置是否启用给定的 Enum.HumanoidStateType 对 Humanoid 。当特定 Enum.HumanoidStateType 被禁用时,Humanoid 将永远无法进入该状态。无论使用 Humanoid:ChangeState() 或 Roblox 内部 Humanoid 验证码码尝试更改状态,这都是真实的。
请注意,在服务器上使用 SetStateEnabled() 不会向客户端复制更改,也不会相反。
参数
需要启用或禁用的 Enum.HumanoidStateType。
true 如果 state 要启用,false 如果 state 要禁用。
返回
代码示例
The following sample will require a one second cooldown after a Humanoid has landed before it is able to jump again.
To try this sample, place it inside a LocalScript in StarterCharacterScripts|StarterPlayer.StarterCharacterScripts.
local character = script.Parent
local JUMP_DEBOUNCE = 1
local humanoid = character:WaitForChild("Humanoid")
local isJumping = false
humanoid.StateChanged:Connect(function(_oldState, newState)
if newState == Enum.HumanoidStateType.Jumping then
if not isJumping then
isJumping = true
humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
end
elseif newState == Enum.HumanoidStateType.Landed then
if isJumping then
isJumping = false
task.wait(JUMP_DEBOUNCE)
humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
end
end
end)
TakeDamage
这种方法降低 Humanoid.Health 的 Humanoid 对给定的 量 的保护,如果它不受到 ForceField 的保护
该方法接受负值参数 数量 的参数。这将增加人形的 Humanoid.Health .但是,这只有在没有 ForceField 存在时才会产生效果。
力场如何防止受到伤害
A Humanoid 被视为受到 ForceField 保护,如果 A ForceField 满足以下任一条件:
无论是否存在任何 现有的伤害,都必须直接设置 。
了解有关如何ForceFields保护Humanoids的更多信息,请参阅ForceField页。
参数
伤害或从 Humanoid.Health 中需要扣除的金额。
返回
代码示例
This code, put in a LocalScript, would make the local player take 99 damage only if a ForceField wasn't present.
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
humanoid:TakeDamage(99)
UnequipTools
未装备的 Tool 将被父辈到 Backpack 与 Player 相关的 Humanoid 。
如果没有配备 Tool,这个方法将无所作为。
虽然 Tools 可以由 NPC (非玩家角色) 装备,但这种方法只适用于 Humanoids 与相应的 Player 。这是因为需要一个 Backpack 对象来父级化未装备的 Tool 。
还见:
- 要装备一个 Tool 而不是,使用 Humanoid:EquipTool()
返回
代码示例
The following sample will bind the U key to unequip any Tool|Tools the Player currently has equipped.
To use this sample, place it inside a LocalScript within StarterPlayerScripts|StarterPlayer.StarterPlayerScripts.
local Players = game:GetService("Players")
local ContextActionService = game:GetService("ContextActionService")
local player = Players.LocalPlayer
ContextActionService:BindAction("unequipTools", function(_, userInputState)
if userInputState == Enum.UserInputState.Begin then
if player.Character then
local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid:UnequipTools()
end
end
end
end, false, Enum.KeyCode.U)
ApplyDescription
这种妥协方法使角色的外观与传入的 HumanoidDescription 匹配。通过的 HumanoidDescription 的副本被缓存为 HumanoidDescription 对于 Humanoid 。
该方法通过假设仅使用此方法来更改角色的外观而优化,在调用之间通过其他手段没有进行任何更改。如果在调用期间对角色进行了更改,则此方法可能不会使角色准确反映传入的 HumanoidDescription 。如果您想使用此方法与其他更新角色的方法结合, Humanoid:ApplyDescriptionReset() 将始终确保角色反映了传递的 HumanoidDescription。
还看到也看到
- Players:GetHumanoidDescriptionFromUserId() 返回描述传入用户的虚拟形象的 HumanoidDescription
- Players:GetHumanoidDescriptionFromOutfitId() 返回一个 HumanoidDescription 其参数被初始化匹配传入服务器端装备资产的参数。
- Player:LoadCharacterWithHumanoidDescription() 生成一个拥有传递到的 HumanoidDescription 外观的玩家。
参数
您想将角色设置匹配的 HumanoidDescription 实例。
返回
ApplyDescriptionReset
这种妥协方法使角色的外观与传入的 HumanoidDescription 相匹配,即使在外部更改后。通过的 HumanoidDescription 的副本被缓存为 HumanoidDescription 对于 Humanoid 。
这种方法始终确保字符反映了传入的 HumanoidDescription ,即使更改了不使用 HumanoidDescription 或 ApplyDescription() 系统的字符 (例如不使用 ApplyDescriptionReset() 或 )。这与 ApplyDescription() 优化的情况相反,可能会错误地应用 HumanoidDescription 如果角色通过除了 HumanoidDescription 系统之外的方式已被更改。
参数
您想将角色设置匹配的 HumanoidDescription 实例。
返回
活动
Climbing
当 Humanoid 的速度上升时发生火焰,当速度上升时更改。
Humanoids 可以攀登由 Parts 或 TrussParts 制成的梯子。
Humanoids 在 70% 的情况下攀登他们的 Humanoid.WalkSpeed .
当 Humanoid 停止攀爬时,这个事件并不总是以 0 的速度发射。
还见:
- 对于游泳和跑步,请参阅 Humanoid.Swimming 和 Humanoid.Running 事件
- 您还可以使用 Humanoid 事件检测当 Humanoid.StateChanged 正在攀登时
- 您可以使用 Humanoid:SetStateEnabled() 函数禁用攀爬
参数
代码示例
local Players = game:GetService("Players")
local function onCharacterClimbing(character, speed)
print(character.Name, "is climbing at a speed of", speed, "studs / second.")
end
local function onCharacterAdded(character)
character.Humanoid.Climbing:Connect(function(speed)
onCharacterClimbing(character, speed)
end)
end
local function onPlayerAdded(player)
player.CharacterAdded:Connect(onCharacterAdded)
end
Players.PlayerAdded:Connect(onPlayerAdded)
Died
此事件在 Humanoid 死亡时触发,通常在 Humanoid.Health 达到 0 时触发。这可能是由于从他们的头部中断连接,或直接设置健康属性造成的。
此事件只会发生,如果 Humanoid 是 Workspace 的子孙。如果 Dead Enum.HumanoidStateType 被禁用,它将不会触发生。
代码示例
The code below would print the player's name, followed by "has died!", whenever a player dies. For example, if the player was named "Shedletsky", "Shedletsky has died!" would be printed to the output when they died.
local Players = game:GetService("Players")
local function onPlayerAdded(player)
local function onCharacterAdded(character)
local humanoid = character:WaitForChild("Humanoid")
local function onDied()
print(player.Name, "has died!")
end
humanoid.Died:Connect(onDied)
end
player.CharacterAdded:Connect(onCharacterAdded)
end
Players.PlayerAdded:Connect(onPlayerAdded)
FallingDown
下降事件在 Humanoid 进入并离开 FallingDown Enum.HumanoidStateType 时触发。
将在启用 状态后的 3 秒进入状态 3 秒。当发生这种情况时,该事件将以 激活 值 为零 发射,而 Humanoid.GettingUp 将以 激活 值 为真 发射。
参数
描述是否 Humanoid 正在进入或离开 FallingDown Enum.HumanoidStateType 。
FreeFalling
当 Humanoid 进入或离开 Freefall``Enum.HumanoidStateType 时,该事件发生。
活动参数 激活 表示 Humanoid 是否进入或离开 Freefall 状态。
虽然 状态一般在 到达地面时结束,但这个事件可能会在 有效 与 错误 相等时发生,如果状态在 下降时被更改。因此,您应该使用 Humanoid.StateChanged 并倾听 Landed 状态在降落时工作出来,当 Humanoid 降落时。
参数
GettingUp
当 Humanoid 进入或离开 Enum.HumanoidStateType.GettingUp 状态时,这个事件会触发,即在 Humanoid 进入 FallingDown (3秒) 或 Ragdoll (1秒) 状态不久后激活的转换状态。
当 Humanoid 尝试重新上线时,此事件首先会发射一个 active 参数的 true 参数,很快再次发射一个 active 参数的 false 。
要强制 Humanoid 倒下,请使用 Humanoid:ChangeState() 函数与 Enum.HumanoidStateType.FallingDown 。
参数
HealthChanged
当 Humanoid.Health 发生变化时,触发此事件。然而,如果生命值从等于或大于 Humanoid.MaxHealth 的值增加,它不会发射。
当 Humanoid.Health 达到零时,Humanoid 将死亡,Humanoid.Died 事件将触发。该事件将以零值发射。
参数
新值 Humanoid.Health 。
代码示例
The following example determines the change in health, printing it to the output. It will only work in a LocalScript.
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local function onCharacterAdded(character)
local humanoid = character:WaitForChild("Humanoid")
local currentHealth = humanoid.Health
local function onHealthChanged(health)
local change = math.abs(currentHealth - health)
print("The humanoid's health", (currentHealth > health and "decreased by" or "increased by"), change)
currentHealth = health
end
humanoid.HealthChanged:Connect(onHealthChanged)
end
player.CharacterAdded:Connect(onCharacterAdded)
This code sample allows you to create a simple color-changing health bar using two nested Frames. Paste this into a LocalScript on the inner frame.
local Players = game:GetService("Players")
local player = Players.LocalPlayer
-- Paste script into a LocalScript that is
-- parented to a Frame within a Frame
local frame = script.Parent
local container = frame.Parent
container.BackgroundColor3 = Color3.new(0, 0, 0) -- black
-- This function is called when the humanoid's health changes
local function onHealthChanged()
local human = player.Character.Humanoid
local percent = human.Health / human.MaxHealth
-- Change the size of the inner bar
frame.Size = UDim2.new(percent, 0, 1, 0)
-- Change the color of the health bar
if percent < 0.1 then
frame.BackgroundColor3 = Color3.new(1, 0, 0) -- black
elseif percent < 0.4 then
frame.BackgroundColor3 = Color3.new(1, 1, 0) -- yellow
else
frame.BackgroundColor3 = Color3.new(0, 1, 0) -- green
end
end
-- This function runs is called the player spawns in
local function onCharacterAdded(character)
local human = character:WaitForChild("Humanoid")
-- Pattern: update once now, then any time the health changes
human.HealthChanged:Connect(onHealthChanged)
onHealthChanged()
end
-- Connect our spawn listener; call it if already spawned
player.CharacterAdded:Connect(onCharacterAdded)
if player.Character then
onCharacterAdded(player.Character)
end
Jumping
当 Humanoid 进入并离开 Jumping``Enum.HumanoidStateType 时,该事件发生。
当一个 Humanoid 跳跃时,此事件在短时间内再次发射一个 active 参数的 true ,之后再次发射一个 active 参数的 false 。这次第二次发射不匹配着降落Humanoid;为此,请收听 Landed Enum.HumanoidStateType 使用 Humanoid.StateChanged 。
您可以使用 Humanoid:SetStateEnabled() 函数禁用跳跃。
参数
MoveToFinished
当 Humanoid 结束走向由 Humanoid.WalkToPoint 和 Humanoid.WalkToPart 属性声明的目标时,此事件发生。
Humanoid.WalkToPoint 和 Humanoid.WalkToPart 属性可以单独设置,或使用 Humanoid:MoveTo() 函数。
如果 Humanoid 在 8 秒内达到目标,这个事件将返回 达到 作为真实的。如果目标在 8 秒内没有达到,那么 Humanoid 将停止行走,而 达到 将为错误。在时间限制期间再次调用 Humanoid:MoveTo() 可重置该时间限制。
参数
代码示例
This code sample includes a function that avoids the 8 second timeout on Humanoid:MoveTo() by calling Humanoid:MoveTo() again before the timeout elapses. It also includes an optional andThen parameter where developers can pass a function to be called when the humanoid reaches its destination.
local function moveTo(humanoid, targetPoint, andThen)
local targetReached = false
-- listen for the humanoid reaching its target
local connection
connection = humanoid.MoveToFinished:Connect(function(reached)
targetReached = true
connection:Disconnect()
connection = nil
if andThen then
andThen(reached)
end
end)
-- start walking
humanoid:MoveTo(targetPoint)
-- execute on a new thread so as to not yield function
task.spawn(function()
while not targetReached do
-- does the humanoid still exist?
if not (humanoid and humanoid.Parent) then
break
end
-- has the target changed?
if humanoid.WalkToPoint ~= targetPoint then
break
end
-- refresh the timeout
humanoid:MoveTo(targetPoint)
task.wait(6)
end
-- disconnect the connection if it is still connected
if connection then
connection:Disconnect()
connection = nil
end
end)
end
local function andThen(reached)
print((reached and "Destination reached!") or "Failed to reach destination!")
end
moveTo(script.Parent:WaitForChild("Humanoid"), Vector3.new(50, 0, 50), andThen)
PlatformStanding
当 Humanoid 进入或离开 PlatformStanding``Enum.HumanoidStateType 时,该事件发生。
当 Humanoid 处于 PlatformStanding 状态时,Humanoid.PlatformStand 属性将为 true。
虽然 Humanoid.PlatformStand 设置为 true ,但 Humanoid 无法移动工具。有关更多信息,请参阅 Humanoid.PlatformStand 页面。
平台支架 Enum.HumanoidStateType 与现在已禁用的 Platform 部分相关。尽管如此,开发人员仍然可以使用它。
参数
Ragdoll
当 Humanoid 进入或离开 Ragdoll``Enum.HumanoidStateType 时,该事件发生。
active 参数将有值 true 或 false 来表示进入或离开。
使用 Humanoid:SetStateEnabled() 来禁用获取状态以保持在布娃娃状态。
还见:
参数
Running
当 Humanoid 运行速度发生变化时,此事件会触发。
在运行 Humanoids 封面时,平均每秒钟的 Humanoid.WalkSpeed 在钉子上。
当 Humanoid 停止运行此事件时,速度为 0。
还见:
- 对于游泳和攀登,请参阅 Humanoid.Swimming 和 Humanoid.Climbing 事件
- 您还可以使用 Humanoid 事件检测当 Humanoid.StateChanged 正在运行时
参数
代码示例
Demonstrates connecting to the Humanoid.Running event. The event is connected to every player's humanoid that joins.
The function connected will print whether or not the humanoid is running based on the speed.
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local function onRunning(speed: number)
if speed > 0 then
print(`{localPlayer.Name} is running`)
else
print(`{localPlayer.Name} has stopped`)
end
end
humanoid.Running:Connect(function(speed: number)
onRunning(speed)
end)
Seated
当 Humanoid 坐在或从 Seat 或 VehicleSeat 中起来时,此事件会触发。
当角色与座椅接触时,它们被附加到座椅上,播放坐姿动画。了解有关此更多信息,请参阅Seat页。
- 如果角色坐下,active参数将为 真实 ,而currentSeatPart将是他们当前坐的座位。
- 如果角色从座位上起来了,那么 active 参数将为 false ,而 currentSeatPart 将为 nil 。
还见:
- Humanoid.Sit , 这表示是否有人形当前坐着
- Humanoid.SeatPart , 这表示 Humanoid 目前坐在的座位,如果有。
参数
代码示例
This code sample demonstrates when the local player's Character sits down or stands up. It should be placed inside a LocalScript within StarterCharacterScripts in order to run when the player's character spawns in.
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local function onSeated(isSeated, seat)
if isSeated then
print("I'm now sitting on: " .. seat.Name .. "!")
else
print("I'm not sitting on anything")
end
end
humanoid.Seated:Connect(onSeated)
StateChanged
当 Humanoid 状态发生变化时,触发此事件。
由于没有闲置空闲”的人形状态,你应该使用 Humanoid.Running 事件或倾听 RootPart 部分的 Velocity 来确定当 Humanoid 静止时工作。
还看到也看到
- Humanoid:GetState() 和 Humanoid:ChangeState() 获取和设置状态。
- Humanoid:SetStateEnabled() 启用和禁用特定状态。
参数
人形的前一个状态输入。
人形的当前状态输入。
代码示例
Emits particles from the local player's Player.Character when they jump. To try this code sample, place it inside a LocalScript parented to StarterCharacterScripts.
local character = script.Parent
local primaryPart = character.PrimaryPart
-- create particles
local particles = Instance.new("ParticleEmitter")
particles.Size = NumberSequence.new(1)
particles.Transparency = NumberSequence.new(0, 1)
particles.Acceleration = Vector3.new(0, -10, 0)
particles.Lifetime = NumberRange.new(1)
particles.Rate = 20
particles.EmissionDirection = Enum.NormalId.Back
particles.Enabled = false
particles.Parent = primaryPart
local humanoid = character:WaitForChild("Humanoid")
local isJumping = false
-- listen to humanoid state
local function onStateChanged(_oldState, newState)
if newState == Enum.HumanoidStateType.Jumping then
if not isJumping then
isJumping = true
particles.Enabled = true
end
elseif newState == Enum.HumanoidStateType.Landed then
if isJumping then
isJumping = false
particles.Enabled = false
end
end
end
humanoid.StateChanged:Connect(onStateChanged)
The following sample will require a one second cooldown after a Humanoid has landed before it is able to jump again.
To try this sample, place it inside a LocalScript in StarterCharacterScripts|StarterPlayer.StarterCharacterScripts.
local character = script.Parent
local JUMP_DEBOUNCE = 1
local humanoid = character:WaitForChild("Humanoid")
local isJumping = false
humanoid.StateChanged:Connect(function(_oldState, newState)
if newState == Enum.HumanoidStateType.Jumping then
if not isJumping then
isJumping = true
humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
end
elseif newState == Enum.HumanoidStateType.Landed then
if isJumping then
isJumping = false
task.wait(JUMP_DEBOUNCE)
humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
end
end
end)
StateEnabledChanged
状态启用更改事件在 Humanoid:SetStateEnabled() 被调用时触发,当 Humanoid 上调用时。
参数包括问题中的 Enum.HumanoidStateType 以及指示是否启用此状态的 bool。
还见:
- 要找到是否启用状态,请使用 Humanoid:GetStateEnabled()
- 要听到 Humanoid 状态变更使用 Humanoid.StateChanged
参数
启用状态已更改的 Enum.HumanoidStateType 对于哪个状态。
如果状态已启用,则为真。
代码示例
When a humanoid state changes for the Players.LocalPlayer, the code below prints whether the state has been enabled or disabled.
This code should work as expected when placed in a LocalScript.
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local function onStateEnabledChanged(state, enabled)
if enabled then
print(state.Name .. " has been enabled")
else
print(state.Name .. " has been disabled")
end
end
humanoid.StateEnabledChanged:Connect(onStateEnabledChanged)
Strafing
此事件不会在 Humanoid 扫射时触发,开发人员不应使用
当 Humanoid 进入或离开 StrafingNoPhysics Enum.HumanoidStateType 时,此事件会发射。
当 Humanoid 进入 StrafingNoPhysics 状态时,此事件将以 有效 参数 true 发射。事件将在 激活 等于 假 时再次发射,当 Humanoid 离开 StrafingNoPhysics
这个事件与 StrafingNoPhysics Humanoid 状态相关,当 不 发射时,当 Humanoid 移动与面向的方向垂直时。该状态目前未使用,如果使用 Humanoid:ChangeState() 设置状态,状态将恢复到 RunningNoPhysics。
参数
Swimming
当 Humanoid 游泳在 Terrain 水中的速度发生变化时,此事件会触发。
Humanoids 在 87.5% 的情况下游泳 Humanoid.WalkSpeed .
当 Humanoid 停止游泳时,这个事件并不总是以 0 的速度发射,
还见:
- 对于跑步和攀登,请参阅 Humanoid.Running 和 Humanoid.Climbing 事件
- 您还可以使用 Humanoid 事件检测当一个 Humanoid.StateChanged 正在游泳时
- 您可以使用 Humanoid:SetStateEnabled() 函数禁用游泳
参数
Touched
当一个机器人肢体与另一个BasePart接触时,此事件发生。被肢体触摸的 BasePart ,以及肢体本身,都被提供。
当肢属于 Humanoid 时,此事件不会发生,当它们与自己接触时。
替代品
虽然 Humanoid.Touched 事件很有用,但你应该考虑是否有更适合你需求的替代方案。
- 在大多数情况下,建议连接一个BasePart.Touched事件,而不是连接一个BaseParts事件,因为当人形移动时,事件Humanoid.Touched会不断发射。例如,在一个躲避球游戏中,将球连接到Touched事件而不使用Humanoid.Touched更实用。
注释
- 连接到此事件会导致每个肢体都创建一个 TouchTransmitter 。
- 目前没有等价于 BasePart.TouchEnded 的 Humanoids 。
参数
代码示例
When placed inside a Player.Character model this code will give a player the 'Midas touch'. Everything their character touches will change to gold.
When the Humanoid dies, this change is undone and the golden BasePart|BaseParts are returned to their original state.
To test this out, place this code inside a Script and place it in StarterCharacterScripts|StarterPlayer.StarterCharacterScripts.
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local partInfo = {}
local debounce = false
local function onHumanoidTouched(hit, _limb)
if debounce then
return
end
if not hit.CanCollide or hit.Transparency ~= 0 then
return
end
if not partInfo[hit] then
partInfo[hit] = {
BrickColor = hit.BrickColor,
Material = hit.Material,
}
hit.BrickColor = BrickColor.new("Gold")
hit.Material = Enum.Material.Ice
debounce = true
task.wait(0.2)
debounce = false
end
end
local touchedConnection = humanoid.Touched:Connect(onHumanoidTouched)
local function onHumanoidDied()
if touchedConnection then
touchedConnection:Disconnect()
end
-- undo all of the gold
for part, info in pairs(partInfo) do
if part and part.Parent then
part.BrickColor = info.BrickColor
part.Material = info.Material
end
end
end
humanoid.Died:Connect(onHumanoidDied)