Really relevant. Read this reply to a similar problem:
Use a Region3 to check which parts may be in an area and then use another algorithm to double-check.
If you only need to check if a point is occupied by something (which looks like what you’re trying to do!), I wrote a function for that here:
EDIT: I edited the function a bit so that it returns a list of parts that are on the point. This should be useful for your usecase (checking if the camera is submerged in lava).
local function getPartsInPoint(p)
-- Fudge the region by a bit to avoid the edgecase you saw in OP.
local region = Region3.new(p-Vector3.new(1, 1, 1), p+Vector3.new(1, 1, 1))
local possible = workspace:FindPartsInRegion3(region, 100)
-- Broadphase: The region is bigger than the point and the region is empty.
-- Therefore, there must be nothing in this point.
if #possible == 0 then return {} end
-- Test each part in the region via raycast.
-- FindPartsInRegion3 has a limit of 100 parts, though.
-- If that point in the map is very, *very* dense with parts, then some parts won't be
-- included in the Region3. You can fix this by doing more Region3
-- calls using FindPartsInRegion3WithIgnoreList (with the ignore list
-- being the parts we're found so far) until no more parts can be found.
local inPoint = {}
for _, part in next, possible do
-- Raycast to the center of the part.
local ray = Ray.new(p, part.Position-p)
-- If we don't hit the part, then the point must be inside of the part.
if next(workspace:FindPartOnRayWithWhitelist(ray, {part})) == nil then
table.insert(inPoint, part)
end
end
return inPoint
end
PS: I’m happy that you managed to apply most of my answer from a while ago to your problem
I have edited a script that i found somewhere a littlebit and it works without using rays / region3’s.
function isInRegion3(region, point)
local relative = (region.CFrame:PointToObjectSpace(point) - region.CFrame:PointToObjectSpace(region.Position)) / region.Size
return -0.5 <= relative.X and relative.X <= 0.5
and -0.5 <= relative.Y and relative.Y <= 0.5
and -0.5 <= relative.Z and relative.Z <= 0.5
end
Fair! The original script was designed to accommodate all part types (wedges, spheres, cylinders) and convex meshes. If you’re only worried about boxy parts, then that’s a fine optimization to use.
You’ll still need to use Region3s to get the parts close to a point, though. Unless you’re fine with iterating through all the lava parts every frame (totally OK if there’s only a few).
Reading your function again, I don’t see how you’d actually work this into what you’re doing. You’re checking if a point is inside a Region3, but why? I thought you want to check if a point is inside a lava part?