Detect if part is on the screen, even behind anything

What do you want to achieve?
I want to know how to detect if a specific part is on screen, even while other parts are blocking the view.

What solutions have you tried so far?
I’ve tried figuring it out by myself, and also by searching it up but I couldn’t find any answer I’m looking for.

So basically, i want to know how to make it so that a localscript detects If a specific part is on the player’s view, even if other parts are blocking the view, how would I do that? Any help appreciated!

4 Likes
2 Likes

The best approach for this is to use:

local vector, onScreen = camera:WorldToScreenPoint(worldPoint)

This will return false for onScreen if there are parts in front of the target.
However, you can use Camera:GetPartsObscuringTarget to find and filter parts in the way:

--Here is a short X-Ray script--
local function XRay(castPoints, ignoreList)
	ignoreList = ignoreList or {}
	local parts = workspace.CurrentCamera:GetPartsObscuringTarget(castPoints, ignoreList)

	for _, part in pairs(parts) do
		part.LocalTransparencyModifier = 0.75
		for _, child in pairs(part:GetChildren()) do
			if child:IsA("Decal") or child:IsA("Texture") then
				child.LocalTransparencyModifier = 0.75
			end
		end
	end
end

XRay({ Vector3.new() })

This function will make any BaseParts, Decals or Textures coming between the Workspace.CurrentCamera and the given cast points translucent.

Read more here:

2 Likes