你如何获得方块侧面的位置,以便你可以将方块完美地放在方块旁边

How do you get the position of the side of a block so you can place a block perfectly next to the block

一个例子,图像两个块,

Part1 and Part2, Part1作为普通方块放置在游戏中,Part2使用脚本放置在游戏中, 然后在它被放置到工作区之前,脚本获取 Part1 的侧面位置然后将 Part2 位置作为 Part1 的侧面然后放置到工作区。

那么第 2 部分将紧挨着第 1 部分。

假设您指的是没有 CSG 操作的简单零件,您可以做一些简单的数学运算来排列它们。

尝试这样的事情:

local function createRandomBlock(name, brickColor)
    -- create a randomly sized block
    local part = Instance.new("Part")
    part.Name = name
    part.Size = Vector3.new(math.random(1,10), math.random(1,10), math.random(1,10))
    part.BrickColor = brickColor
    part.Anchored = true
    part.CanCollide = false
    part.Transparency = 0.5
    part.Parent = game.Workspace
    part.BottomSurface = Enum.SurfaceType.Smooth
    part.TopSurface = Enum.SurfaceType.Smooth

    return part
end

-- place part 1 floating in the air, rotated randomly
local twoPi = 2 * math.pi
local testCFrame = CFrame.new(Vector3.new(0,10,0))
testCFrame = testCFrame * CFrame.Angles(math.random(0,twoPi), math.random(0,twoPi), math.random(0,twoPi))

local testBlock = createRandomBlock("Test", BrickColor.White())
testBlock.CFrame = testCFrame

-- start placing blocks relative to the test block
local pFront = createRandomBlock("Front", BrickColor.Red())
pFront.CFrame = testBlock.CFrame + (testBlock.CFrame.LookVector * (testBlock.Size.Z + pFront.Size.Z) * 0.5)

local pBack = createRandomBlock("Back", BrickColor.Red())
pBack.CFrame = testBlock.CFrame + (-1 * testBlock.CFrame.LookVector * (testBlock.Size.Z + pBack.Size.Z) * 0.5)

local pRight = createRandomBlock("Right", BrickColor.Blue())
pRight.CFrame = testBlock.CFrame + (testBlock.CFrame.RightVector * (testBlock.Size.X + pRight.Size.X) * 0.5)

local pLeft = createRandomBlock("Left", BrickColor.Blue())
pLeft.CFrame = testBlock.CFrame + (-1 * testBlock.CFrame.RightVector * (testBlock.Size.X + pLeft.Size.X) * 0.5)

local pTop = createRandomBlock("Top", BrickColor.Green())
pTop.CFrame = testBlock.CFrame + (testBlock.CFrame.UpVector * (testBlock.Size.Y + pTop.Size.Y) * 0.5)

local pBottom = createRandomBlock("Bottom", BrickColor.Green())
pBottom.CFrame = testBlock.CFrame + (-1 * testBlock.CFrame.UpVector * (testBlock.Size.Y + pBottom.Size.Y) * 0.5)

此示例将块放置在原点并随机定向。然后它添加零件并将它们粘在测试块的两侧。