Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make a line between 2 UDim2 positions?

How can I make a line between 2 UDim2 positions? Similar like with CFrame, but I cant get this one working. Could you help me ;D ?

1 answer

Try this:

-- @author Narrev
-- Draw a line between 2 UDim2 positions
-- Does not support Scale

local Position1 = UDim2.new(0,10,0,0)
local Position2 = UDim2.new(0,100,0,100)
local Line_Thickness = 2
local Parent = script.Parent

local function Draw(Position1, Position2)
	local Distance_X, Distance_Y = Position2.X.Offset - Position1.X.Offset, Position2.Y.Offset - Position1.Y.Offset
	local Distance = (Distance_X * Distance_X + Distance_Y * Distance_Y) ^ .5
	local Angle = math.atan2(Distance_Y, Distance_X)

	-- Create Line
	local Line = Instance.new("Frame", Parent)
	Line.Size = UDim2.new(0, Distance, 0, Line_Thickness)
	local Center_X, Center_Y = 0.5 * (Position1.X.Offset + Position2.X.Offset), 0.5 * (Position1.Y.Offset + Position2.Y.Offset)
	Line.Position = UDim2.new(0, Center_X - 0.5*Distance, 0, Center_Y - 0.5*Line_Thickness)
	Line.Rotation = math.deg(Angle)
end

Draw(Position1, Position2)

2 comments

0

Thank you, I didnt think about using atan for the rotation, ;)

0

No problem! Please accept answer if I helped!

This discussion is preserved as it was. New answers, comments, and votes are closed.