Detecting parts inside/behind a UI

local topRight = topLeft + Vector2.new(gui.AbsoluteSize.X, 0)
local bottomLeft = topLeft + Vector2.new(0, gui.AbsoluteSize.Y)
local bottomRight = topLeft + Vector2.new(gui.AbsoluteSize.X, gui.AbsoluteSize.Y)

local length = (topLeft - topRight).Magnitude
local width = (topLeft - bottomLeft).Magnitude

for x = topLeft.X,topLeft.X +length,1 do
    for y = topLeft.Y,topLeft.Y + width,1 do
        local vpRay = workspace.CurrentCamera:ViewportPointToRay(x,y)
            
        local castedRay = workspace:Raycast(vpRay.Origin,vpRay.Direction*10000,rp)
            
        if castedRay and castedRay.Instance then
            visualizeRay(castedRay,vpRay.Origin,castedRay.Position)
            local foundInTable = table.find(selectionTable,castedRay.Instance)
                
            if not foundInTable then
                table.insert(selectionTable,castedRay.Instance)
            end
        end
    end
        
    task.wait()
end

My code, I’m trying to raycast the length and width of the box, and see what parts are there. This is very inaccurate, i’m guessing I’ve done something wrong with the raycasting.

Need more info on what you’re trying to check.

Find parts behind/inside a UI.

Let’s say I have a box in the middle of my screen, I want to find out what parts that box would be overlapping.

Can you give an example? How does this relates to UI? Are you trying to check whether or not a UI on the screen is blocking a physical part in your screen?

Are you trying to check whether or not a UI on the screen is blocking a physical part in your screen?

yeah pretty much

Is it a specific part(s) you require to check? as you may be able to return the position of the part on the viewport using WorldToScreenPoint then check if this is inside your GUI elements?

No, not specific, could be anything in workspace.

Can you be more specific about your goal? Do you want to know when a part’s rendered shape is fully covered by a rectangle on the screen? Or just when they’re intersecting?

What shape are the parts? Are they always bricks? Can they be mesh parts?

This is possible by turning world space points into screen points and comparing with the position of the UI. However, how hard it is depends on what accuracy you need. Do you want to detect parts that are only completely block / not at all blocked? This is harder. If you only need to check if the center of the part is blocked, that is, approximately blocked, this is way easier.

Either way you will need this function in Camera:

This is only sorta related, but it has code in it you might find useful: