概要
属性
方法
AddPersistentPlayer(playerInstance: Player):() |
BreakJoints():() |
breakJoints():() |
MakeJoints():() |
makeJoints():() |
RemovePersistentPlayer(playerInstance: Player):() |
SetPrimaryPartCFrame(cframe: CFrame):() |
TranslateBy(delta: Vector3):() |
代码示例
基本模型实例化
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)API 参考
属性
PrimaryPart
代码示例
掷骰子
-- 创建一个包含两个半部分的骰子模型并将它们连接在一起
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
-- 将骰子放在工作区原点上方的空中(不需要主部件)
diceModel.Parent = workspace
diceModel:PivotTo(CFrame.new(0, 10, 0))
-- 在物理模拟之前分配主部件
-- 如果没有这一行,脚本将始终输出相同的内容,模型的边界框将不会改变方向
diceModel.PrimaryPart = diceTop
-- 在掷骰子之前稍等一下(让它稳定在地面上)
for i = 5, 1, -1 do
print("掷骰子中...", i)
task.wait(1)
end
diceTop:ApplyAngularImpulse(Vector3.new(15000, 1000, 5000))
diceTop:ApplyImpulse(Vector3.new(0, 3000, 0))
task.wait(1)
-- 等待掷骰子完成
while diceTop.AssemblyLinearVelocity.Magnitude > 0.1 or diceTop.AssemblyAngularVelocity.Magnitude > 0.1 do
task.wait()
end
-- 获取骰子的方向,受主部件影响
local orientation = diceModel:GetBoundingBox()
if orientation.YVector.Y > 0.5 then
print("是男孩!")
else
print("是他的母亲!")
endWorldPivot
代码示例
重置支点
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)世界枢轴
local Workspace = game:GetService("Workspace")
local bluePart = Instance.new("Part")
bluePart.Name = "BluePart"
bluePart.Parent = Workspace
local redPart = Instance.new("Part")
redPart.Name = "RedPart"
redPart.Parent = Workspace
local model = Instance.new("Model")
Workspace.BluePart.Parent = model
Workspace.RedPart.Parent = model
model.Parent = Workspace
print(model:GetPivot()) -- 当前等于包含 "BluePart" 和 "RedPart" 的边界框的中心
model:PivotTo(CFrame.new(0, 10, 0)) -- 这在不需要显式设置 "model.WorldPivot" 的情况下工作方法
AddPersistentPlayer
BreakJoints
breakJoints
GetBoundingBox
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())GetModelCFrame
GetModelSize
GetPrimaryPartCFrame
GetScale
返回
代码示例
使用 PivotTo 和 ScaleTo 替换模型
local CollectionService = game:GetService("CollectionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- 查找我们想要替换的所有带标签的模型
local items = CollectionService:GetTagged("Tree")
local newModel = ReplicatedStorage.FancyTreeReplacementModel
for _, item in items do
-- 创建新项目并将其缩放/定位到旧项目的位置
local newItem = newModel:Clone()
newItem:ScaleTo(item:GetScale())
newItem:PivotTo(item:GetPivot())
-- 将相同的标签添加到替换项
CollectionService:AddTag(newItem, "Tree")
-- 删除旧项目并将新项目的父级设置为旧项目的父级
newItem.Parent = item.Parent
item:Destroy()
endMakeJoints
makeJoints
move
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)moveTo
RemovePersistentPlayer
ResetOrientationToIdentity
SetIdentityOrientation
SetPrimaryPartCFrame
TranslateBy
参数
返回
()
代码示例
模型 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("明亮的黄色")
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("明亮的蓝色")
part2.Parent = model
model.PrimaryPart = part1
model.Parent = workspace
local obstruction = Instance.new("Part")
obstruction.Name = "障碍"
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("明亮的绿色")
obstruction.Parent = workspace
task.wait(3)
-- 使用 TranslateBy 将模型移入障碍
model:TranslateBy(END_POSITION - START_POSITION)