Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Game Object visible by camera?

Discussion in 'Editor & General Support' started by ratamorph, Oct 19, 2007.

  1. ratamorph

    ratamorph

    Joined:
    Sep 2, 2007
    Posts:
    458
    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

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    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

    Joined:
    Sep 2, 2007
    Posts:
    458
    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

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    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...