How to tell wether a point is inside a viewportframe's view

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to tell wether a point is inside a viewport frame.
  2. What is the issue? Include screenshots / videos if possible!
    No idea how
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    viewportCamera:WorldToViewportPoint(position)
    But this doesn’t work. I suppose it is due to the difference in fov.

Please help me with this problem

If you’re using the Workspace’s CurrentCamera as the ViewportFrame’s CurrentCamera the WorldToViewportPoint method returns the screen position in pixels and checks if the point is visible on the player’s screen, not the ViewportFrame.

If you’ve created a new camera for the ViewportFrame then WorldToViewportPoint returns the screen position in scale where (0, 0) is top left corner and (1, 1) is bottom right corner of a 1:1 square fit inside the ViewportFrame, regardless of the ViewportFrame’s aspect ratio.

Here’s a video showing what the returned screen position is at different parts of the screen:


With this in mind you can check if a point is outside the ViewportFrame by using the following checks:

-- Select first value [screen position] from tuple returned by WorldToViewportPoint
local screenPos = select(1, --[[Camera]]:WorldToViewportPoint(--[[position]]))

-- Calculate offset from a 1:1 square's left edge to ViewportFrame's actual left edge
local xSize, ySize = viewport.AbsoluteSize.X, viewport.AbsoluteSize.Y
local xEdgeOffset = (xSize - ySize) / (ySize * 2)

if screenPos.X < -xEdgeOffset or screenPos.X > 1 + xEdgeOffset then
	-- Point is outside ViewportFrame on x-axis
elseif screenPos.Y < 0 or screenPos.Y > 1 then
	-- Point is outside ViewportFrame on y-axis
end
1 Like

I am not using the current camera, sorry for not explaning, I am using a custom camera, with a custom viewportframe and objects to be displayed are parented to the viewport frame.

what I’m trying to do is zoom into these eggs