BasePart

显示已弃用

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

无法创建
不可浏览

BasePart 是一个抽象基类,用于世界中的对象渲染和物理模拟时在 Workspace 。有几种实现 BasePart , 最常见的是 PartMeshPart .其他包括 WedgePart , SpawnLocation , 和单例 Terrain 对象。一般来说,当文档引用“部分”时,大多数 BasePart 实现将工作,而不仅仅是 Part

有关如何将 BaseParts 组合成模拟刚体的信息,请参阅 装配

BasePart (除了 Terrain ) 互动的许多不同对象,包括:

概要

属性

继承自PVInstance属性

方法

继承自PVInstance方法
  • 写入并联

    获取 PVInstance 的枢轴。

  • PivotTo(targetCFrame : CFrame):()

    将 以及所有其子孙 转换为位于指定 的位置,使旋转点现在位于指定的 。

活动

属性

Anchored

读取并联

Anchored 属性决定零件是否通过物理无法移动。启用后,零件将永远不会因重力、其他部件碰撞、覆盖其他部件或任何其他与物理相关的原因而改变位置。因结果,两个锚定的部件永远不会在彼此上发射 Touched 事件。

锚定的部件仍然可以通过修改其 CFramePosition 来移动,仍然可能具有非零 AssemblyLinearVelocityAssemblyAngularVelocity

最后,如果未锚定的部分通过像 Weld 这样的对象与锚定的部分连接,它也会起作用锚定。如果这样的连接断裂,零件可能再次受到物理影响。请参阅装配获取更多详情。

无法在锚定部件上设置网络所有权。如果零件的锚定状态在服务器上发生变化,该零件的网络所有权将受到影响。

代码示例

This code sample will allow a part to be clicked to toggle its anchored property. When toggled, the visual appearance of the part is updated (red means anchored, yellow means free).

Part Anchored Toggle

local part = script.Parent
-- Create a ClickDetector so we can tell when the part is clicked
local cd = Instance.new("ClickDetector", part)
-- This function updates how the part looks based on its Anchored state
local function updateVisuals()
if part.Anchored then
-- When the part is anchored...
part.BrickColor = BrickColor.new("Bright red")
part.Material = Enum.Material.DiamondPlate
else
-- When the part is unanchored...
part.BrickColor = BrickColor.new("Bright yellow")
part.Material = Enum.Material.Wood
end
end
local function onToggle()
-- Toggle the anchored property
part.Anchored = not part.Anchored
-- Update visual state of the brick
updateVisuals()
end
-- Update, then start listening for clicks
updateVisuals()
cd.MouseClick:Connect(onToggle)

AssemblyAngularVelocity

未复制
读取并联

该零件的装配角速度向量。它是每秒钟角度变化率以 ради为单位。

角度速度在装配的每个点都相同。

直接设置速度可能会导致不现实的运动。使用 TorqueAngularVelocity 约束是首选,或使用 ApplyAngularImpulse() 如果你想要立即更改速度。

如果零件是服务器的 拥有者 ,此属性必须从服务器 更改 (而不是从 或 设置为 )。如果零件通过 自动拥有权 由客户拥有,此属性可以通过客户端脚本 或服务器脚本更改;将服务器拥有的部分更改为客户端脚本将无效。

AssemblyCenterOfMass

只读
未复制
读取并联

位置由组装中所有零件的 MassPosition 计算出来。

如果装配有锚定部件,该部件的质量中心将是装配的质量中心,装配将拥有无限质量。

了解质量中心可以帮助组装维持稳定性。对质量中心施加的力不会导致角速度加速,仅线性。低质量中心的装配会在重力效果下保持更好的时间抬头。

AssemblyLinearVelocity

未复制
读取并联

该部分的装配的线速度向量。它是每秒钟的位置变化率 AssemblyCenterOfMass 在螺柱上。

如果您想知道速度在装配的质量中心以外的点,请使用 GetVelocityAtPosition()

直接设置速度可能会导致不现实的运动。使用 VectorForce 约束是首选,或使用 ApplyImpulse() 如果你想立即改变速度。

如果零件是服务器的 拥有者 ,此属性必须从服务器 更改 (而不是从 或 设置为 )。如果零件通过 自动拥有权 由客户拥有,此属性可以通过客户端脚本 或服务器脚本更改;将服务器拥有的部分更改为客户端脚本将无效。

AssemblyMass

只读
未复制
读取并联

本部分装配中所有 BaseParts 的质量之和。不是装配的根部分,且是 Massless 的零件不会贡献到 AssemblyMass

如果装配有锚定部件,装配的质量被认为是无限的。未锚定装配件之间存在大量差异的物质限制和其他物理互动可能会导致不稳定。

AssemblyRootPart

只读
未复制
读取并联

该属性表示自动选择代表装配根部分的 BasePart 。这是开发者调用 GetRootPart() 时返回的相同部分。

根部分可通过更改装配中零件的 RootPriority 来更改。

所有共享相同 AssemblyRootPart 的零件都在同一装配中。

了解根部件的更多信息,请参阅集装箱

AudioCanCollide

读取并联

AudioCanCollide 决定零件是否会物理地与音频模拟进行交互,类似于 CastShadow 对于照明。

禁用时,音频通过零件;它不被遮蔽或反射。

BackSurface

读取并联

BackSurface 属性决定了用于零件正面 Z 方向的表面类型。当两个零件的面靠近时,它们可能会在之间创建一个联合。

BottomSurface

读取并联

BottomSurface 属性决定了用于零部件负向 Y 方向的表面类型。当两个零件的面靠近时,它们可能会在之间创建一个联合。

BrickColor

未复制
读取并联

该属性决定零件的颜色。如果零件具有 Material,这也会决定在渲染材质时使用的颜色。对颜色的更多控制,可以使用 Color 属性,该属性将使用最接近的 BrickColor

零件的其他视觉属性由 TransparencyReflectance 决定。

CFrame

读取并联

CFrame 属性决定了世界中 BasePart 的位置和方向。它作为几何上的任意参考位置,但 ExtentsCFrame 代表其物理中心的实际 CFrame

当在零件上设置 CFrame 时,其他加入的零件也相对于零件移动,但建议您使用 PVInstance:PivotTo() 移动整个模型,例如当传送玩家角色时。

