Detect if a part is on screen issue

  1. What do you want to achieve?
    I want to know how it should check if a part is on the screen but not entirely
  2. What is the issue?
    It only detects if the whole part is on the screen
  3. What solutions have you tried so far?
for i,v in next, script.Parent.Parent.Parent:FindFirstChild("Parts").Value:GetChildren() do
	local _, withinScreenBounds = workspace.CurrentCamera:WorldToViewportPoint(v.Position)
		
	if withinScreenBounds then
		local ray = Ray.new(workspace.CurrentCamera.CFrame.Position,(v.CFrame.Position - workspace.CurrentCamera.CFrame.Position))
		local Part = workspace:FindPartOnRay(ray,game:GetService("Players").LocalPlayer.Character)
if Part == v then

So basically it only detects if a certain side of the part is on screen or the entire part is on screen. But i want it to detect if any part of the part is onscreen. even if it’s only a single pixel of the part on screen.

you can try to check every corner of the block and see if they are all on screen

The method WorldToViewportPoint does not ask for the part, it asks for the position in the world. So therefore, “half” of the part would need to be visible to be considered onscreen. Why do you need to detect a part onscreen?

Because sometimes there might be something infront of it. And i just need to know how it can detect something on screen even if it’s just a single pixel of the part that’s onscreen.

Why do you need to detect a part onscreen?

U need to search Detect if part visible in first person view Old tread of mine

Because it needs a function to it


Like this. It needs to detect the part from behind anything even it’s just a little visible

That doesn’t explain why you need it. I’m asking why you need this because maybe there is an easier way to accomplish what you’re actually trying to do.

I now used a new way from @mymomtookmyps5away 's post and now have:

local _, withinScreenBounds = workspace.CurrentCamera:WorldToViewportPoint(v.Position)
		
   local BlockingParts = workspace.CurrentCamera:GetPartsObscuringTarget({v.Position}, {v})
if withinScreenBounds and #BlockingParts >= 0 then

But do u have any idea on how to make sure that #Part needs to have a specific name to unblock and ignore the rest of the blocking parts??

It would be easier if u would just specify one part or use a coma in the local part = workspace.Part1, workspace.Part2

Sorry for late reply.
Yea i understand but it has to be done with for i,v because otherwise it won’t work with my game

Does this work?

local _, withinScreenBounds = workspace.CurrentCamera:WorldToViewportPoint(v.Position)
local _, partSize = workspace.CurrentCamera:WorldToViewportPoint(v.Size)

if withinScreenBounds or partSize then
	local ray = Ray.new(workspace.CurrentCamera.CFrame.Position,(v.CFrame.Position - workspace.CurrentCamera.CFrame.Position))
	local Part = workspace:FindPartOnRay(ray,game:GetService("Players").LocalPlayer.Character)
	if Part == v then 
		-- Do Stuff Here
	end
end