1. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  2. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  3. We’re giving 2017.1 beta testers a chance to win t-shirts and a Nintendo Switch. Read more on the blog.
    Dismiss Notice
  4. Unity 2017.1 beta is now available for download.
    Dismiss Notice
  5. Unity 5.6 is now released.
    Dismiss Notice
  6. Get further faster with the Unity Plus Accelerator Pack, free for new Unity Plus subscribers for a limited time. Click here for more details.
    Dismiss Notice
  7. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  8. Learn how you'll soon be able to publish your games to China in four simple steps with Xiaomi. Sign up now for early access.
    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

    Graphics Plumber Unity Technologies

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

    Graphics Plumber Unity Technologies

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