Trying to make a line between two points

I’m attempting to create a line between two points using a frame (Vector2). The first point is simply the center of the screen, while the second is a white dot but I’m not sure what I’m doing.

Here’s the code I’m using:

local startPosition = Camera.ViewportSize / 2 --center of the screen
local endPosition = Vector2.new(ScreenPosition.X, ScreenPosition.Y) --white dot
		
local line = Instance.new("Frame")
		
local length = (endPosition - startPosition).magnitude
local angle = math.atan2(endPosition.Y - startPosition.Y, endPosition.X - startPosition.X)

line.Size = UDim2.new(0, length, 0, 2)
line.Position = UDim2.new(0, startPosition.X, 0, startPosition.Y)
line.Rotation = math.deg(angle)
line.Parent = script.Parent.Parent

This is what is happening:
image
The line should be from my mouse (center of the screen) to the white dot on that part.

It looks like the line is being drawn correctly but it’s just misplaced.

The line is parented to a ScreenGui with IgnoreGuiInset set to true.

2 Likes
local startPosition = Camera.ViewportSize / 2 --center of the screen
local endPosition = Vector2.new(ScreenPosition.X, ScreenPosition.Y) --white dot
		
local line = Instance.new("Frame")
		
local length = (endPosition - startPosition).Magnitude
local angle = math.atan2(endPosition.Y - startPosition.Y, endPosition.X - startPosition.X)
local center = Vector2.new((startPosition.X + endPosition.X) / 2, (startPosition.Y + endPosition.Y) / 2)

line.Size = UDim2.new(0, length, 0, 2)
line.AnchorPoint = Vector2.new(0.5, 0.5)
line.Position = UDim2.new(0, center.X, 0, center.Y)
line.Rotation = math.deg(angle)
line.Parent = script.Parent.Parent

The position was a bit off.

1 Like

You could also just use a beam from your characters head to the part by adding attachments to the part & head and making a new beam instance.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.