Weird Glitch With WorldToScreenPoint?

Hi!

I’m trying to make a GUI arrow image that points toward a specific part. This is done by converting the 3D position of the part into 2D using WorldToScreenPoint.

The issue is that when looking certain directions (refer to image & video below) the arrow points in the opposite direction that it’s supposed to. This is further confirmed by some debugging I did. From what I’ve gathered, the on screen position of the part seems to be randomly going from around 500K to around -26K suddenly.

This is the code I use to rotate the image. (Called every RenderStepped)

local pointOnScreen = workspace.CurrentCamera:WorldToScreenPoint(target.Position)
local absPos = GUI.Frame.AbsolutePosition + Vector2.new(GUI.Frame.AbsoluteSize.X / 2, GUI.Frame.AbsoluteSize.Y / 2)
local angle = math.deg(math.atan2(pointOnScreen.Y - absPos.Y, pointOnScreen.X - absPos.X))

GUI.Frame.Rotation = angle


Edit: The video above shows the arrow snapping to a position opposite of the target position.

1 Like

Try increasing the camera’s field of view to higher or lower values and see if the arrow remains normal as long as the part is on screen

local pointOnScreen = workspace.CurrentCamera:WorldToScreenPoint(target.Position)

local screenPoint = Vector2.new(pointOnScreen.X, pointOnScreen.Y)
local absPosition = GUI.Frame.AbsolutePosition + Vector2.new(GUI.Frame.AbsoluteSize.X / 2, GUI.Frame.AbsoluteSize.Y / 2)
local direction = (absPosition - screenPoint).Unit -- I'm assuming your absPosition is correct (I think it is)

local angle = math.deg(math.atan2(direction.Y, direction.X))

I hope this works, I haven’t tested it, but you can try it out.

Solution found! Turns out that I had to just add 180 degrees to the angle variable if pointOnScreen.Z was less than 0.

1 Like