Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I get a list of parts that are inside a Frame?

Asked by
luadotorg 194
5 years ago

Hello.

I'm in the process of making a RTS game in Roblox, but I've came up with an issue.

Making it short, I'm currently scripting a selection box UI. I've got it ready; it's like the Windows right click dragging that forms that blue rectangle box.

However, after detecting that the user has lifted their mouse left button and it is ready to detect units inside of the rectangle, I have came unable to be able to list these.

Does anybody have any idea how I could do this?

I've got this so far, but of course it doesn't work.

local xS = script.Parent.Selection.Position.X.Offset
            if script.Parent.Selection.Size.X.Offset < 0 then
                xS = xS - math.abs(script.Parent.Selection.Size.X.Offset)
            end

            local xE = script.Parent.Selection.Position.X.Offset
            if script.Parent.Selection.Size.X.Offset > 0 then
                xE = xE + script.Parent.Selection.Size.X.Offset
            end

            local yS = script.Parent.Selection.Position.Y.Offset
            if script.Parent.Selection.Size.Y.Offset < 0 then
                yS = yS - math.abs(script.Parent.Selection.Size.Y.Offset)
            end

            local yE = script.Parent.Selection.Position.Y.Offset
            if script.Parent.Selection.Size.Y.Offset > 0 then
                yE = yE + script.Parent.Selection.Size.Y.Offset
            end

            local castPoints = {}

            print(xS)
            print(xE)
            warn(yS)
            warn(yE)

            for x = xS, xE do for y = yS, yE do
                table.insert(castPoints, Vector3.new(x, y, 0))
            end end 
            local objs = workspace.CurrentCamera:GetPartsObscuringTarget(castPoints, { })

            for i, v in pairs(objs) do
                print(v.Name)
            end
0
The best I can come up with at the moment is looking in to https://developer.roblox.com/api-reference/function/Camera/ScreenPointToRay and then doing some maths to work out intersection in a shape. fredfishy 833 — 5y

1 answer

Log in to vote
0
Answered by
luadotorg 194
5 years ago

I've got to the conclusion and answer about this problem.

Thanks @fredfishy for hinting me on ScreenPointToRay!

local dist = (workspace.CurrentCamera.CoordinateFrame.p - lp.Character.Head.Position).magnitude -- Calculates the camera's current distance

for x = xS, xE do
    for y = yS, yE do
        local ray = workspace.CurrentCamera:ScreenPointToRay(x, y, dist * 5) -- Sets maximum detection distance to 5 times the distance

        table.insert(castPoints, ray.Origin) -- Only using Origin to prevent camera rotation bugging the selection
    end
end

local objs = workspace.CurrentCamera:GetPartsObscuringTarget(castPoints, { })
Ad

Answer this question