首页 > 解决方案 > 如何在 Roblox 的地形上放置零件?

问题描述

我在我的 ROBLOX 游戏中生成了一个地形(山丘、平原、河流),并且我有一个树模型。树只是一组“砖块”,所以它看起来有点像 Minecraft 树。

现在我想在运行时以编程方式克隆该树,并且需要将该副本(克隆)放置在地图上的其他位置。

这一切都很容易。但是在我给我的克隆新坐标后(我保持 Y 轴的坐标与我的原始树相同),它被放置或在空中,或者它在我的地形表面之下。

如何将其直接放置在地形上?或者我如何获得地图上那个特定地点的 Y 轴坐标?

谢谢!

编辑

虽然@Kylaaa 的回答非常完整,但我在这里提供了我的解决方案,同时我还需要有关该地点地形材料的信息(岩石/草/等)

function FindTerrainHeight(pos)

local VOXEL_SIZE = 4
local voxelPos = workspace.Terrain:WorldToCell(pos)

// my region will start at -100 under the desired position and end in +100 above desired position
local voxelRegion = Region3.new((voxelPos - Vector3.new(0,100,0)) * VOXEL_SIZE, (voxelPos + Vector3.new(1,100,1)) * VOXEL_SIZE)
local materialMap, occupancyMap = workspace.Terrain:ReadVoxels(voxelRegion, VOXEL_SIZE)

local steps = materialMap.Size.Y // this is actually 200 = height of the region

// now going from the very top of the region downwards and checking the terrain material
// (while it's AIR = no terrain found yet)
for i = steps, 1, -1 do

    if (materialMap[1][i][1] ~= Enum.Material.Air) then

        materialMap[1][i][1] ---> this is the material found on surface
        ((i-100) * VOXEL_SIZE) ---> this is the Y coordinate of the surface in world coordinates
    end
end

return false
end

编辑 2

所以事实证明@Kylaaa 的回答解决了所有问题。它还包括地形材料!作为元组中的第 4 项。这意味着我的解决方案太复杂了,没有理由。使用他的解决方案。

标签: roblox

解决方案


尝试从起点向下进行射线投射,直到找到地形。game.Workspace:FindPartOnRayWithWhitelist()非常适合这个!

-- choose some starting point in world space
local xPos = 15
local yPos = 20
local zPos = 30

-- make a ray and point it downward.
-- NOTE - It's unusual, but you must specify a magnitude for the ray
local origin = Vector3.new(xPos, yPos, zPos)
local direction = Vector3.new(0, -100000, 0)
local ray = Ray.new(origin, direction)

-- search for the collision point with the terrain
local whitelist = { game.Workspace.Terrain }
local ignoreWater = true
local _, intersection, surfaceNormal, surfaceMaterial = game.Workspace:FindPartOnRayWithWhitelist(ray, whitelist, ignoreWater)

-- spawn a part at the intersection.
local p = Instance.new("Part")
p.Anchored = true
p.Size = Vector3.new(1, 1, 1)
p.Position = intersection -- <---- this Vector3 is the intersection point with the terrain
p.Parent = game.Workspace

如果您调用 时您的模型以不寻常的方式移动,请SetPrimaryPartCFrame()注意 Roblox 物理学不喜欢相互穿透的对象。因此,模型通常会被向上推,直到它们不再相互渗透。对象CanCollide = false不存在此问题,但如果它们不存在Anchored或焊接到零件设置为,它们也会从世界中消失CanCollide = true


推荐阅读