If i print vector i getr less than 1 for x and y, so i set the gui objects position with scale, which yields unaccurate results.
I find it particularly odd since this function is supposed to return an offset value, which is an integer, does the fact that the camera is in a viewport change anything?
I think the discrepancy is that WorldToScreenSpace is trying to transform that position onto your main screen rather than one of the viewport’s size. I have no clue how to set up a viewport and I’m late for work, but give this a go. Courtesy of Stravant and his ancient ScreenSpace module.
You need to define viewport and camera
function WorldToScreen(at)
local point = camera.CoordinateFrame:pointToObjectSpace(at)
local aspectRatio = viewport.AbsoluteSize.X/viewport.AbsoluteSize.Y
local hfactor = math.tan(math.rad(camera.FieldOfView)/2)
local wfactor = aspectRatio*hfactor
--
local x = (point.x/point.z) / -wfactor
local y = (point.y/point.z) / hfactor
--
return Vector2.new(viewport.AbsoluteSize.X*(0.5 + 0.5*x), viewport.AbsoluteSize.Y*(0.5 + 0.5*y))
end
It’s also a module worth checking out, even nowadays it has some cool stuff in it. For example, you can use it to get the size of a part required to fill the whole screen at a certain distance away.
---replace viewport with your viewportFrame
---replace camera with viewportCamera
local vector = WorldToScreen(part.Position)
frame.Position = UDim2.fromOffset(vector.X,vector.Y)--parented under viewport frame
The issue with the original world to screen point was the new camera Camera.ViewportSize.
When you create a new camera the view port size is only 1,1
Vs the normal one which is dependent on the current window size
Consequently, this is why you are getting the low values for vector XY according to what WorldToScreenPoint/Viewport does.
Consequently, using fromScale should actually work because of the ratios for example.
But since you still have the issue although I believe it works from my replication code, you should check where the frames are parented and such as they can effect Position as well and also your model PrimaryPart is in the right position.