Screen coordinates of object within Viewport frame?

Solved: Figured it out myself - was accidentally transforming using the wrong object. oops.

Feel like I’m missing something here / it’s late and I’m being a little silly. This picture will describe what I’m trying to do better than I can explain it:

I’m trying to get the screen coordinates of an object I’m viewing within a ViewportFrame to draw a line from the horizontal lines to such object.

I’m getting close as you can see, but things are way off. What I’ve done so far is get the Offset coords via the Viewport’s CurrentCamera using the WorldToScreenPoint method, and then attempted to transform it… However, as you can see it doesn’t seem to work. Any suggestions, please?

4 Likes

Since you managed to figure it out, mind posting what the solution was?

4 Likes

Sure. Essentially:

local vCamera = Viewport.CurrentCamera
local Pos = vCamera:WorldToViewportPoint(Obj.Position)
Pos = UDim2.new(0, Pos.X * Viewport.Parent.AbsoluteSize.X, 0, Pos.Y * Viewport.Parent.AbsoluteSize.Y)

Then just draw a line from your start position to the position defined above and it’s done. I’m unsure as to why, but the issue was with the method WorldToScreenPoint as well as me transforming with the incorrect object.

6 Likes