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

时间:2019-06-07 00:02:51

标签: lua roblox

一个例子,想象两个块,

Part1 Part2 ,Part1作为普通块放置在游戏中,而Part2使用脚本放置在游戏中,然后再放置到游戏中。工作空间,脚本获取Part1的侧面位置,然后将Part2的位置作为Part1的侧面,然后将其放入工作空间。

因此,第2部分将完美地位于第1部分旁边。

1 个答案:

答案 0 :(得分:0)

假设您指的是没有零件的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)

此示例在原点放置一个块并将其随机定向。然后添加零件并将其粘贴到测试块的侧面。