First thing I tried using was Region3 but that gave me some nonsensical results and detected collisions even if a part was a distance away:
I then tried using GetTouchingParts, however that only detects intersections; it doesn’t detect if a part is fully within another part.
I thought about shooting rays in each direction, however I remember reading that the rays don’t collide with inward faces.
Now I have no idea what to do. Region3 seems like the thing to use, maybe there’s something wrong with my code?
If you can't see the video, here's the code I execute in the command bar for the Region3
while workspace.Part do
wait()
local t = {}
local region = Region3.new(workspace.Part.Position - workspace.Part.Size/2, workspace.Part.Position + workspace.Part.Size/2)
for i,v in pairs(workspace:FindPartsInRegion3(region, workspace.Part)) do
if v.CanCollide then
t[#t+1]={v,v.Color,v.Transparency}
v.Color = Color3.fromRGB(255, 0, 0)
v.Transparency = 0
print(v:GetFullName())
end
end
wait()
for i,v in pairs(t) do
v[1].Color = v[2]
v[1].Transparency = v[3]
end
print("\n")
end
If all of your parts are rectangular prisms or cubes, then I’d suggest finding a basic collision algorithm on the internet. Your problem is quite literally the same as collision detection.
Sadly, they’re not necessarily all regular parts. Some are MeshParts.
Either way, Region3 already uses AABB collision detection, and that gave the strange results shown in the video.
For some context, in my game players have abilities which get placed in front of the character. This means if they face a wall, it would get placed inside the wall (unless it’s not tall, in that case it gets placed on top).
I haven’t found a way to solve the presented question, however I found a solution to my issue in a different way.
For those interested, I shoot two rays. One from the character to the ability and another from the ability to the character. If the ray from the character hits something but the ray from the ability doesn’t hit anything, then the ability is inside of an object, since rays don’t collide with inward faces. In all other cases, the ability is not inside of an object (fully, it could still be at most half submerged in an object).