Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

QUICK QUESTION: How do you tell when something is inside of a player's view?

I want to be able to tell if a certain object is in the view of a player's screen. Obviously I could use Mouse.Target, but that would only work if the player looks directly at the object. I have absolutely no background knowledge on how to do this. I'm not sure if this is complicated as writing 4 different math equations, or if it's as easy as typing 3 lines, but it would help if someone pointed me in the right direction.

4 comments

1

Something in the Camera could help, like a method

0

Oh, here we go, I found something.

0

"WorldToScreenPoint: Takes a 3D world point and returns information regarding its 2D screen location, its depth, and whether it is visible on the screen or not.". Thanks for the suggestion.

0

check out my answer below v

1 answer

✓ Accepted answer

You can use Camera:WorldToScreenPoint() to see if a point is in front of your camera. Given that, we can then use Camera:GetPartsObscuringTarget() to see if any objects obscure the point.

local screenPos, inFront = camera:WorldToScreenPoint(part.Position)

-- Check screen bounds
if inFront and screenPos.X >= 0 and screenPos.Y >= 0 and screen.Pos.X <= mouse.ViewSizeX and screen.Pos.Y <= mouse.ViewSizeX then

	local obstacles = camera:GetPartsObscuringTarget({part.Position}, {})

	if obstacles == {} then
		print("Success!")
	else
		print("Object blocked")
	end
end

3 comments

1

thebayou!! I SEE YOU PLAYING POLYGUNS WITH ME XD

0

nice but you didn't have to comment to make me check your answer

0

lol sry just didn't want to be ninja'd by a comment

This discussion is preserved as it was. New answers, comments, and votes are closed.