Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to ignore "Air" material when using :FindPartOnRay()?

Asked by 5 years ago
Edited 5 years ago

Hi I am trying to use FindPartOnRay() to detect terrain type of "Mud" but I keep getting "Air."

mouse.Move:Connect(function()
    local ray,point,surfaceNormal,material = game.Workspace:FindPartOnRay(Ray.new(game.Workspace.Camera.CFrame.Position,mouse.Hit.Position)
    print(material)
end)
--Always prints air, even when pointed at a plastic brick or Mud terrain

1 answer

Log in to vote
0
Answered by
SteamG00B 1633 Moderation Voter
5 years ago
Edited 5 years ago
mouse.Move:Connect(function()
    local ray,point,surfaceNormal,material = game.Workspace:FindPartOnRayWithIgnoreList(mouse.UnitRay,{Enum.Material.Air})
    print(material)
end)

The "FindPartOnRayWithIgnoreList" is what you were looking for.

Also just found the possible reason why Enum.Material.Air does not work for an ignore list, it is set to not browsable https://wiki.roblox.com/index.php?title=API:Class_reference/ReflectionMetadata

I came up with another idea, but it didn't work, I'll put the code here anyway (it does work for other applications)

I made a region3 from the camera's position to the mouse position and used Terrain:ReadVoxels() to get the terrain along the path of the ray, but the problem in this case is that Enum.Material.Air is not able to be detected by ReadVoxels

mouse.TargetFilter = workspace.Part

function ignoreList(first,last)
    local region = Region3.new(last, first)
    print(region.CFrame," SIZE ",region.Size)
    workspace.Part.CFrame=region.CFrame--this places a part in the middle of the region so you can visualize where the region is
    region = region:ExpandToGrid(4)
    local material,occupancy = game.Workspace.Terrain:ReadVoxels(region, 4)
    local size = material.Size
    local allMats = {}
    for x = 1, size.X do
        for y = 1, size.Y do
            for z = 1, size.Z do
                allMats[#allMats+1]=material[x][y][z]

            end
        end
    end
    return allMats
end
0
That yields an "Unable to cast value to Object" error Phibonacci 37 — 5y
0
I know, I'm currently trying to figure out why it's doing that, but I'm assuming it has something to do with using the mouse.UnitRay SteamG00B 1633 — 5y
0
the main problem is that "Air" is not an object. It has no parent so we don't know how to reference it Phibonacci 37 — 5y
0
oh I see SteamG00B 1633 — 5y
0
But this is still a creative way to get the material of the terrain, even if we can't get past the air SteamG00B 1633 — 5y
Ad

Answer this question