Help getting a table part rendered in a specific viewport

I am trying to make a sort of drag selection system right now. I have a completely working dragger, but I am a little confused on how I should be able to get a list of 3d parts from what’s in the dragger.

local mouse = game.Players.LocalPlayer:GetMouse()
local selectorFrame
local oldPos

function mousePosition()
	return UDim2.fromOffset(mouse.X,mouse.Y)
end

mouse.Button1Down:Connect(function()
	oldPos = mousePosition()
	
	selectorFrame = Instance.new("Frame",script.Parent)
	selectorFrame.BackgroundTransparency = .5
	selectorFrame.Position = oldPos
end)

mouse.Button1Up:Connect(function()
	selectorFrame:Destroy()
end)

mouse.Move:Connect(function()
	if selectorFrame then
		selectorFrame.Size = mousePosition()-oldPos
	end
end)

Hi it’s me again

Two ways:

  1. get the viewport position of every part via Camera:WorldToViewportPoint and check if they are within the 2D bounding box.

  2. Convert the 2D bounding box to a 3D box and use spatial query via workspace:GetPartsInPart with an arbitrary Z length to find all intersecting parts.

The difference between these two methods is the first one only takes into account the center of each part, while the latter will consider parts that simply intersect the bounding box.

I (definitely coincidently) have a place file for the first method here:
2DSelectionOn3DSpace Button1Up.rbxl (37.5 KB)

Note that I made that back in 2021 so expect some horrendous coding practices, like ZERO TYPE ANNOTATIONS

2 Likes

I didn’t know about the screenpoint function, but I read your code and took some notes. I’m thinking of so many things that I quit on because I didn’t know about this. Thanks you!

Always, ALWAYS read the damn documentation! Go save yourself hours of effort by using something Roblox has already made instead of making them yourself from scratch. I cannot stress this enough.

For example, I just discovered this Camera method a few weeks ago:
image
And these new math stuff for Vector3s ported over from the scalar math library

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.