Model

显示已弃用

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

模型是容器对象,意味着它们组合对象在一起。它们最好用于存储 BaseParts 集合,并具有一些扩展其功能的函数。

模型用于代表 几何 集合。如果你的分组没有几何解释,例如集合 Scripts , 那么使用 Folder 取而代之。

其组成部分用缝合件连接在一起的模型(以便它们可以通过物理模拟移动或被摧毁)通常有一个 PrimaryPart 设置,该集合指定模型内哪一部分将作为模型移动时跟关注和捆绑盒子。留在一个地方的静态模型不会受益于拥有主要零件设置。

模型具有广泛的应用,包括 Roblox 玩家角色。它们还有一些独特的行为,值得注意:

  • 当一个 和一个命名为 头部 的模型下的父辈时,一个名称/健康图形用户界面会出现在模型上;请参阅 角色名称/健康显示 获取详细信息。
  • 如果零件在 Y 轴上的位置击中了 Workspace.FallenPartsDestroyHeight 值,如果它是 Model 内的最后一个对象,模型也会被摧毁。
  • 在将 Workspace.StreamingEnabled 设置为真实时,在一个地方使用 ModelStreamingMode 来控制模型和任何子模型的复制和/或移除方式的各种行为。此外,LevelOfDetail影响模型的渲染。

与所有 Instance 类型一样,父 Model 复制到客户端的事实并不保证所有其子都复制。如果这些实例被客户端上的代码访问,例如在 LocalScript 中,这非常重要。使用 ModelStreamingMode 与值 Atomic 等可确保父模型存在于客户端,或在原子性不需要时使用 WaitForChild() 来确保整个模型和所有其子模型都存在。

代码示例

The following sample includes a basic function that takes a table of objects and parents them into a new Model, returning that Model.

Basic Model Instantiation

local function groupObjects(objectTable)
local model = Instance.new("Model")
for _, object in pairs(objectTable) do
object.Parent = model
end
return model
end
local objects = {
Instance.new("Part"),
Instance.new("Part"),
}
groupObjects(objects)

概要

属性

继承自PVInstance属性

方法

继承自PVInstance方法
  • 写入并联

    获取 PVInstance 的枢轴。

  • PivotTo(targetCFrame : CFrame):()

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

属性

插件安全性
读取并联

将模型上的细节级别设置为启用实例 流式传输 的体验。

当设置为 StreamingMesh 时,低分辨率“捣乱者”网格(颜色的粗糙网格包围了模型所有子部件)在流媒体范围之外渲染。

当设置为 DisabledAutomatic 时,低分辨率网格不会显示。

ModelStreamingMode

读取并联

控制实例 播放 时,如何将 传送进来和出去,当启用实例 传播 时。行为取决于选定的枚举。在启用 streaming 时无效。

此属性应仅在启用串流时在 Studio 中通过 属性窗口 更改,或在 Scripts 中,但永远不要在 LocalScripts 中更改(这样会导致无定义行为)。

PrimaryPart

读取并联

指向 Model 的主部分。主要部分是 BasePart ,它作为模型的支点的物理参考。即,当模型内的零件由于物理模拟或其他原因移动时,支点会与主要零件同步移动。

请注意,Models 没有默认设置PrimaryPart。如果你正在创建一个需要物理操作的模型,你应该在工作室或脚本中手动设置此属性。如果主部分是 设置,枢轴将保持在世界空间中的同一位置,即使模型内的零件被移动。

还注意,当设置此属性时,必须是 BasePart 模型的子孙。如果您尝试将 Model.PrimaryPart 设置为 BasePart 那是 不是 模型的子孙,它将被设置为该部分,但在下一步模拟时重置为 nil — 这是支持脚本的遗产行为,它假设它可以暂时将主部件设置为 BasePart 这不是模型的子孙。

模型的一般规则是:

  • whose部件通过物理连接(例如WeldConstraintsMotor6Ds)连接在一起的模型应该有一个主要部件分配。例如,Roblox 角色模型默认设置为 Model.PrimaryPart 将其设置为 HumanoidRootPart
  • 静态(通常为 Anchored )模型,如果脚本明确移动它们,除非它们留在一个地方,否则不需要 Model.PrimaryPart 并且倾向于没有一个设置。

代码示例

This code sample creates and throws a dice model, then outputs whether it landed with the blue side facing up. If Model.PrimaryPart was not set, the pivot / bounding box of the dice would rotate, as the parts inside of it are physically simulated during the roll.