与设置 BasePart.Position 不同,设置 CFrame 将始终将零件移至准确指定的 CFrame;换言之: 没有重叠检查 ,物理解决器将尝试解决任何重叠,除非两个部件都是 Anchored

对于跟踪零件相对位置的 CFrame ,一个 Attachment 可能有用。

代码示例

This code sample demonstrates setting a part's CFrame in many different ways. It showcases how to create and compose CFrame values. It references a sibling part called "OtherPart" for demonstrating relative positioning.

Setting Part CFrame

local part = script.Parent:WaitForChild("Part")
local otherPart = script.Parent:WaitForChild("OtherPart")
-- Reset the part's CFrame to (0, 0, 0) with no rotation.
-- This is sometimes called the "identity" CFrame
part.CFrame = CFrame.new()
-- Set to a specific position (X, Y, Z)
part.CFrame = CFrame.new(0, 25, 10)
-- Same as above, but use a Vector3 instead
local point = Vector3.new(0, 25, 10)
part.CFrame = CFrame.new(point)
-- Set the part's CFrame to be at one point, looking at another
local lookAtPoint = Vector3.new(0, 20, 15)
part.CFrame = CFrame.lookAt(point, lookAtPoint)
-- Rotate the part's CFrame by pi/2 radians on local X axis
part.CFrame = part.CFrame * CFrame.Angles(math.pi / 2, 0, 0)
-- Rotate the part's CFrame by 45 degrees on local Y axis
part.CFrame = part.CFrame * CFrame.Angles(0, math.rad(45), 0)
-- Rotate the part's CFrame by 180 degrees on global Z axis (note the order!)
part.CFrame = CFrame.Angles(0, 0, math.pi) * part.CFrame -- Pi radians is equal to 180 degrees
-- Composing two CFrames is done using * (the multiplication operator)
part.CFrame = CFrame.new(2, 3, 4) * CFrame.new(4, 5, 6) --> equal to CFrame.new(6, 8, 10)
-- Unlike algebraic multiplication, CFrame composition is NOT communitative: a * b is not necessarily b * a!
-- Imagine * as an ORDERED series of actions. For example, the following lines produce different CFrames:
-- 1) Slide the part 5 units on X.
-- 2) Rotate the part 45 degrees around its Y axis.
part.CFrame = CFrame.new(5, 0, 0) * CFrame.Angles(0, math.rad(45), 0)
-- 1) Rotate the part 45 degrees around its Y axis.
-- 2) Slide the part 5 units on X.
part.CFrame = CFrame.Angles(0, math.rad(45), 0) * CFrame.new(5, 0, 0)
-- There is no "CFrame division", but instead simply "doing the inverse operation".
part.CFrame = CFrame.new(4, 5, 6) * CFrame.new(4, 5, 6):Inverse() --> is equal to CFrame.new(0, 0, 0)
part.CFrame = CFrame.Angles(0, 0, math.pi) * CFrame.Angles(0, 0, math.pi):Inverse() --> equal to CFrame.Angles(0, 0, 0)
-- Position a part relative to another (in this case, put our part on top of otherPart)
part.CFrame = otherPart.CFrame * CFrame.new(0, part.Size.Y / 2 + otherPart.Size.Y / 2, 0)

CanCollide

读取并联

CanCollide 决定零件是否会物理地与其他零件互动。禁用时,其他部分可以无中断地通过零件。用于 装饰 的零件通常会被禁用CanCollide,因为它们不需要由物理引擎考虑

如果零件不是 Anchored 且有 CanCollide 被禁用,它可能会从世界上掉出来,最终被 Workspace.FallenPartsDestroyHeight 摧毁。

禁用 CanCollide 时,零件仍可触发 Touched 事件(以及其他触碰它们的零件)。您可以使用 CanTouch 来禁用此功能。

了解有关碰撞的更多信息,请参阅碰撞

代码示例

This code sample shows how a part can fade away when touched by a Humanoid then reappear a moment after to create a passable door.

Fade Door

-- Paste into a Script inside a tall part
local part = script.Parent
local OPEN_TIME = 1
-- Can the door be opened at the moment?
local debounce = false
local function open()
part.CanCollide = false
part.Transparency = 0.7
part.BrickColor = BrickColor.new("Black")
end
local function close()
part.CanCollide = true
part.Transparency = 0
part.BrickColor = BrickColor.new("Bright blue")
end
local function onTouch(otherPart)
-- If the door was already open, do nothing
if debounce then
print("D")
return
end
-- Check if touched by a Humanoid
local human = otherPart.Parent:FindFirstChildOfClass("Humanoid")
if not human then
print("not human")
return
end
-- Perform the door opening sequence
debounce = true
open()
task.wait(OPEN_TIME)
close()
debounce = false
end
part.Touched:Connect(onTouch)
close()

CanQuery

读取并联

该属性决定零件在空间查询操作中是否被考虑,例如 GetPartBoundsInBoxRaycast 。请注意, 必须被禁用才能使 生效,空间查询函数永远不会包含含有 的部分。

除了这个属性之外,在调用空间查询函数时,也可以使用 OverlapParamsRaycastParams 对象排除包含在给定列表中的零件的子零件。

CanTouch

读取并联

该属性决定是否 TouchedTouchEnded 事件在零件上发生。如果 ,其他触碰部件也必须设置为 以触发触碰事件。如果 false , 触摸事件无法为零件设置,尝试这样做将发生错误。同样,如果属性在触摸事件连接后设置为 false ,事件将被断开,并移除 TouchTransmitter

请注意,这种碰撞逻辑可以通过 碰撞组 属性设置遵守 Workspace.TouchesUseCollisionGroups 碰撞组。如果 true , 非碰撞群中的零件将忽略 both collisions 触碰事件, 因此使这个属性无关。

性能

对于具有 CanTouchCanCollide 设置为 false 的零件,由于这些零件永远不需要计算任何类型的零件碰撞,因此性能提升很小。然而,它们仍然可以被 RaycastsOverlapParams 查询击中。

CastShadow

读取并联

决定零件是否投射阴影。禁用此属性对给定零件可能会导致对该零件投射的阴影上的视觉缺陷。

