Help with making WorldToViewportPoint return true when a part is on screen

So I want to detect when a part is on screen, or not, but with my current script, it has a small issue. Even if a section of the part is on screen, the actual position isn’t, so it will detect the part as off screen, and I’m not sure how to fix this:

Local script, inside of StartCharacterScripts:

local RunS = game:GetService("RunService")

RunS.RenderStepped:Connect(function(dt)
	for _, part in pairs(workspace:GetDescendants()) do
		if part:IsA("Part") then
			local vector, inScreen = workspace.CurrentCamera:WorldToViewportPoint(part.Position)
			
			if inScreen then
				part.LocalTransparencyModifier = 0.5
				print("yes")
			else 
				part.LocalTransparencyModifier = 1
				print("no")
			end
		end
	end
end)

Any help is appreciated!

Don’t know if this is a good method, but if the part is small enough, you can try to test all eight corners of a part by doing something like this:

local cornerA = part.Position + Vector3.new(part.Size.X / 2, 0, part.Size.Z / 2)
local cornerB = part.Position + Vector3.new(-part.Size.X / 2, part.Size.Y / 2, -part.Size.Z / 2)

Hopefully you get the idea. But if the part you’re talking about is something big like a baseplate or something, then you can use a similar method, by finding a point on the part every certain distance (like 10 studs or some other number). Hope this helps!

1 Like