How can i make this function more accurate

function WorldToScreen(Object , at)
	local viewportFrame = Object.Adornee.Parent.Parent
	local viewportCamera = viewportFrame.CurrentCamera
	local point = viewportCamera.CoordinateFrame:pointToObjectSpace(at)
	local aspectRatio = viewportFrame.AbsoluteSize.X/viewportFrame.AbsoluteSize.Y
	local hfactor = math.tan(math.rad(viewportCamera.FieldOfView)/2)
	local wfactor = aspectRatio*hfactor
	--
	local x = (point.x/point.z) / -wfactor
	local y = (point.y/point.z) /  hfactor
	--
	return Vector2.new(viewportFrame.AbsoluteSize.X*(0.5 + 0.5*x), viewportFrame.AbsoluteSize.Y*(0.5 + 0.5*y))
end


I ran the function once for each red cube and set the value returned as the position for these two things, how can i make the function more accurate?

1 Like

Leaving this here just in case: Camera | Roblox Creator Documentation

1 Like

That doesn’t work with viewports, because all cameras except the ones in the workspace are deleted, and if i make a camera with a script, it’s viewportsize is always 1,1 which breaks that function.

1 Like

How about Camera | Roblox Creator Documentation ?

Gonna try that, seems promising.

This yields the same result, though slightly further down.

Can you post the code of what you’re trying to do?

	local Effect = game.ReplicatedStorage.GuiElements.PunchEffect:Clone()
	local Vector = WorldToScreen(EnemyCreature, EnemyCreature.Adornee.CreatureRig.PrimaryPart.Position)
	Effect.Position = UDim2.fromOffset(Vector.X , Vector.Y)
	Effect.Parent = SelfCreature.Adornee:FindFirstAncestorWhichIsA("ScreenGui")