Search Unity

Find out if object is visible on screen

Discussion in 'Scripting' started by Elisabeth123, Apr 19, 2017.

  1. Elisabeth123

    Elisabeth123

    Joined:
    Feb 17, 2016
    Posts:
    13
    Hi!

    I have a tricky question. I have an empty game object with a box collider around and I am drawing a rectange around it (the borders are the points of the bounding box of the box colider). Now I would like to only draw it if the game object is visible. So when another game object is in front of my empty game objects with the box collider I don't want to draw the rectangle. Now my question is: How can I find out if another object is in front. (The other objects have no bounding boxes). Is there even a possiblity to find that out if the other objects have no bounding box?

    Thanks a lot in advance.
     
  2. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    916
    You can use Monobehaviour.OnBecameVisible and OnBecameInvisible, but that would require that the object has a Renderer attached. If the blocking objects were static occluders then you could instead resort to using Unity's CullingGroup API. which will work give you hooks when the cullsphere is occluded by the camera's fulstrum or static Occluders (as marked in the static button in the inspector) but will notcalculate against dynamic occluders (occluders that can move). Since theres not a whole lot of code on the web showing how to use Unity CullingGroups I can provide code classes that I've written for it.

    otherwise you'll be better off adding a render to your objects and making sure it has the same bounds as that box collider