- Home /
renderer.is(Completely)Visible?
I'm trying to make a camera that follows two players which zooms in and out. It zooms in when both characters are visible and zooms out when one or more aren't visible. I used renderer.isVisible
to approach this and it works well; but it's not entirely what I want. A character that is completely off of the screen except for a single pixel will register as "visible." Is there a way that I can only call a character visible if they are entirely on the screen?
Please, no links to the documentary or previously asked questions. I don't even necessarily need code (although that would be nice!) but just an idea or a good way to approach this. Thanks in advance!
Answer by Dave-Carlile · Aug 28, 2015 at 08:24 PM
A potential way to do this would be to use Camera.WorldToScreenPoint on several vertices that bound your object (you can probably calculate these vertices using renderer.bounds). If all of the vertices are within the screen bounds then the object is entirely on the screen.
There is also GeometryUtility.TestPlanesAABB which may eliminate some of that work. You can pass Renderer.bounds along with the frustum planes calculated by GeometryUtility.CalculateFrustumPlanes. But the documentation isn't entirely clear if it returns true only if the bounds are entirely within/touching the planes, or if it would return true if it's partially within, so you'd need to test that.
I believe renderer.isVisible will give you exactly the same results as GeometryUtility.TestPlanesAABB with Renderer.bounds. The only big difference is that isVisible will check all active cameras, while GeometryUtility lets you pick.
Haven't tested it though, but I'd be supprised if that wasn't the case.
Your answer
Follow this Question
Related Questions
C# Renderer.IsVisible counts when editor camera sees object 0 Answers
How can I show split screen or multiple camera views at the same time? 1 Answer
Hiding model for local player 1 Answer
GameObject invisible at certain camera angles 0 Answers
Is there a shader in Unity that prevents transparent parts from overlapping? 1 Answer