How do I convert a position in a ViewportFrame to a world position?

I have a ViewportFrame that covers the entire screen. I have a position inside the viewport frame that I want to emit particles from, but particles do not work in viewport frames.

I am trying to take the position of the particle emitter in the viewport frame and convert it to a world position so that I can then emit the particle from the workspace instead.

How do I achieve this?

2 Likes

I would recommend looking into WorldToViewportPoint, this seems pretty close to what you’re trying to achieve

1 Like

Unfortunately this function does the opposite of what I need it to. I’ve looked into reversing it but haven’t found much on the subject.

local Vector = ViewportFrame.CurrentCamera:WorldToViewportPoint(Position)
local RayDirection = CurrentCamera:ViewportPointToRay(Vector.X*CurrentCamera.ViewportSize.X, Vector.Y*CurrentCamera.ViewportSize.Y, Vector.Z).Direction * Vector.Z
Attachment.WorldCFrame = CurrentCamera.CFrame + RayDirection
Attachment.WorldCFrame = CurrentCamera.CFrame + CurrentCamera.CFrame:VectorToWorldSpace(Position)

These 2 attempts get me very close to the desired result, but both are a bit off from where they should be. Not sure why. Additional help would really be appreciated.

Seems like if the part is centered at origin, then no problem, but if it’s not then it’s off. I suspect the differences in the two camera’s FOVs is causing the gap.