Why Camera:WorldToScreenPoint isn't returning correct 2d position

I am wanting to return a 2d position of where my model is on screen. This is so I can position and size UI over said model.

local Viewport = script.Parent.Viewport

-- Get WorldModel
local WorldModel = script:WaitForChild("WorldModel")
WorldModel:PivotTo(CFrame.new(0, 0, 0) * CFrame.Angles(math.rad(180), 0, math.rad(90)))
WorldModel.Parent = Viewport

-- Create Camera
local Camera = Instance.new("Camera")
Camera.CFrame = CFrame.new(
	Vector3.new(0, 10, 0),
	WorldModel:GetPivot().Position
)
Camera.Parent = Viewport

Viewport.CurrentCamera = Camera

--// Open Player card
local function Open()
	InTween:Play()
	InTween.Completed:Wait()
	
	local vector, onScreen = Camera:WorldToScreenPoint(WorldModel:GetPivot().Position)
	local screenPoint = Vector2.new(vector.X, vector.Y)
	
	script.Parent.Frame.Position = UDim2.fromScale(screenPoint.X, screenPoint.Y)
	print(screenPoint)
end

I get 0.500000298, -35.6624527

Whether this is scale or offset, idk, but either way, -35 is not right
ezgif.com-gif-maker (43)

The X and Y components of the returned Vector3 value represent scale from the top left hand corner of the screen and a Y axis scale of -35 means that the specified world point is currently out of view of the camera, you can use the second value returned by WorldToScreenPoint() to determine this. The second value is a Boolean value which represents whether or not the world point is currently visible from the camera’s perspective.

https://developer.roblox.com/en-us/api-reference/function/Camera/WorldToScreenPoint

This still doesn’t really work :confused:

local TopLeft = Vector3.new(
	WorldModel.BottomHalf.Position.X + (WorldModel.BottomHalf.Size.Z / 2),
	WorldModel.BottomHalf.Position.Y,
	WorldModel.BottomHalf.Position.Z - (WorldModel.BottomHalf.Size.X / 2)
)
	
TopLeftPart.Position = TopLeft
local vector, onSreen = Camera:WorldToViewportPoint(TopLeft)
script.Parent.TopLeftFrame.Position = UDim2.fromScale(vector.X, vector.Y)

Even tho the TopLeftPart is positioned at the correct Vector3, the frame isn’t being positioned correctly.

If you look top left. There’s a small yellow part, that’s TopLeftPart. But as the frame increases, the TopLeftFrame (red square frame) slides off the edge of the screen
ezgif.com-gif-maker (47)

When it should over the yellow part

Also, what’s documented is that it returns the offset (pixels) as this image would imply
image
that 222, 85, 18, would return 697, 149 as the vector2
https://developer.roblox.com/en-us/api-reference/function/Camera/WorldToViewportPoint

1 Like