Ray with gui objects

You can write your topic however you want, but you need to answer these questions:

  1. I want to connect two points of the screen together with a frame (like a ray with two gui objects)

  2. I tried raying the two world points

  3. The ray works, but not with orientation

I once wrote a script that allows me to draw a line between 2 Vector Points, maybe you can use that.

	ClientUtil.DrawLine = function(Point1, Point2)
		local p1, p2 = Point1, Point2
		local v = (p2 - p1)
		local frame = Instance.new("Frame")
		frame.BackgroundColor3 = Color3.fromRGB(0, 255, 0)
		frame.BorderSizePixel = 0
		frame.Name = "Line"
		frame.Size = UDim2.new(0, v.magnitude, 0, 1)
		frame.Rotation = math.deg(math.atan2(v.y, v.x))
		frame.Position = UDim2.new(0, (p1.x + v.x/2) - frame.Size.X.Offset/2, 0, (p1.y + v.y/2) - frame.Size.Y.Offset/2)
		return frame
	end

Just note, do not give the position itself directly, you have to calculate the absolute position on the screen for this.

local p1, p2 = Frame1.AbsolutePosition + (Frame1.AbsoluteSize/2), Frame2.AbsolutePosition + (Frame2.AbsoluteSize/2)

And then you just call DrawLine(p1, p2), which then returns a line between those positions.

Example Code:

local p1, p2 = Frame1.AbsolutePosition + (Frame1.AbsoluteSize/2), Frame2.AbsolutePosition + (Frame2.AbsoluteSize/2)
local Line = ClientUtil.DrawLine(p1, p2)
Line.Parent = script.Parent
local Pos = Line.AbsolutePosition
local FramePos = ParentFrame.AbsolutePosition
local PosForLine = Pos - FramePos
Line.Position = UDim2.new(0, PosForLine.X, 0, PosForLine.Y)

Frame1: The first dot
Frame2: The second dot
ParentFrame: The parent frame where the dot should be parented to (Needs to have an Absolute Position!)

3 Likes

It worked! Thank you! :grinning: