How to know if a part is on the screen?

Example of what I’m trying to do:

If I’m standing on the baseplate, and the baseplate is anywhere on my screen, and press a button, the console should print either true or false.

  • I’ve tried using WorldToScreenPoint, but if I’m not directly over the center of the baseplate, it doesn’t see it.
  • My character can fly, so touching won’t work.
  • Casting 1920x1080 (2+ million) rays… I think that would crash any machine…

Thanks!

what you can do is make a pyramid part and change it’s CFrame and size to be the same as the frustum of the camera then using part:GetPartsInPart() you know what parts are visible/ inside the frustum of the camera that’s how i do it in my ViewFinder game and it works great.

this is what your part should look like
image

1 Like

Alright, so something like this?

exactly! now you use part:GetPartsInPart() to know what parts you can see

I think I need some help…

Can you spot anything obviously wrong here?
image

Because it only shows the results from only one of the corner wedges.

Thanks!

I also notice in your game, your “reticle” is a square and it can spin. How did you set up your camera?
Did you duplicate it from the workspace and zoom it in? If so, how do you know by how much to decrease the FOV? Thanks!

1 Like

ok so this is the function where i calculate the size and position of the part

function getDimentions(cam)
	local corners = {}

	-- the 4 corners of the viewport
	for x = 0, cam.ViewportSize.X , cam.ViewportSize.X do
		for y = 0, cam.ViewportSize.Y , cam.ViewportSize.Y do
			local ray = cam:ViewportPointToRay(x, y, 0)
			local newPosition = ray.Origin + ray.Direction * RAY_DISTANCE
			table.insert(corners, newPosition)
		end
	end

	local center = (corners[2] + corners[3]) / 2
	local size_x = (corners[1] - corners[2]).Magnitude
	local size_y = (center - cam.CFrame.Position).Magnitude
	local size_z = size_x

	local coneCFrame = cam.CFrame * CFrame.new(0, 0, -size_y/2) * CFrame.Angles(math.rad(90), 0, 0)
	local coneSize = Vector3.new(size_x, size_y, size_z)

	return coneCFrame, coneSize
end

this is how i use it

local cFrame, size = getDimentions(photoCamera)
local viewCone = viewConeTemplate:Clone()
viewCone.CFrame = cFrame
viewCone.Size = size

and this is the part used
Union.rbxm (4.4 KB)

This function adjusts the vertical FOV to match a new viewport height while keeping the same perspective. Since FOV scales on the Y axis, resizing affects only the Y axis. For the X axis, aspect ratio handles scaling automatically, so FOV remains unaffected horizontally.

local function calculateFOV(originalFov, currentViewportSize, desiredViewportSize)
	local rad = 2 * math.atan(math.tan(math.rad(originalFov) / 2) * (desiredViewportSize / currentViewportSize))
	return math.deg(rad)
end

In my game, the viewport’s Y-axis size is halved, so you use calculateFOV(70, 1080, 1080/2) to get the adjusted FOV. which returns 38.59 which is exactly what we need

1 Like

THANK YOU SO SO MUCH!!!

1 Like

I did change a few things though:

local function getDimentions(absolutePos, absoluteSize, cam) --< here
	
	local corners = {}

	for x = absolutePos.X, absolutePos.X+absoluteSize.X , absoluteSize.X do     --< here
		for y = absolutePos.Y, absolutePos.Y+absoluteSize.Y , absoluteSize.Y do --< here
			print(x,y)
			local ray = cam:ScreenPointToRay(x, y)
			local newPosition = ray.Origin + ray.Direction * RAY_DISTANCE
			table.insert(corners, newPosition)
		end
	end

	local center = (corners[2] + corners[3]) / 2
	local size_x = (corners[1] - corners[2]).Magnitude
	local size_y = (center - cam.CFrame.Position).Magnitude
	local size_z = (corners[1] - corners[3]).Magnitude --< and here

	local coneCFrame = cam.CFrame * CFrame.new(0, 0, -size_y/2) * CFrame.Angles(math.rad(90), 0, 0)
	local coneSize = Vector3.new(size_x, size_y, size_z)

	return coneCFrame, coneSize
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.