Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Detecting GameObject in a view of a Camera

Discussion in 'Scripting' started by loock_, Aug 4, 2010.

  1. loock_

    loock_

    Joined:
    Aug 4, 2010
    Posts:
    18
    Hi,

    How can I detect whether a GameObject is seen by a Camera or being currently rendered on screen?
     
  2. AkilaeTribe

    AkilaeTribe

    Joined:
    Jul 4, 2010
    Posts:
    1,149
    http://unity3d.com/support/documentation/ScriptReference/Renderer.OnBecameVisible.html

    http://unity3d.com/support/documentation/ScriptReference/Renderer.OnBecameInvisible.html

    http://unity3d.com/support/documentation/ScriptReference/Renderer-isVisible.html

    About OnBecameVisible and OnBecameInvisible, there seems to be two tricks :
    - When you launch the game, if the gameobject is in the camera frustum (between the near and far clip), OnBecameVisible is triggered. The inverse is false : when you launch the game, if the gameObject is not in the camera frustum, OnBecameInvisible is not triggered.
    - Even if the gameObject is not visible (behind another gameObject), OnBecameVisible will be triggered anyway.
     
    vuong_vin likes this.
  3. loock_

    loock_

    Joined:
    Aug 4, 2010
    Posts:
    18
    Thanks, that was so simple ;)
     
  4. vuong_vin

    vuong_vin

    Joined:
    Sep 17, 2014
    Posts:
    18
    seem it simple, but if I want appear a popup when detected gameobject by camera, how can i do? Thanks Screen Shot 2014-09-17 at 2.54.38 PM.png Screen Shot 2014-09-17 at 2.53.48 PM.png
     
  5. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    238
    If you keep track of a true/false boolean variable, and change it to true when the object is visible, back to false when not, then in GUI do something like:

    //isVisible is the boolean variable
    if (isVisible) {
    //display dialog gui
    }

    Hope this makes sense and helps! :)
     
    vuong_vin likes this.