该属性不设计用于性能提升,但在复杂的场景中,战略地禁用某些部分可能会提高性能。由于可能存在视觉艺术品,我们建议在大多数情况下将其启用在所有部分。

CenterOfMass

只读
未复制
读取并联

CenterOfMass 属性描述了零件质量中心的 本地 位置。如果这是单个部件装配,这是从世界空间转换为本地的 AssemblyCenterOfMass。在简单的 Parts 上,质量中心总是 (0, 0, 0) ,但可能会因为 WedgePartMeshPart 而变化。

CollisionGroup

未复制
读取并联

CollisionGroup 属性描述了零件碰撞组的名称(最多 100 个字符)。零件从默认组开始,其名称为 "Default" 。该值不能为空。

虽然这个属性本身不可复制,但引擎内部通过另一个私有属性复制值来解决回退兼容问题。

代码示例

This example demonstrates one basic use of collision groups. It assigns BallPart to "CollisionGroupBall" and DoorPart to "CollisionGroupDoor", then makes the two groups non-collidable using PhysicsService:CollisionGroupSetCollidable().

PhysicsService:RegisterCollisionGroup

local PhysicsService = game:GetService("PhysicsService")
local collisionGroupBall = "CollisionGroupBall"
local collisionGroupDoor = "CollisionGroupDoor"
-- Register collision groups
PhysicsService:RegisterCollisionGroup(collisionGroupBall)
PhysicsService:RegisterCollisionGroup(collisionGroupDoor)
-- Assign parts to collision groups
script.Parent.BallPart.CollisionGroup = collisionGroupBall
script.Parent.DoorPart.CollisionGroup = collisionGroupDoor
-- Set groups as non-collidable with each other and check the result
PhysicsService:CollisionGroupSetCollidable(collisionGroupBall, collisionGroupDoor, false)
print(PhysicsService:CollisionGroupsAreCollidable(collisionGroupBall, collisionGroupDoor)) --> false

Color

未复制
读取并联

Color 属性决定零件的颜色。如果零件具有 Material,这也会决定在渲染材质时使用的颜色。

如果此属性已设置,BrickColor将使用最接近此Color值的匹配。

零件的其他视觉属性由 TransparencyReflectance 决定。

代码示例

This code sample colors a player's entire character based on how much health they have. It generates a color based on their max health, then sets the color properties of objects within their character, removing any extra objects.

Character Health Body Color

-- Paste into a Script within StarterCharacterScripts
-- Then play the game, and fiddle with your character's health
local char = script.Parent
local human = char.Humanoid
local colorHealthy = Color3.new(0.4, 1, 0.2)
local colorUnhealthy = Color3.new(1, 0.4, 0.2)
local function setColor(color)
for _, child in pairs(char:GetChildren()) do
if child:IsA("BasePart") then
child.Color = color
while child:FindFirstChildOfClass("Decal") do
child:FindFirstChildOfClass("Decal"):Destroy()
end
elseif child:IsA("Accessory") then
child.Handle.Color = color
local mesh = child.Handle:FindFirstChildOfClass("SpecialMesh")
if mesh then
mesh.TextureId = ""
end
elseif child:IsA("Shirt") or child:IsA("Pants") then
child:Destroy()
end
end
end
local function update()
local percentage = human.Health / human.MaxHealth
-- Create a color by tweening based on the percentage of your health
-- The color goes from colorHealthy (100%) ----- > colorUnhealthy (0%)
local color = Color3.new(
colorHealthy.R * percentage + colorUnhealthy.r * (1 - percentage),
colorHealthy.G * percentage + colorUnhealthy.g * (1 - percentage),
colorHealthy.B * percentage + colorUnhealthy.b * (1 - percentage)
)
setColor(color)
end
update()
human.HealthChanged:Connect(update)

CurrentPhysicalProperties

只读
未复制
读取并联

CurrentPhysicalProperties 表示零件的当前物理属性。您可以为每个零件的物理属性设置自定义值,自定义材料,以及材料覆盖。Roblox 引擎在确定零件的有效物理属性时,会优先考虑更细粒度的定义。以下列表中的值按从最高到最低的优先级排序:

  • 零件的特定物理属性
  • 零件的自定义物料的特定物理属性
  • 零件材料覆盖的物理特性自定义
  • 零件材料的默认物理属性

CustomPhysicalProperties

读取并联

CustomPhysicalProperties 允许你自定义零件的各种物理特性,例如密度、摩擦和弹性。

启用后,该属性允许您配置这些物理属性。如果禁用,这些物理属性由零件的 Material 决定。

代码示例

This code sample demonstrates how to set the CustomPhysicalProperties property of a part.

Set CustomPhysicalProperties

local part = script.Parent
-- This will make the part light and bouncy!
local DENSITY = 0.3
local FRICTION = 0.1
local ELASTICITY = 1
local FRICTION_WEIGHT = 1
local ELASTICITY_WEIGHT = 1
local physProperties = PhysicalProperties.new(DENSITY, FRICTION, ELASTICITY, FRICTION_WEIGHT, ELASTICITY_WEIGHT)
part.CustomPhysicalProperties = physProperties

EnableFluidForces

读取并联

true 启用时,和当 Workspace.FluidForces 启用时,会导致物理引擎在这个 BasePart 上计算空气动力。

ExtentsCFrame

只读
未复制
读取并联

表示物理扩展 CFrame 的物理中心的 BasePart 的物理扩展。

ExtentsSize

只读
未复制
读取并联

物理尺寸 BasePart 在物理引擎中被视为实际尺寸,例如在 碰撞检测 中。

FrontSurface

读取并联

FrontSurface 属性决定了用于零 Z 方向的零件的表面类型。当两个零件的面靠近时,它们可能会在之间创建一个联合。

LeftSurface

读取并联

LeftSurface 属性决定了用于零部件负向 X 方向的表面类型。当两个零件的面靠近时,它们可能会在之间创建一个联合。

LocalTransparencyModifier

隐藏
未复制
读取并联

LocalTransparencyModifier 属性是一个乘数到 Transparency ,仅对本地客户可见。它不从客户端复制到服务器,对于当零件不应为特定客户端渲染时有用,例如玩家在缩放到第一人称模式时不能看到他们角色的身体部位。

该属性通过以下公式修改本地部分的透明度,结果值位于 01 之间。

1 - (( 1 - Transparency ) × ( 1 - LocalTransparencyModifier ))


<th><code>本地透明修改器</code></th>
<th>服务器端</th>
<th>客户端</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>0.5</code></td>
<td><code>0</code></td>
<td><code>0.5</code></td>
<td><code>0.5</code></td>
</tr>
<tr>
<td><code>0.5</code></td>
<td><code>0.25</code></td>
<td><code>0.5</code></td>
<td><code>0.625</code></td>
</tr>
<tr>
<td><code>0.5</code></td>
<td><code>0.5</code></td>
<td><code>0.5</code></td>
<td><code>0.75</code></td>
</tr>
<tr>
<td><code>0.5</code></td>
<td><code>0.75</code></td>
<td><code>0.5</code></td>
<td><code>0.875</code></td>
</tr>
<tr>
<td><code>0.5</code></td>
<td><code>1</code></td>
<td><code>0.5</code></td>
<td><code>1</code></td>
</tr>
</tbody>

Locked

读取并联

属性决定是否可以通过单击选择工作室中的零件(或包含其中的零件)。此属性最常被启用在环境模型中的零件上,这些零件目前不正在编辑中。

代码示例

This code sample uses the concept of recursion to unlock all parts that are a descendant of a model.

Recursive Unlock

-- Paste into a Script within a Model you want to unlock
local model = script.Parent
-- This function recurses through a model's heirarchy and unlocks
-- every part that it encounters.
local function recursiveUnlock(object)
if object:IsA("BasePart") then
object.Locked = false
end
-- Call the same function on the children of the object
-- The recursive process stops if an object has no children
for _, child in pairs(object:GetChildren()) do
recursiveUnlock(child)
end
end
recursiveUnlock(model)

Mass

只读
未复制
读取并联

Mass 是一个只读属性,描述零件体积和密度的产品。它由 GetMass() 函数返回。

Massless

读取并联

如果此属性启用,零件不会对装配的总质量或惯性贡献,只要它焊接到另一部件上,其上有质量。

如果零件是自己的根部件,根据 AssemblyRootPart,这将被忽略为该部件,仍然会为其装配提供质量和惯性贡献,就像普通部件一样。无质量的零件永远不应成为装配根部件,除非所有其他零件都是无质量的。

这可能对车辆上的可选配件或无质量渲染网格焊接到简单的碰撞网格而不影响车辆的处理有用。

还请参阅集合,一篇文档记录了根部件是什么以及如何使用它们。

读取并联

Material 属性允许您设置零件的纹理和默认物理属性(如果 CustomPhysicalProperties 未设置)。默认Plastic材料的纹理非常淡,而SmoothPlastic材料则没有纹理。一些材质纹理,例如 DiamondPlateGranite 有非常明显的纹理。每种材料的纹理反射不同的阳光,特别是Foil

设置此属性然后启用 CustomPhysicalProperties 将使用材料的默认物理属性。例实例,DiamondPlate 是非常密集的材料,而Wood 是非常轻。零件的密度决定它是否会在地形水中漂浮。

Glass 材质变更渲染行为在中等图形设置下,通过应用一点反射率(类似于 Reflectance)和扭曲视角来改变图像质量。效果特别明显在球形零件上。Glass部分后面的半透明零件不可见。

MaterialVariant

未复制
读取并联

系统搜索指定名称和类型的 实例,并查找 类输入。如果成功找到匹配的 MaterialVariant 实例,它将使用该实例来替换默认材料默认材料可以是内置材料或覆盖 MaterialVariantMaterialService 中指定的材料。

Orientation

隐藏
未复制
读取并联

Orientation 属性描述了零件在 XYZ 轴上的旋转度,使用 Vector3 进行。旋转在 Y > X > Z 顺序中应用。这与正确的 欧几里 角度不同,而是 泰特-布莱恩 角度,它描述了 偏航 , 俯仰角滚动 .

还值得注意的是,这个属性与在不同顺序中应用旋转的 CFrame.Angles() 生成器有何不同( ZYX )。为了获得更好地控制零件的旋转,建议设置 CFrame 而不是。

当设置此属性时,连接到这一部分的任何 WeldsMotor6Ds 都会更新匹配的 C0C1 属性,以允许部分相对于任何其他部分移动。WeldConstraints 在移动工具动期间也会暂时禁用并重新启用。

代码示例

This code sample rotates a part continually on the Y axis.

Part Spinner

local part = script.Parent
local INCREMENT = 360 / 20
-- Rotate the part continually
while true do
for degrees = 0, 360, INCREMENT do
-- Set only the Y axis rotation
part.Rotation = Vector3.new(0, degrees, 0)
-- A better way to do this would be setting CFrame
--part.CFrame = CFrame.new(part.Position) * CFrame.Angles(0, math.rad(degrees), 0)
task.wait()
end
end

PivotOffset

读取并联

该属性指定零件的旋转点与其 CFrame 的偏移值,即 BasePart:GetPivot()BasePart.CFrame 乘以 BasePart.PivotOffset 相等。

这有助于将枢轴设置为 本地 空间的位置,但将零件枢轴设置为 世界 空间的位置可以按照以下方式进行:


local Workspace = game:GetService("Workspace")
local part = Workspace.BluePart
local desiredPivotCFrameInWorldSpace = CFrame.new(0, 10, 0)
part.PivotOffset = part.CFrame:ToObjectSpace(desiredPivotCFrameInWorldSpace)

代码示例

This code sample shows a custom function for resetting the pivot of a model back to the center of that model's bounding box.

Reset Pivot

local function resetPivot(model)
local boundsCFrame = model:GetBoundingBox()
if model.PrimaryPart then
model.PrimaryPart.PivotOffset = model.PrimaryPart.CFrame:ToObjectSpace(boundsCFrame)
else
model.WorldPivot = boundsCFrame
end
end
resetPivot(script.Parent)

This code sample creates a clock at the origin with a minute, second, and hour hand, and makes it tick, displaying the local time.

Clock Hands