Throwing Dice

-- Create a dice model with two halves and attach them together
local diceModel = Instance.new("Model")
diceModel.Name = "ChanceCube"
local diceTop = Instance.new("Part")
diceTop.Size = Vector3.new(4, 2, 4)
diceTop.Position = Vector3.new(0, 1, 0)
diceTop.Color = Color3.new(0, 0, 1)
diceTop.Parent = diceModel
local diceBottom = diceTop:Clone()
diceBottom.Position = Vector3.new(0, -1, 0)
diceBottom.Color = Color3.new(1, 0, 0)
diceBottom.Parent = diceModel
local weld = Instance.new("WeldConstraint")
weld.Part0 = diceTop
weld.Part1 = diceBottom
weld.Parent = diceModel
-- Put the dice up in the air above the workspace origin (does not require a primary part)
diceModel.Parent = workspace
diceModel:PivotTo(CFrame.new(0, 10, 0))
-- Assign the primary part before physical simulation
-- Without this line, the script will always output the same thing and the bounding box of the model will not change orientation
diceModel.PrimaryPart = diceTop
-- Wait a bit before rolling the dice (let it settle onto the floor)
for i = 5, 1, -1 do
print("Rolling dice in...", i)
task.wait(1)
end
diceTop:ApplyAngularImpulse(Vector3.new(15000, 1000, 5000))
diceTop:ApplyImpulse(Vector3.new(0, 3000, 0))
task.wait(1)
-- Wait for the roll to complete
while diceTop.AssemblyLinearVelocity.Magnitude > 0.1 or diceTop.AssemblyAngularVelocity.Magnitude > 0.1 do
task.wait()
end
-- Get the dice orientation, impacted by the primary part
local orientation = diceModel:GetBoundingBox()
if orientation.YVector.Y > 0.5 then
print("It's the boy!")
else
print("It's his mother!")
end

Scale

未复制
不可写入脚本
读取并联

在属性窗口设置此属性将使模型像被调用一样缩放,缩放模型中的所有子模型实例,例如材料、图像和零件的 3D 几何图形,以便模型具有指定的缩放因子与原始尺寸相关。

此属性仅在工作室中可用,如果在 ScriptLocalScript 中使用,将抛出错误。Model/ScaleToModel/GetScale应从脚本中使用。

WorldPivot

未复制
读取并联

该属性决定了哪个位置决定了 Model 哪个做不到 **** 拥有设置的 Model.PrimaryPart 的旋转点。如果 Model 一个 PrimaryPart ,那么 Model 的枢轴与该主部件的枢轴相等,而这个 WorldPivot 属性被忽略。

对于刚刚创建的 Model ,其旋转点将被视为其内容边界盒的中心,直到 第一次 设置其 Model.WorldPivot 属性为止。一旦世界旋转被设置为第一次,就无法恢复这种初始行为。

最常见的情况是,使用 Studio 工具或具有类似功能的模型移动功能(例如 PVInstance:PivotTo()Model:MoveTo())移动模型,将设置世界旋转点,因此结束这种新模型行为。

这种行为的目的是允许 Luau 代码通过创建新模型并将其作为父来实现合理的旋转,避免每次创建模型在代验证码中必须明确设置 Model.WorldPivot 每次创建模型。


local Workspace = game:GetService("Workspace")
local model = Instance.new("Model")
Workspace.BluePart.Parent = model
Workspace.RedPart.Parent = model
model.Parent = Workspace
print(model:GetPivot()) -- Currently equal to the center of the bounding box containing "BluePart" and "RedPart"
model:PivotTo(CFrame.new(0, 10, 0)) -- This works without needing to explicitly set "model.WorldPivot"

代码示例

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)

方法

AddPersistentPlayer

()

参数

playerInstance: Player
默认值:"nil"

返回

()

GetBoundingBox

该函数返回一个包含所有 BasePart 子的卷的描述,该卷位于 Model 内。音量的方向基于 PrimaryPart 的方向,在模型被选择时与 Studio 渲染的选择框匹配。镜像 Terrain:FillBlock() 的行为,返回一个表示该边界盒中心的 CFrame 和一个表示其大小的 Vector3

如果模型没有 PrimaryPart,边界盒将对齐世界轴。


local Workspace = game:GetService("Workspace")
local model = Workspace.Model
local part = Workspace.Part
local orientation, size = model:GetBoundingBox()
-- 调整零件大小并将其放置在模型边界盒内
part.Size = size
part.CFrame = orientation

返回

一个 CFrame 代表卷的方向,随后由一个 Vector3 代表卷的大小。

GetExtentsSize

返回包含所有 BasePartsModel 中的最小边界盒的大小。如果 Model.PrimaryPart 存在,那么边界盒将对齐到那一部分。如果主要部分尚未设置,函数将选择模型中的一部分来对边界盒进行校准。由于这部分的选择不是决定性的,建议设置一个 Model.PrimaryPart 以获得与此函数一致的结果。

注意,此函数仅返回最小边界方块的大小,开发者必须使用自己的方法来获得边界方块的位置。


返回

Vector3 的扩展大小为 Model

代码示例

The code sample below demonstrates how Model.GetExtentsSize can be used to get the size of the bounding box containing the parts.

Model GetExtentsSize

local model = Instance.new("Model")
model.Parent = workspace
local RNG = Random.new()
for _ = 1, 5 do
local part = Instance.new("Part")
part.Anchored = true
part.Size = Vector3.new(RNG:NextNumber(0.05, 5), RNG:NextNumber(0.05, 5), RNG:NextNumber(0.05, 5))
part.Parent = model
end
print(model:GetExtentsSize())

GetPersistentPlayers

Instances

当此方法从 Script 调用时,它返回该模型持有的所有 Player 对象。当从 LocalScript 调用时,此方法仅检查此模型是否为永久的 LocalPlayer


返回

Instances

一个包含该模型对象所持有的所有 Player 对象的表。

GetScale

模型包含一个持续的常规缩放因子,其起始值为新创建的模型的 1,随着模型被缩放调用 Model/ScaleTo 而改变。该函数返回模型的当前惯用缩放因子。

当前缩放因子不会直接影响模型下的实例的大小*。*它用于内容编写和脚本目的,用于记住模型是如何相对于原始尺寸被缩放的。

在给定的会话内,模型将在第一次 Model/ScaleTo 调用后储存子 Instances 的精确原始大小信息。这意味着通过调用 ScaleTo(x) 然后 ScaleTo(1) 将获得你回到 正确 模型的原始配置,没有浮点漂移。避免漂浮点漂移是使用 Scale 到 函数而不是使用 Scale 函数的动机。

缩放因子对引擎行为产生影响的方式之一是:模型的缩放因子将应用于该模型下的共同偏移 播放的动画,因此即使在缩放时,动画也会正确播放,即使在缩放时。


返回

模型当前的惯例缩放因子。

代码示例

This code sample demonstrates substituting in a replacement for all the copies of a tree model using PivotTo and ScaleTo. The pivot and scale of the models are used as a reference ensuring that the relative sizes and locations of the replacement models match those of the originals.

Substituting in a replacement model using PivotTo and ScaleTo

