Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you use WorldToViewportPoint()?

Can someone explain what WorldToViewportPoint() does? This is what I currently have:

while true do
local CanSeePart = game.workspace.CurrentCamera:WorldToViewportPoint(game.workspace.Part.Position)
	if CanSeePart then
	print("Part is visible!")
	else
		print("Can't see the part!")
	end
	wait()
end

It keeps printing "Part is visible!", even If I look away. Did I do something wrong?

Thanks.

1 answer

✓ Accepted answer

Takes a 3D world point and returns the following: A Vector3 where the X/Y components map the position in screen coordinates the worldPoint maps to, and the Z component maps how far away from the screen plane the 3d position is (positive if in front of camera, negative if behind). And a boolean which is true if the point is visible on the screen or not.

Source: Roblox Wiki

In other words, you need to use the second returned value of the function to get whether it is visible or not.

local position, isVisible = game.workspace.CurrentCamera:WorldToViewportPoint(game.workspace.Part.Position)

print(isVisible) -- Whether the part is currently visible or not.

This discussion is preserved as it was. New answers, comments, and votes are closed.