Ok, so to start off, what I am trying to do is find all of the parts in the 3d game world that a 2d UI Frame would theoretically contain based off of the camera's rotation, field of view, and such if the 2d UI Frame was given a 3d coordinate, and a depth-axis that extends back about 1000 studs.
The method I have coded to do so involves creating two rays at the upper and lower corners of the UI Frame, and creating a Region3 from there, and using the function Workspace:FindPartsInRegion3() to get all of the parts that the UI Frame would theoretically contain if it were converted to a 3d Region3 with the conditions previously stated above.
Here is my code:
local function GetPartsInUI(ui)
local cam = game.Workspace.CurrentCamera;
local min_corner = ui.AbsolutePosition;
local max_corner = ui.AbsolutePosition + ui.AbsoluteSize;
local r1 = cam:ScreenPointToRay(min_corner.X, min_corner.Y, 5);
local r2 = cam:ScreenPointToRay(max_corner.X, max_corner.Y, 5);
local pos1 = CFrame.new(r1.Origin, (r1.Direction - r1.Origin).unit);
local pos2 = CFrame.new(r2.Origin, (r2.Direction - r2.Origin).unit);
pos1 = pos1 - (pos1.upVector * 999); --increase the imaginary depth axis by about 1000 studs
local pos1_sum = pos1.p.X + pos1.p.Y + pos1.p.Z;
local pos2_sum = pos2.p.X + pos2.p.Y + pos2.p.Z;
local reg3 = Region3.new((function()
local rt = {};
if pos1_sum > pos2_sum then
rt[1] = pos2.p;
rt[2] = pos1.p;
else
rt[1] = pos1.p;
rt[2] = pos2.p;
end
return unpack(rt);
end)());
local parts = game.Workspace:FindPartsInRegion3(reg3, nil, math.huge);
return parts;
end
My problem is that half of the time the parts inside of the UI Frame are not recognized to be inside of the Region3.
I am assuming that this is either due to my setting up of the position (pos1 and pos2) variables, or an issue with the rays returned from the :ScreenPointToRay() function.
With all of that being said, please tell me what am I doing wrong, and how can I improve upon this???
Thanks
