Game Object visible by camera?

Discussion in 'Unity Support' started by ratamorph, Oct 19, 2007.

  1. ratamorph

    ratamorph

    Member

    Joined:
    Sep 2, 2007
    Messages:
    451
    Forgive me if this is trivial... I need a function to test if a given Game Object is currently visible by the camera.
  2. Aras

    Aras

    Graphics Plumber Unity Technologies

    Joined:
    Nov 7, 2005
    Messages:
    4,167
    There are several ways to do it.

    OnBecameVisible and OnBecameInvisible functions in any script are called when game object becomes visible/invisible to any camera.

    OnWillRenderObject (new in Unity 2.0) is called on any script when object will be rendered this frame for some camera. Inside this function, Camera.current is the camera that will render the object. If object will be rendered by several cameras, OnWillRenderObject will be called multiple times.

    And finally, GeometryUtility class allows you to test visibility manually (by doing bounding box vs. camera frustum intersection test).
  3. ratamorph

    ratamorph

    Member

    Joined:
    Sep 2, 2007
    Messages:
    451
    Thanks that works pretty well, what if I want to test if an object is occluted?
    Seems like the OnBecameVisible and OnBecameInvisible ignore occlusion.
  4. Aras

    Aras

    Graphics Plumber Unity Technologies

    Joined:
    Nov 7, 2005
    Messages:
    4,167
    Unity does not do occlusion culling, i.e. it renders everything in camera's frustum. Of course there's nothing stopping you from doing some sort of occlusion culling yourself, targeted at your specific game.

    But if you want to test occlusion, you'll have to do it yourself somehow. Occlusion is a complex topic though...