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

Posted

技术标签:

【中文标题】您如何获得块侧面的位置,以便您可以将块完美地放置在块旁边【英文标题】:How do you get the position of the side of a block so you can place a block perfectly next to the block 【发布时间】:2019-10-22 11:57:21 【问题描述】:

一个例子,图像两个块,

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

那么 Part2 将完美地紧挨着 Part1。

【问题讨论】:

【参考方案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)

这个例子在原点放置一个方块并随机定位它。然后它添加零件并将它们粘贴到测试块的侧面。

【讨论】:

以上是关于您如何获得块侧面的位置,以便您可以将块完美地放置在块旁边的主要内容,如果未能解决你的问题,请参考以下文章

如何动态编写 React.useState 函数,以便您可以从 cms 获得尽可能多的输入类型

如何在 Amazon EC2 上部署 Java webapp,以便您可以有效地使用自动缩放?

如何在 Amazon EC2 上部署 Java webapp,以便您可以有效地使用自动缩放?

如何获取 UIPickerView 中组件的位置?

mp4只能保存一个书签吗

如何在 WooCommerce 页面上维护 Wordpress 中的侧边栏位置