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.
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