local function createHand(length, width, yOffset)
local part = Instance.new("Part")
part.Size = Vector3.new(width, 0.1, length)
part.Material = Enum.Material.Neon
part.PivotOffset = CFrame.new(0, -(yOffset + 0.1), length / 2)
part.Anchored = true
part.Parent = workspace
return part
end
local function positionHand(hand, fraction)
hand:PivotTo(CFrame.fromEulerAnglesXYZ(0, -fraction * 2 * math.pi, 0))
end
-- Create dial
for i = 0, 11 do
local dialPart = Instance.new("Part")
dialPart.Size = Vector3.new(0.2, 0.2, 1)
dialPart.TopSurface = Enum.SurfaceType.Smooth
if i == 0 then
dialPart.Size = Vector3.new(0.2, 0.2, 2)
dialPart.Color = Color3.new(1, 0, 0)
end
dialPart.PivotOffset = CFrame.new(0, -0.1, 10.5)
dialPart.Anchored = true
dialPart:PivotTo(CFrame.fromEulerAnglesXYZ(0, (i / 12) * 2 * math.pi, 0))
dialPart.Parent = workspace
end
-- Create hands
local hourHand = createHand(7, 1, 0)
local minuteHand = createHand(10, 0.6, 0.1)
local secondHand = createHand(11, 0.2, 0.2)
-- Run clock
while true do
local components = os.date("*t")
positionHand(hourHand, (components.hour + components.min / 60) / 12)
positionHand(minuteHand, (components.min + components.sec / 60) / 60)
positionHand(secondHand, components.sec / 60)
task.wait()
end

Position

隐藏
未复制
读取并联

Position 属性描述使用 Vector3 描述零件的坐标。它反映了零件的CFrame位置,但也可以设置。

当设置此属性时,连接到这一部分的任何 WeldsMotor6Ds 都会更新匹配的 C0C1 属性,以允许部分相对于任何其他部分移动。WeldConstraints 在移动工具动期间也会暂时禁用并重新启用。

ReceiveAge

隐藏
只读
未复制
读取并联

表示零件的物理最后更新在本地客户端或服务器上的时间(秒)。当零件没有物理时(Anchoredtrue),此值将为0

Reflectance

读取并联

Reflectance 属性决定零件反映天空的程度。一个值 0 表示零件不反射,一个值 1 表示零件完全反射。

反射率不受 Transparency 影响,除非零件完全透明,否则反射率将完全部失效。反射率可能会或可能不会被忽略,取决于零件的 Material

ResizeIncrement

只读
未复制
读取并联

ResizeIncrement 属性是一个只读属性,描述了 Resize() 方法允许的最小变更的尺寸。它与实现抽象类 BasePart 的不同之处在于,例实例, Part 设置为 1 ,而 TrussPart 设置为 2 ,因为个人桁架部分的尺寸为 2×2×2。

ResizeableFaces

只读
未复制
读取并联

ResizeableFaces 属性使用一个 Faces 对象来描述可以调整零件大小的不同面。对于大多数 BasePart 的实现,例如 PartWedgePart,此属性包括所有面。然而, 将将其 设置设置为只有两个面,因为这些类型的零件必须有两个 长度维度。

此属性最常用于用于建造和操纵零件的工具,在那之外的情况下使用较少。具有 Handles 属性的 Handles.Faces 类可以与此属性结合,仅显示可在零件上调整尺寸的面上的手柄。

代码示例

This code sample creates a Handles object and shows how to set the Faces property of the object. It also references ResizeableFaces of a part. Try placing this script in multiple kinds of parts to see how ResizeableFaces varies.

Resize Handles

-- Put this Script in several kinds of BasePart, like
-- Part, TrussPart, WedgePart, CornerWedgePart, etc.
local part = script.Parent
-- Create a handles object for this part
local handles = Instance.new("Handles")
handles.Adornee = part
handles.Parent = part
-- Manually specify the faces applicable for this handle
handles.Faces = Faces.new(Enum.NormalId.Top, Enum.NormalId.Front, Enum.NormalId.Left)
-- Alternatively, use the faces on which the part can be resized.
-- If part is a TrussPart with only two Size dimensions
-- of length 2, then ResizeableFaces will only have two
-- enabled faces. For other parts, all faces will be enabled.
handles.Faces = part.ResizeableFaces

RightSurface

读取并联

RightSurface 属性决定了用于零件正面 X 方向的表面类型。当两个零件的面靠近时,它们可能会在之间创建一个联合。

RootPriority

读取并联

该属性是在 -127127 之间的整数,优先于所有其他规则对根部分排序。当考虑到不是 Anchored 且共享相同 Massless 值的多个零件时,拥有更高 RootPriority 的零件将优先于拥有更低 RootPriority 值的零件。

您可以使用此属性来控制装配中哪一部分是根部分,并在尺寸变更时保持根部分稳定。

还请参阅集合,一篇文档记录了根部件是什么以及如何使用它们。

Rotation

未复制
读取并联

零件在三个轴上的旋转度。

当设置此属性时,连接到这一部分的任何 WeldsMotor6Ds 都会更新匹配的 C0C1 属性,以允许部分相对于任何其他部分移动。WeldConstraints 在移动工具动期间也会暂时禁用并重新启用。

未复制
读取并联

零件的 Size 属性决定了其 视觉 尺寸,而 ExtentsSize 代表物理引擎使用的实际尺寸,例如在 碰撞检测 中。个人维度(长度、宽度、高度)可以低至 0.001 并高达 2048 。尺寸尺寸低于 > 将被视觉上表示为如果零件的尺寸是 。

零件的 Size 被用于各种额外方式:

代码示例

This code sample constructs a pyramid by stacking parts that get progressively smaller. It also colors the parts so they blend between a start color and end color.

Pyramid Builder

local TOWER_BASE_SIZE = 30
local position = Vector3.new(50, 50, 50)
local hue = math.random()
local color0 = Color3.fromHSV(hue, 1, 1)
local color1 = Color3.fromHSV((hue + 0.35) % 1, 1, 1)
local model = Instance.new("Model")
model.Name = "Tower"
for i = TOWER_BASE_SIZE, 1, -2 do
local part = Instance.new("Part")
part.Size = Vector3.new(i, 2, i)
part.Position = position
part.Anchored = true
part.Parent = model
-- Tween from color0 and color1
local perc = i / TOWER_BASE_SIZE
part.Color = Color3.new(
color0.R * perc + color1.R * (1 - perc),
color0.G * perc + color1.G * (1 - perc),
color0.B * perc + color1.B * (1 - perc)
)
position = position + Vector3.new(0, part.Size.Y, 0)
end
model.Parent = workspace
读取并联

