im trying to make a script that makes lines and connects them between skill tree nodes. The problem is the lines get misplaced and even the distance and angle calculations are incorrect for some reason. I tried to set the offset of the line(frame) directly to the button with
line.Position = UDim2.fromOffset(obj1.AbsolutePosition.X, obj1.AbsolutePosition.Y)
That would give it this weird offset, and even printing out the absolute position of the line and the button would give different results despite setting it directly equal to one another.
The nodes(buttons) have UIAspectRatioConstrains but even without it it still doesnt work. The anchor point of buttons is 0.5, 0.5, but even with 0, 0 it doesnt work. The parent container for lines is identical to parent container for buttons, but even parenting it to the same parent doesnt work. IgnoreGuiInset is on but it doesnt make a difference. What did somewhat work was
line.Position = obj1.Position
or parenting it to the button directly. This still makes the angle and size calculations fail though, even when I try doing them with scale. Parenting the line to the button doesnt work since I need the line to be rendered behind the button(and the calculations dont work).
Heres the current code:
local function makeLine(obj1: Frame, obj2: Frame)
local pos1 = obj1.AbsolutePosition --+ obj1.AbsoluteSize * obj1.AnchorPoint
local pos2 = obj2.AbsolutePosition --+ obj2.AbsoluteSize * obj2.AnchorPoint
local pos = (pos1 + pos2) / 2
local addedPos = (obj1.Position + obj2.Position)
local subtractedPos = (obj1.Position - obj2.Position)
local scaleSize = Vector2.new(subtractedPos.X.Scale, subtractedPos.Y.Scale).Magnitude
local offsetSize = Vector2.new(subtractedPos.X.Offset, subtractedPos.Y.Offset).Magnitude
local midpoint = UDim2.new(addedPos.X.Scale / 2, addedPos.X.Offset / 2, addedPos.Y.Scale / 2, addedPos.Y.Offset / 2)
local size = (pos1 - pos2).Magnitude --+ obj1.AbsoluteSize.Y
local angle = math.deg(math.atan2(pos2.X - pos1.X, pos2.Y - pos1.Y))
local line = Instance.new("Frame")
--line.Size = UDim2.new(scaleSize, offsetSize, 0, 2)
line.Size = UDim2.fromOffset(size, 2)
line.BackgroundColor3 = Color3.new(1, 1, 1)
line.BorderSizePixel = 0
--line.Position = UDim2.new(0.5, pos.X, 0.5, pos.Y)
line.Name = obj1.Name
--line.Rotation = angle
--line.Rotation = 45
--line.AnchorPoint = Vector2.new(0.5, 0.5)
obj1.AnchorPoint = Vector2.zero
--[[line.Parent = obj1
print(line.AbsolutePosition)
local absPos = line.AbsolutePosition + line.AbsoluteSize * line.AnchorPoint
local absSize = line.AbsoluteSize
line.Size = UDim2.fromOffset(absSize.X, absSize.Y)
line.Parent = obj1.Parent.Parent.Parent.Lines
line.Position = UDim2.fromOffset(absPos.X, absPos.Y)
print(line.AbsolutePosition)]]
--line.Position = UDim2.fromOffset(pos1.X + line.AbsoluteSize.X / 2, pos1.Y + line.AbsoluteSize.Y / 2)
--line.Position = UDim2.fromOffset(pos1.X, pos1.Y)
line.Parent = obj1.Parent.Parent.Parent.Lines
local offsetVector = (obj2.AbsolutePosition - obj1.AbsolutePosition).Unit * line.AbsoluteSize.X / 2
--line.Position = midpoint
line.Position = UDim2.fromOffset(obj1.AbsolutePosition.X, obj1.AbsolutePosition.Y)
--line.Position = obj1.Position + UDim2.fromOffset(offsetVector.X, offsetVector.Y)
--task.delay(0.1, print, obj1.AbsolutePosition, line.AbsolutePosition)
--line.Parent = obj1
return line
end
Ive commented out a lot of my other attempts.