local CollectionService = game:GetService("CollectionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Find all the models with the tag we want to replace
local items = CollectionService:GetTagged("Tree")
local newModel = ReplicatedStorage.FancyTreeReplacementModel
for _, item in items do
-- Make the new item and scale / position it where the old one was
local newItem = newModel:Clone()
newItem:ScaleTo(item:GetScale())
newItem:PivotTo(item:GetPivot())
-- Add the same tag to the replacement
CollectionService:AddTag(newItem, "Tree")
-- Delete the old item and parent the new one
newItem.Parent = item.Parent
item:Destroy()
end

MoveTo

()

PrimaryPart 移至给定位置。如果未指定主要部分,模型的根部分将被使用,但根部分不是确定的,建议您在使用 MoveTo() 时总是设置主要部分。

如果模型需要移动的地方有障碍物,例如 Terrain 或其他 BaseParts,模型将向上移动,直到没有障碍物为止。如果不想要这种行为,那么应该使用 PVInstance:PivotTo() 来代替。

请注意,当移动模型时使用 MoveTo() 时,旋转不会保留。建议使用 TranslateBy()PVInstance:PivotTo() 如果模型当前旋转需要保留。

参数

position: Vector3

移至 Vector3Model

默认值:""

返回

()

代码示例

This sample demonstrates how Model:MoveTo avoids collisions.

A simple two part Model is created, and its PrimaryPart is set. An large obstruction part is placed next to it.

After 5 seconds Model:MoveTo is used to direct the model to move inside the obstruction part. However, as MoveTo will not move a model inside of an obstruction the Model is moved up on the Y axis and placed above the obstruction.

Model MoveTo

local START_POSITION = Vector3.new(-20, 10, 0)
local END_POSITION = Vector3.new(0, 10, 0)
local model = Instance.new("Model")
model.Parent = workspace
local part1 = Instance.new("Part")
part1.Size = Vector3.new(4, 4, 4)
part1.Position = START_POSITION
part1.Anchored = true
part1.BrickColor = BrickColor.new("Bright yellow")
part1.Parent = model
local part2 = Instance.new("Part")
part2.Size = Vector3.new(2, 2, 2)
part2.Position = START_POSITION + Vector3.new(0, 3, 0)
part2.Anchored = true
part2.BrickColor = BrickColor.new("Bright blue")
part2.Parent = model
model.PrimaryPart = part1
model.Parent = workspace
local obstruction = Instance.new("Part")
obstruction.Name = "Obstruction"
obstruction.Size = Vector3.new(10, 10, 10)
obstruction.Position = Vector3.new(0, 10, 0)
obstruction.Anchored = true
obstruction.BrickColor = BrickColor.new("Bright green")
obstruction.Parent = workspace
task.wait(3)
model:MoveTo(END_POSITION)

RemovePersistentPlayer

()

参数

playerInstance: Player
默认值:"nil"

返回

()

ScaleTo

()

模型包含永久的常规缩放因子,新创建的模型起始于 1。这个函数将模型扩展到斧头位置,相对于它将看到 1 倍缩放因子的方式。为了实现这一目标,它做了两件事:

  • 将模型的当前缩放因子设置为指定值
  • 根据需要调整所有子实例的大小和位置

地点的缩放在枢轴位置周围完成。

子实例的所有“几何”属性都将被缩放。这显然包括零件的尺寸,但以下是一些其他缩放的属性示例:

  • WeldConstraintsClass.Rope|Ropes 这样的连接长度
  • 物理速度和力,如 Hinge.MaxServoTorque
  • 像粒子发射器的尺寸等视觉属性
  • 其他长度属性,如 Sound.RollOffMinDistance

参数

newScaleFactor: number
默认值:""

返回

()

TranslateBy

()

通过给定的 Model 偏移值移动一个 Vector3 ,保留模型的方向。如果新位置上已存在另一个 BasePartTerrain ,那么 Model 将覆盖该对象。

翻译在对象空间而不是世界空间中应用,这意味着即使模型的零件有不同的方向,它仍然会沿标准轴移动。

参数

delta: Vector3

Vector3 来翻译 Model

默认值:""

返回

()

代码示例

This sample demonstrates how Model:TranslateBy ignores collisions and respects the orientation of the model.

A simple two part Model is created, rotated 45 degrees on the Y axis, and its PrimaryPart is set. An large obstruction part is placed next to it.

After 5 seconds Model:TranslateBy is used to direct the model to move inside the obstruction part. The model will move inside of the obstruction and maintain it's current orientation.

Model TranslateBy

local START_POSITION = Vector3.new(-20, 10, 0)
local END_POSITION = Vector3.new(0, 10, 0)
local model = Instance.new("Model")
local part1 = Instance.new("Part")
part1.Size = Vector3.new(4, 4, 4)
part1.CFrame = CFrame.new(START_POSITION) * CFrame.Angles(0, math.rad(45), 0)
part1.Anchored = true
part1.BrickColor = BrickColor.new("Bright yellow")
part1.Parent = model
local part2 = Instance.new("Part")
part2.Size = Vector3.new(2, 2, 2)
part2.CFrame = part1.CFrame * CFrame.new(0, 3, 0)
part2.Anchored = true
part2.BrickColor = BrickColor.new("Bright blue")
part2.Parent = model
model.PrimaryPart = part1
model.Parent = workspace
local obstruction = Instance.new("Part")
obstruction.Name = "Obstruction"
obstruction.Size = Vector3.new(10, 10, 10)
obstruction.Position = Vector3.new(0, 10, 0)
obstruction.Transparency = 0.5
obstruction.Anchored = true
obstruction.BrickColor = BrickColor.new("Bright green")
obstruction.Parent = workspace
task.wait(3)
-- use TranslateBy to shift the model into the obstruction
model:TranslateBy(END_POSITION - START_POSITION)

活动