Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you use WorldToViewportPoint()?

Asked by 7 years ago

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

Log in to vote
2
Answered by 7 years ago

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.
Ad

Answer this question