✓ Accepted answer
Stravant has a ScreenSpace module script which contains multiple functions for interactions between the 2D screen and the 3D environment. I'll give you the function from the module that deals with what you're asking for:
local M = game.Players.LocalPlayer:GetMouse()
local Cam = game.Workspace.CurrentCamera
function WorldToScreen(Pos)
local point = Cam.CoordinateFrame:pointToObjectSpace(Pos)
local aspectRatio = M.ViewSizeX / M.ViewSizeY
local hfactor = math.tan(math.rad(Cam.FieldOfView) / 2)
local wfactor = aspectRatio*hfactor
local x = (point.x/point.z) / -wfactor
local y = (point.y/point.z) / hfactor
return Vector2.new(M.ViewSizeX * (0.5 + 0.5 * x), M.ViewSizeY * (0.5 + 0.5 * y))
end
local Vec2 = WorldToScreen(Vector3.new(0, 50, 0))
Gui.Position = UDim2.new(0, Vec2.X, 0, Vec2.Y)
Hope this helped!
Note: This only works if it's in a localscript. Also, the Vector2 will be the coordinates of the point from the top left corner of the screen, so if the gui is located in another gui that's offset from the (0, 0), the gui will be positioned incorrectly.
1 comment
it doesn't work ;_;