I want to create a 2d to 3d selection system via mouse dragging. Unfortunately when I use region3, it either creates a part or it creates a really tiny part.
What I have:
I have the 2 points where I want a region to be at using rays but I’m unable to use Region3. I’ve read the Developer Hub but I’m genuinely confused if the second argument is where you want the other end to be at or the size so I’ve also calculated the sizing.
https://gyazo.com/fc3b4121e45e400c7c9742b8ce6ed9c4
Code:
--|| SERVICES ||--
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--|| VARIABLES ||--
local Player = Players.LocalPlayer
local Camera = game.Workspace.CurrentCamera
local Mouse = Player:GetMouse()
local PlayerGui = Player:WaitForChild("PlayerGui")
local MainGUI = PlayerGui:WaitForChild("MainGUI")
--|| MODULES ||--
local GlobalFunctions = require(ReplicatedStorage.GlobalFunctions)
local Connections = {
MouseMove = nil,
MouseRelease = nil
}
local function cleanUpConnections(exceptionList)
for ConnectionName, Connection in pairs(Connections) do
if not exceptionList[ConnectionName] then
Connection:Disconnect()
Connection = nil
end
end
end
Mouse.Button1Down:Connect(function()
local x1,y1 = Mouse.X, Mouse.Y
local x2,y2 = x1, y1
--> Clean up any previous functions that may have exited
cleanUpConnections({})
Connections.MouseMove = Mouse.Move:Connect(function()
x2 = Mouse.X
y2 = Mouse.Y
--> Changing the Anchor Point under certain conditions
if x2 < x1 then
MainGUI.SelectionFrame.AnchorPoint = Vector2.new(1,MainGUI.SelectionFrame.AnchorPoint.Y)
else
MainGUI.SelectionFrame.AnchorPoint = Vector2.new(0,MainGUI.SelectionFrame.AnchorPoint.Y)
end
if y2 < y1 then
MainGUI.SelectionFrame.AnchorPoint = Vector2.new(MainGUI.SelectionFrame.AnchorPoint.X,1)
else
MainGUI.SelectionFrame.AnchorPoint = Vector2.new(MainGUI.SelectionFrame.AnchorPoint.X,0)
end
--> Changing the size
MainGUI.SelectionFrame.Position = UDim2.new(0,x1,0,y1)
MainGUI.SelectionFrame.Size = UDim2.new(0,math.sqrt((x2-x1)^2),0,math.sqrt((y2-y1)^2))
end)
Connections.MouseRelease = Mouse.Button1Up:Connect(function()
MainGUI.SelectionFrame.Size = UDim2.new(0,0,0,0)
MainGUI.SelectionFrame.AnchorPoint = Vector2.new(0,0)
cleanUpConnections({})
local pointX = Camera:ScreenPointToRay(x1,y1,1)
local pointY = Camera:ScreenPointToRay(Mouse.X,Mouse.Y,1)
local rayx = Ray.new(pointX.Origin, pointX.Origin + pointX.Direction*100)
local rayy = Ray.new(pointY.Origin, pointY.Origin + pointY.Direction*100)
local hitx, posx = game.Workspace:FindPartOnRayWithWhitelist(rayx, {game.Workspace.World})
local hity, posy = game.Workspace:FindPartOnRayWithWhitelist(rayy, {game.Workspace.World})
local sizeX = math.abs(posx.X - posy.X)
local sizeY = math.abs(pointX.Origin.Y - posx.Y)
local sizeZ = math.abs(posx.Z - posy.Z)
GlobalFunctions.createVisualRay(pointX.Origin, pointX.Origin + pointX.Direction*100)
GlobalFunctions.createVisualRay(pointY.Origin, pointY.Origin + pointY.Direction*100)
local region = Region3.new(posx, posy)
local Part = Instance.new("Part", game.Workspace)
Part.CFrame = region.CFrame
Part.Size = region.Size
-- local selectedRegion = Region3.new(posx, Vector3.new(sizeX,sizeY,sizeZ))
-- local res = 4
-- selectedRegion = selectedRegion:ExpandToGrid(res)
-- local terrain = game.Workspace:WaitForChild("Terrain")
-- terrain:FillRegion(selectedRegion, 4, Enum.Material.Water)
end)
end)
return Connections