How to check if a part is within a frame

I want to check if a part is within a frame / gui, so for example:
image
This would be true as the part is within the frame

image
This would be false as the part is outside the frame

I know of camera:WorldToScreenPoint() but don’t know if that is relevant / how I would use it.

2 Likes

Well you would be able to get the position of the part with camera:WorldToScreenPoint and then use absolute position and size to see if its inside the regions

local function GetFrameEdges(frame : Frame)
	local size = frame.AbsoluteSize
	local pos = frame.AbsolutePosition
	local anchor = frame.AnchorPoint


	return pos.Y - size.Y*anchor.Y, pos.Y + size.Y-(size.Y*anchor.Y), pos.X- size.X*anchor.X, pos.X + size.X-(size.X*anchor.X)
end

local function IsPartInFrame(part : Part, frame : Part)
	local part_pos, on_screen = workspace.CurrentCamera:WorldToScreenPoint(part.Position)
	if on_screen then
		local top, bottom, left, right = GetFrameEdges(frame)
		return part_pos.Y > top and part_pos.Y < bottom and part_pos.X > left and part_pos.X < right
	end
end

Example

4 Likes