TopSurface 属性决定了用于零件正面 Y 方向的表面类型。当两个零件的面靠近时,它们可能会在之间创建一个联合。

Transparency

读取并联

Transparency 属性控制零件在01的比例可见度,其中0完全可见(不透明),而1完全不可见(未渲染)。

尽管完全透明的部件不会渲染,但部分透明的对象有一定的渲染成本。拥有许多透明部件可能会影响性能。

当透明部件重叠时,渲染顺序可能无法预测,因此您应该避免半透明部件重叠。

还见到 LocalTransparencyModifier 作为乘数到 Transparency ,仅对本地客户可见。

代码示例

This code sample shows how a part can fade away when touched by a Humanoid then reappear a moment after to create a passable door.

Fade Door

-- Paste into a Script inside a tall part
local part = script.Parent
local OPEN_TIME = 1
-- Can the door be opened at the moment?
local debounce = false
local function open()
part.CanCollide = false
part.Transparency = 0.7
part.BrickColor = BrickColor.new("Black")
end
local function close()
part.CanCollide = true
part.Transparency = 0
part.BrickColor = BrickColor.new("Bright blue")
end
local function onTouch(otherPart)
-- If the door was already open, do nothing
if debounce then
print("D")
return
end
-- Check if touched by a Humanoid
local human = otherPart.Parent:FindFirstChildOfClass("Humanoid")
if not human then
print("not human")
return
end
-- Perform the door opening sequence
debounce = true
open()
task.wait(OPEN_TIME)
close()
debounce = false
end
part.Touched:Connect(onTouch)
close()

方法

AngularAccelerationToTorque

参数

angAcceleration: Vector3
默认值:""
angVelocity: Vector3
默认值:"0, 0, 0"

返回

ApplyAngularImpulse

()

对这部分的装配应用即时角力脉冲,导致装配旋转。

由脉冲产生的角速度取决于装配的 mass。因此需要更高的脉冲来移动更大的装配。脉冲对于需要立即施加力的情况,例如爆炸或碰撞,很有用。

如果零件是服务器的 拥有者 ,此函数必须从服务器 调用(不是从 或 设置为 的 )。如果零件通过自动所有权 获得所有权,此函数可以从客户端脚本 或服务器脚本调用;从客户端脚本调用服务器拥有的零件将无效。

参数

impulse: Vector3

可应用于装配的角度脉冲向量。

默认值:""

返回

()

ApplyImpulse

()

这个函数应用即时力脉冲到这部分的装配。

力在装配的 center of mass 应用,因此得到的运动只能是线性的。

由脉冲产生的速度取决于装配的 mass 。因此需要更高的脉冲来移动更大的装配。脉冲对于需要立即施加力的情况,例如爆炸或碰撞,很有用。

如果零件是服务器的 拥有者 ,此函数必须从服务器 调用(不是从 或 设置为 的 )。如果零件通过自动所有权 获得所有权,此函数可以从客户端脚本 或服务器脚本调用;从客户端脚本调用服务器拥有的零件将无效。

参数

impulse: Vector3

应用于装配的线性脉冲向量。

默认值:""

返回

()

ApplyImpulseAtPosition

()

该函数在世界空间中指定位置应用瞬间力脉冲对该部件的装配,以实现快速装配。

如果位置不在装配的 center of mass ,脉冲将导致位置和旋转运动。

由脉冲产生的速度取决于装配的 mass 。因此需要更高的脉冲来移动更大的装配。脉冲对于需要立即施加力的情况,例如爆炸或碰撞,很有用。

如果零件是服务器的 拥有者 ,此函数必须从服务器 调用(不是从 或 设置为 的 )。如果零件通过自动所有权 获得所有权,此函数可以从客户端脚本 或服务器脚本调用;从客户端脚本调用服务器拥有的零件将无效。

参数

impulse: Vector3

应用于装配的脉冲向量。

默认值:""
position: Vector3

在世界空间中的位置,用于应用脉冲。

默认值:""

返回

()

CanCollideWith

写入并联

返回零件是否可以相互碰撞。该函数考虑了两个部件的碰撞组。如果指定的部分不是基础部件,该函数将发生错误。

参数

part: BasePart

指定的部件正在检查是否存在碰撞。

默认值:""

返回

零件是否可以相互碰撞。

CanSetNetworkOwnership

CanSetNetworkOwnership 函数检查您是否可以设置零件的网络所有权。

函数的返回值验证您是否可以调用 BasePart:SetNetworkOwner()BasePart:SetNetworkOwnershipAuto() 而不遇到错误。如果您可以修改/阅读网络所有权或返回 false 并说明您无法修改/阅读的原因,则返回 true;如果您无法修改/阅读网络所有权,则返回 false 并说明原因。


返回

无论您是否可以修改或阅读网络所有权和原因。

代码示例

This example checks whether or not the network ownership of the first BasePart named Part in the Workspace can be set.

Check if a Part's Network Ownership Can Be Set

local part = workspace:FindFirstChild("Part")
if part and part:IsA("BasePart") then
local canSet, errorReason = part:CanSetNetworkOwnership()
if canSet then
print(part:GetFullName() .. "'s Network Ownership can be changed!")
else
warn("Cannot change the Network Ownership of " .. part:GetFullName() .. " because: " .. errorReason)
end
end

GetClosestPointOnSurface

参数

position: Vector3
默认值:""

返回

GetConnectedParts

Instances
写入并联

返回一个包含任何类型刚性连接头的对象与零件之间的表。

如果 recursive 是真的,这个函数将返回装配中所有部件都与基础零件紧密连接。

刚性关节

当连接件将两个部分连接在一起时(Part0 → Part1),连接件是 刚性 的,如果物理学Part1Part0。仅适用于以下共同类型:

参数

recursive: boolean

由任何类型的 joint 连接到对象的零件表。

默认值:false

返回

Instances

GetJoints

Instances
写入并联

返回连接到该部分的所有关节或约束。


返回

Instances

与零件连接的所有关节或约束的阵列。

GetMass

写入并联

获取质量 返回读写只的 Mass 属性的值。

此函数优先于大众属性。它仍然支持兼容性;你应该直接使用大众属性。


返回

零件的质量。

代码示例

This example creates a new part, myPart, in the game's Workspace, with dimensions 4x6x4 studs. The part is also anchored.

Then, myMass is set to equal the mass of the new part. The mass of the part is printed at the end of the print statement:

My part's mass is ...

Finding a Part's Mass

local myPart = Instance.new("Part")
myPart.Size = Vector3.new(4, 6, 4)
myPart.Anchored = true
myPart.Parent = workspace
local myMass = myPart:GetMass()
print("My part's mass is " .. myMass)

GetNetworkOwner

写入并联

返回当前网络所有者为该部分的网络所有者的现有玩家,或 nil 在服务器的情况下。


返回

当前是这部分网络所有者的玩家,或者 nil 在服务器的情况下。

GetNetworkOwnershipAuto

写入并联

如果游戏引擎自动决定此部分的网络所有者,返回真值。


返回

游戏引擎是否自动决定网络所有者为此部分。

GetNoCollisionConstraints

Instances

返回

Instances

GetRootPart

写入并联

返回装配的基础部分。当使用 CFrame 移动零件组时移动零件。移动这个基础部分(这将随之移动所有连接到它的其他部分)很重要。更多信息可用于 组装 文章。

此函数优先于 AssemblyRootPart 属性。它仍然支持向后兼容,但你应该直接使用 AssemblyRootPart


返回

装配的基础部分(一组连接在一起的零件)。

GetTouchingParts

Instances

返回与该部分物理交互的所有零件的表。如果零件本身设置了 CanCollide 为 false,则此函数返回一个空表,除非零件有一个 TouchInterest 对象父辈到它 (即意味着有东西连接到其触发的事件)。相邻但不交叉的零件不被视为触碰。此函数优先于 WorldRoot:GetPartsInPart() 函数,提供更多灵活性并避免上述特殊 TouchInterest 规则。使用 WorldRoot:GetPartsInPart() 取而代之。


返回

Instances

一张包含所有部件的表,这些部件可以交叉并与此部件碰撞。

GetVelocityAtPosition

写入并联

返回零件在给定位置对这个零件的线速度。它可用于识别除根部件以外装配中的零件的线速度。如果装配没有角速度,那么线速度将始终为每个位置相同。

参数

position: Vector3
默认值:""

返回

IsGrounded

写入并联

如果对象连接到一个部件,可以将其固定在那里(例如 Anchored 部件),则返回真值;否则返回错误值。在具有 Anchored 部分的装配中,每个其他部分都被接地。


返回

物体是否连接到一个部件,该部件可以将其固定在那场景。

Resize

像使用 Studio 缩放工具一样,改变对象的大小。

参数

normalId: Enum.NormalId

要调调整大小的侧面。

默认值:""
deltaAmount: number

在指定的一侧上增长/减少多少。

默认值:""

返回

零件是否已调整大小。

SetNetworkOwner

()

将指定的玩家设置为网络所有者,用于此网络和所有连接的零件。当 playerInstance 是 nil 时,服务器将成为玩家而不是玩家的所有者。

参数

playerInstance: Player

玩家被授予零件的网络所有权。

默认值:"nil"

返回

()

SetNetworkOwnershipAuto

()

让游戏引擎动态决定谁将处理零件的物理(客户端之一或服务器)。


返回

()

TorqueToAngularAcceleration

参数

torque: Vector3
默认值:""
angVelocity: Vector3
默认值:"0, 0, 0"

返回

IntersectAsync

暂停

创建一个新的 IntersectOperation 从零件和给定数组列中的其他零件的交叉几何中,从中创建一个新的 。只支持 Parts ,不支持 TerrainMeshParts 。与 Clone() 类似,返回的对象没有设置 Parent

调用部分的以下属性被应用到最终的 IntersectOperation :

在以下图像比较中,IntersectAsync() 被调用到紫色块使用包含蓝色砖块的表。最终的 IntersectOperation 解释成两部分交叉几何形状。

Two block parts overlapping

<figcaption>分离零件</figcaption>
Parts intersected into a new solid model

<figcaption>结果 <code>Class.IntersectOperation</code></figcaption>

注释

  • 原始零件在成功的交叉操作后保持完好无损。在大多数情况下,你应该 Destroy() 所有原始零件,并将返回的 IntersectOperation 返回到与调用的 BasePart 相同的位置。
  • 默认情况下,结果交叉点的面颜色从原始零件的 Color 属性中借用。要将整个交叉点更改为特定颜色,将其 UsePartColor 属性设置为 true
  • 如果交叉操作会导致超过 20,000 个三角形的部件,将简化为 20,000 个三角形。

参数

parts: Instances

参与交叉的对象。

默认值:""
collisionfidelity: Enum.CollisionFidelity
默认值:"Default"
renderFidelity: Enum.RenderFidelity

结果的 值。

默认值:"Automatic"

返回

结果 IntersectOperation 与默认名称 交叉 .

SubtractAsync

暂停

从零件中创建一个新的 UnionOperation ,减去给定数组列中零件所占的几何图形。只支持 Parts ,不支持 TerrainMeshParts 。与 Clone() 类似,返回的对象没有设置 Parent

请注意,由于减法导致的结果联盟不能为空。如果操作会导致完全空的几何图形,它将失败。

在以下图像比较中,SubtractAsync() 被调用到蓝色筒体,使用包含紫色砖块的表。最终得到的 UnionOperation 形状将删除砖块的几何图形,使其与筒体的几何图形一致。

Longer block overlapping a cylinder

<figcaption>分离零件</figcaption>
Block part subtracted from cylinder

<figcaption>结果 <code>Class.UnionOperation</code></figcaption>

参数

parts: Instances

参加减法的对象。

默认值:""
collisionfidelity: Enum.CollisionFidelity
默认值:"Default"
renderFidelity: Enum.RenderFidelity

结果的 值。

默认值:"Automatic"

返回

结果 UnionOperation 使用默认名称 联盟 .

