Game Development Reference
In-Depth Information
There are several important caveats worth noting with the events OnBecameVisible
and OnBecameInvisible . First, visibility here only means that an object has come
within the camera frustum; thus, it can still be occluded by other, nearer objects,
and so, it might not be truly visible at all. Second, the events pertain to all cameras
and not to specific cameras. OnBecameVisible is called once to tell you that the
object, while previously not visible, has now entered the frustum of at least one
camera. Likewise, OnBecameInvisible is called once and tells you that the object,
while previously visible, has now left the frustum of all cameras. Finally, and rather
unhelpfully, these functions also include the visibility of the scene camera. This
means that if you're testing your game with the Scene tab open and visible and the
object is visible to you in the Scene tab, this will count as being visible. In short, the
methods OnBecameVisible and OnBecameInvisible would be useful only if your
behavior depends on the total visibility or invisibility in the scene, where visibility
just corresponds to the frustum's presence. In other words, these events are a great
place to toggle behaviors such as AI behaviors that depend on visibility, for example,
NPC panic behaviors and other kinds of NPC-to-NPC interactions.
More information on the functions OnBecameVisible
and OnBecameInvisible can be found online in the
Unity documentation at http://docs.unity3d.com/
ScriptReference/MonoBehaviour.OnBecameVisible.
html and http://docs.unity3d.com/ScriptReference/
MonoBehaviour.OnBecameInvisible.html .
More on the object visibility
Another check that's important, besides testing when an object enters and leaves
camera visibility, is to test whether an object is visible right now to a specific
camera. Unlike OnBecameVisible and OnBecameInvisible , which were called on
a one-off basis when an object enters or leaves the frustum, this kind of test is about
the current state of an object that assumes no prior knowledge of it. To achieve this,
the OnWillRenderObject event can be used. This event is called continuously on an
object, once per frame for each camera to which it is visible as long as the object is
visible to that camera. "Visible" here is taken to mean "within the camera frustum".
Again, no occlusion testing is applied. Refer to the following code sample 5-3, and
notice that inside this event, the Camera.current member can be used to retrieve a
reference to the camera to which the object is currently visible, including the scene
view camera:
void OnWillRenderObject()
{
Debug.Log (Camera.current.name);
}
 
Search WWH ::




Custom Search