I’m trying to make a selectionbox type of thing in studio, currently im trying to project 2 points in 2d space into the 3d workspace, I tried this first using rays but i had trouble with camera, so now I’m trying to project these points outward at the same distance for an easier time extending and sizing correctly.
My issue isthat camera:ScreenPointToRay.Direction is giving me a tiny value, ive tried multiplying but I either cant find the right value or am trying the wrong thing.
https://gyazo.com/98d97840bb24aea357f0a502ce0e91ad
In this video im getting my screenpoints from those two grey squares, as you can see the parts, both being positioned with cframe.lookat end up entirely wrong.
local length = 5
local toprayinf, bottomrayinf = workspace.CurrentCamera:ScreenPointToRay(topcorner.X.Offset, topcorner.Y.Offset), workspace.CurrentCamera:ScreenPointToRay(bottomcorner.X.Offset, bottomcorner.Y.Offset)
part.CFrame = CFrame.lookAt(toprayinf.Origin, toprayinf.Direction)
part.Position = part.Position + (part.CFrame.LookVector * length)
If you need testing here’s the full script + startergui layout
local holding = false
local frame = script.Parent.Frame
local mouseoffset = 50
local raylength = 500
local length = 5
game:GetService("UserInputService").InputBegan:Connect(function(input, gpe)
if not gpe and input.UserInputType == Enum.UserInputType.MouseButton1 then
holding = true
local mouse = game:GetService("UserInputService"):GetMouseLocation()
frame.Position = UDim2.fromOffset(mouse.X, (mouse.Y - mouseoffset))
frame.Visible = true
end
end)
game:GetService("UserInputService").InputEnded:Connect(function(input, gpe)
if not gpe and input.UserInputType == Enum.UserInputType.MouseButton1 then
holding = false
local topcorner, bottomcorner = frame.Position, frame.Position + frame.Size
local toprayinf, bottomrayinf = workspace.CurrentCamera:ScreenPointToRay(topcorner.X.Offset, topcorner.Y.Offset), workspace.CurrentCamera:ScreenPointToRay(bottomcorner.X.Offset, bottomcorner.Y.Offset)
local topray, bottomray = workspace:Raycast(toprayinf.Origin, toprayinf.Direction * raylength), workspace:Raycast(bottomrayinf.Origin, bottomrayinf.Direction * raylength)
if topray and bottomray then
local framemin, framemax = frame.Position, frame.Position + frame.Size
local frame1 = Instance.new("Frame")
frame1.Size = UDim2.fromOffset(10,10)
frame1.Position = framemin
frame1.Parent = game:GetService("Players").LocalPlayer.PlayerGui.Selectionbox
frame1.AnchorPoint = Vector2.new(0.5,0.5)
local frame2 = Instance.new("Frame")
frame2.Size = UDim2.fromOffset(10,10)
frame2.Position = framemax
frame2.Parent = game:GetService("Players").LocalPlayer.PlayerGui.Selectionbox
frame2.AnchorPoint = Vector2.new(0.5,0.5)
local part = Instance.new("Part", workspace)
part.Anchored = true
part.CFrame = CFrame.lookAt(toprayinf.Origin, toprayinf.Direction)
part.Position = part.Position + (part.CFrame.LookVector * length)
print(part)
local part2 = Instance.new("Part", workspace)
part2.Anchored = true
part2.CFrame = CFrame.lookAt(bottomrayinf.Origin, toprayinf.Direction)
part2.Position = part2.Position + (part2.CFrame.LookVector * length)
print(part2)
local Alflsdf = 1.2
local part23 = Instance.new("Part")
part23.Parent = workspace
part23.CFrame = CFrame.lookAt(workspace.CurrentCamera.CFrame.Position, (topray.Position+bottomray.Position)/2)
part23.Size = Vector3.new(math.abs(topray.Position.X-bottomray.Position.X)/Alflsdf, math.abs(topray.Position.Z-bottomray.Position.Z)/Alflsdf, raylength)
part23.CanCollide = false
part23.Anchored = true
part23.Transparency = 0.5
local selectionlist = workspace:GetPartBoundsInBox(CFrame.lookAt(workspace.CurrentCamera.CFrame.Position, (topray.Position+bottomray.Position)/2),Vector3.new(math.abs(topray.Position.X-bottomray.Position.X), math.abs(topray.Position.Z-bottomray.Position.Z), raylength))
print(selectionlist)
for i, selection in selectionlist do
if selection:HasTag("Troop") then
local a = Instance.new("Highlight")
a.FillTransparency = 1
a.Parent = selection
end
end
end
end
end)
game:GetService("RunService").Heartbeat:Connect(function()
if holding then
local startpos = script.Parent.Frame.Position
local mouse = game:GetService("UserInputService"):GetMouseLocation()
frame.Size = UDim2.fromOffset(mouse.X - frame.AbsolutePosition.X, mouse.Y - (frame.AbsolutePosition.Y + mouseoffset))
end
end)