代码示例

This example demonstrates how to subtract part(s) from another BasePart to form a negated UnionOperation.

BasePart:SubtractAsync()

local Workspace = game:GetService("Workspace")
local mainPart = script.Parent.PartA
local otherParts = { script.Parent.PartB, script.Parent.PartC }
-- Perform subtract operation
local success, newSubtract = pcall(function()
return mainPart:SubtractAsync(otherParts)
end)
-- If operation succeeds, position it at the same location and parent it to the workspace
if success and newSubtract then
newSubtract.Position = mainPart.Position
newSubtract.Parent = Workspace
end
-- Destroy original parts which remain intact after operation
mainPart:Destroy()
for _, part in otherParts do
part:Destroy()
end

UnionAsync

暂停

从零件中创建一个新的 UnionOperation ,加上给定数组列中零件所占的几何图形。只支持 Parts ,不支持 TerrainMeshParts 。与 Clone() 类似,返回的对象没有设置 Parent

调用部分的以下属性被应用到最终的 UnionOperation :

在以下图像比较中,UnionAsync() 被调用到蓝色块,使用包含紫色圆柱的表。最终的 UnionOperation 解决为两个部分的组合几何形状。

Block and cylinder parts overlapping

<figcaption>分离零件</figcaption>
Parts joined together into a single solid union

<figcaption>结果 <code>Class.UnionOperation</code></figcaption>

注释

  • 原始零件在成功的联合操作后保持完好。在大多数情况下,你应该 Destroy() 所有原始零件,并将返回的 UnionOperation 返回到与调用的 BasePart 相同的位置。
  • 默认情况下,结果联盟会尊重每个零件的 Color 属性。要将整个联盟更改为特定颜色,将其 UsePartColor 属性设置为 true
  • 如果联合操作会导致超过 20,000 个三角形的部分,将简化为 20,000 个三角形。

参数

parts: Instances

参加与调用部分联合的对象。

默认值:""
collisionfidelity: Enum.CollisionFidelity
默认值:"Default"
renderFidelity: Enum.RenderFidelity

结果的 值。

默认值:"Automatic"

返回

结果 UnionOperation 使用默认名称 联盟 .

代码示例

This example demonstrates how to combine the geometry of one BasePart with the geometry of other part(s) to form a UnionOperation.

BasePart:UnionAsync()

local Workspace = game:GetService("Workspace")
local mainPart = script.Parent.PartA
local otherParts = { script.Parent.PartB, script.Parent.PartC }
-- Perform union operation
local success, newUnion = pcall(function()
return mainPart:UnionAsync(otherParts)
end)
-- If operation succeeds, position it at the same location and parent it to the workspace
if success and newUnion then
newUnion.Position = mainPart.Position
newUnion.Parent = Workspace
end
-- Destroy original parts which remain intact after operation
mainPart:Destroy()
for _, part in otherParts do
part:Destroy()
end

活动

TouchEnded

在相似 BasePart.Touched 条件下,当零件停止触碰另一零件时发生火焰。

该事件与 Workspace.TouchesUseCollisionGroups 联合工作,确定是否接受 碰撞组 的检测。

参数

otherPart: BasePart

代码示例

This code sample creates a BillboardGui on a part that displays the number of parts presently touching it.

Touching Parts Count

local part = script.Parent
local billboardGui = Instance.new("BillboardGui")
billboardGui.Size = UDim2.new(0, 200, 0, 50)
billboardGui.Adornee = part
billboardGui.AlwaysOnTop = true
billboardGui.Parent = part
local tl = Instance.new("TextLabel")
tl.Size = UDim2.new(1, 0, 1, 0)
tl.BackgroundTransparency = 1
tl.Parent = billboardGui
local numTouchingParts = 0
local function onTouch(otherPart)
print("Touch started: " .. otherPart.Name)
numTouchingParts = numTouchingParts + 1
tl.Text = numTouchingParts
end
local function onTouchEnded(otherPart)
print("Touch ended: " .. otherPart.Name)
numTouchingParts = numTouchingParts - 1
tl.Text = numTouchingParts
end
part.Touched:Connect(onTouch)
part.TouchEnded:Connect(onTouchEnded)

Touched

触碰事件会在一部分与另一部分接触时发生。例实例,如果 部分A 撞到 部分B , then PartA.Touched 发射到 部分B , 而 PartB.Touched 发射到 部分A .

此事件只在物理移动的结果下发生,因此如果 CFrame 属性被更改,导致零件与另一零件重叠,它将不会发生。这也意味着至少一个参与部件必须 在碰撞时不能Anchored

该事件与 Workspace.TouchesUseCollisionGroups 联合工作,确定是否接受 碰撞组 的检测。

参数

otherPart: BasePart

与给定部分接触的另一部分。


代码示例

This code sample creates a BillboardGui on a part that displays the number of parts presently touching it.

Touching Parts Count

local part = script.Parent
local billboardGui = Instance.new("BillboardGui")
billboardGui.Size = UDim2.new(0, 200, 0, 50)
billboardGui.Adornee = part
billboardGui.AlwaysOnTop = true
billboardGui.Parent = part
local tl = Instance.new("TextLabel")
tl.Size = UDim2.new(1, 0, 1, 0)
tl.BackgroundTransparency = 1
tl.Parent = billboardGui
local numTouchingParts = 0
local function onTouch(otherPart)
print("Touch started: " .. otherPart.Name)
numTouchingParts = numTouchingParts + 1
tl.Text = numTouchingParts
end
local function onTouchEnded(otherPart)
print("Touch ended: " .. otherPart.Name)
numTouchingParts = numTouchingParts - 1
tl.Text = numTouchingParts
end
part.Touched:Connect(onTouch)
part.TouchEnded:Connect(onTouchEnded)

This code sample demonstrates how to connect the BasePart.Touched event of multiple parts in a Model to one function.

Model Touched

local model = script.Parent
local function onTouched(otherPart)
-- Ignore instances of the model coming in contact with itself
if otherPart:IsDescendantOf(model) then
return
end
print(model.Name .. " collided with " .. otherPart.Name)
end
for _, child in pairs(model:GetChildren()) do
if child:IsA("BasePart") then
child.Touched:Connect(onTouched)
end
end