【发布时间】:2020-02-12 15:21:36
【问题描述】:
我在我的 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