Hi!
I’m working on a simple system which gets two points on screen (center of screen and a point transformed from 3D onto the viewport) and then creating a line connecting the two. I use simple trigonometry to create the line.
Desired behavior:
I’m able to achieve the desired behavior with one exception. At some point, the line seems to flip and connect to the opposite of the point where the actual object is.
Undesired behavior:
I’ve also noticed that at some point the screen position of the object randomly jumps from [num] to [num].
Undesired behavior output:
Here’s my code for creating the line:
--called every RenderStepped
function updateLine(part)
local pos,on=workspace.CurrentCamera:WorldToViewportPoint(part.Position)
local p1=Vector2.new(gui.AbsoluteSize.X*0.5,gui.AbsoluteSize.Y*0.5)
local p2=Vector2.new(pos.X,pos.Y)
local p3=Vector2.new(p2.X,p1.Y)
local hyp=(p2-p1).Magnitude
local opp=math.abs(p2.Y-p1.Y)
local alpha=math.deg(math.asin(opp/hyp))
gui.p1.Position=UDim2.fromOffset(p1.X,p1.Y) --used for visualization only
gui.p2.Position=UDim2.fromOffset(p2.X,p2.Y) --used for visualization only
gui.p3.Position=UDim2.fromOffset(p3.X,p3.Y) --used for visualization only
gui.Line.Size=UDim2.new(0,hyp,0,1)
gui.Line.Position=UDim2.new(0,(p2.X+p1.X)/2,0,(p2.Y+p1.Y)/2)
if p2.Y>p1.Y then alpha=-alpha end
if p2.X>p1.X then alpha=90+(90-alpha) end
gui.Line.Rotation=alpha
end