Detecting if a part is on a players screen

Hello, I am needing help with this. How would I detect if a part is in a players screen, for example, if the part is showing on the players screen, partSeen = true but if the part is say, blocked by a wall partSeen = false?
Thank you in advance :slight_smile:

This can be done using RayCasting and WorldToScreenPoint().

Here’s the code I used for my game:

local RunService = game:GetService("RunService")
local Target = workspace.MainPart

RunService.RenderStepped:Connect(function()
	local _, withinScreenBounds = workspace.CurrentCamera:WorldToScreenPoint(Target.Position)
		
	if withinScreenBounds then
		local ray = Ray.new(workspace.CurrentCamera.CFrame.Position,(Target.CFrame.Position - workspace.CurrentCamera.CFrame.Position))
		local Part = workspace:FindPartOnRay(ray,game:GetService("Players").LocalPlayer.Character)
		if Part == Target then
			print("on screen")
		end
	end
end)
5 Likes

Thanks a lot! I’ll try it out.

There is an example of how you can achieve this on the devhub : Camera | Documentation - Roblox Creator Hub

1 Like