Seat

显示已弃用

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

一种 BasePart 类型,玩家角色可以在其中“坐下”。当角色接触到启用的座位对象时,它将通过一个 Weld 附加到该部件上,默认角色脚本将播放坐下动画。

座位是如何工作的?

当一个包含 Humanoid 和一个名为 'HumanoidRootPart' 的 BasePart(通常是玩家角色)接触到座位时,将在座位和该部件之间创建一个 WeldC0C1 属性被配置,使得角色在座位上方 2 个单位处焊接。这个焊接被命名为 'SeatWeld',并被作为座位的子项。

当坐下时,Seat.Occupant 属性被设置为“坐在”座位上的 Humanoid。此外, humanoid 的 Humanoid.SeatPart 属性被设置为座位。

角色还可以使用 Seat:Sit() 函数被强制坐在座位上。

角色有两种方式可以离开座位。当玩家跳跃时,他们会被移出座位。然而,这也可以通过手动销毁座位焊接来完成,例如:


seat:FindFirstChild("SeatWeld"):Destroy()

注意,座位有一个冷却时间(当前为 3 秒),这是针对每个角色每个座位的。这意味着一旦角色离开了座位,他们将在 3 秒内无法再次坐在同一座位上。这个冷却行为可能会改变,开发人员不应该依赖它。

座位可以用来做什么?

座位有多种用途,从显而易见的到不太传统的。

  • 创建不需要任何编程的椅子或长凳
  • 允许角色在移动物体(如车辆)中“坐下”而不会被抛来抛去
  • 创建由座位中的角色控制的接口,使用 Seat.Occupant 属性

代码示例

这个代码示例演示了如何使用 Seat.Occupant 属性来跟踪哪个玩家坐在座位上,以及他们何时坐下或起身。

检测座位占用者

local Players = game:GetService("Players")
local seat = Instance.new("Seat")
seat.Anchored = true
seat.Position = Vector3.new(0, 1, 0)
seat.Parent = workspace
local currentPlayer = nil
local function onOccupantChanged()
local humanoid = seat.Occupant
if humanoid then
local character = humanoid.Parent
local player = Players:GetPlayerFromCharacter(character)
if player then
print(player.Name .. " 已经坐下")
currentPlayer = player
return
end
end
if currentPlayer then
print(currentPlayer.Name .. " 已经起身")
currentPlayer = nil
end
end
seat:GetPropertyChangedSignal("Occupant"):Connect(onOccupantChanged)

概要

属性

  • 读取并联

    座位是否可用。如果设置为 true,座位将作为一个普通的部件使用。

  • 只读
    未复制
    读取并联

    坐在座位上的 humanoid。

继承自Part的属性

属性

继承自BasePart的属性

属性

继承自PVInstance的属性

属性

方法

继承自BasePart的方法

方法

继承自PVInstance的方法

方法

活动

继承自BasePart的活动

活动

属性

Disabled

读取并联

座位是否可用。如果设置为 true,座位将作为一个普通的部件使用。

Occupant

只读
未复制
读取并联

坐在座位上的 humanoid

方法

Sit

()

强制具有指定 Humanoid 的角色坐在座位上。

参数

humanoid: Instance

返回

()

活动