Game Development Reference
In-Depth Information
//---------------------------------------------------------
//Function to determine whether an object is visible
public static bool IsVisible(Renderer Renderable, Camera Cam)
{
//If in frustrum then cast line
if(CamUtility.IsRendererInFrustum(Renderable, Cam))
return
//Is direct line between camera and object?
!Physics.Linecast(Renderable.transform.position,
Cam.transform.position);
return false; //No line found or not in frustum
}
//---------------------------------------------------------
Camera vision - front and back
In some games, such as RTS games or casual games, the camera horizon (or far
clipping plane) does not have so great a significance, because the camera always sees
everything that is in front of it. In these cases, when objects are outside the frustum,
they are only outside in the x and y planes but not in the local z axis; that is, the
hidden objects are only hidden because the camera is not directly looking at them.
However, when the camera is appropriately orientated, objects can never be too far
away in the distance to be seen beyond the far clipping plane. In situations like these,
visibility tests can often be reduced to faster and simpler orientation tests. Thus, the
question changes from, "Is the object within the frustum and not occluded?" to "Is the
object in front of the camera or is it behind?" Here, the answer we need is different;
the question is not one of visibility but of orientation, whether the camera and its
subject are so oriented that the subject is in front of the camera or behind it. To test
for this, the vector dot product can be used. The dot product accepts two vectors
as input and reduces them to a single dimensional, numerical value as output.
This value describes the angular relationship between the two input vectors. In the
following code sample 5-7, the CamFieldView class can be attached to a camera, and
it detects whether the camera can see a target object, that is, whether the target object
is within a limited field of view in front of the camera:
using UnityEngine;
using System.Collections;
//-------------------------------------------------
public class CamFieldView : MonoBehaviour
{
//-------------------------------------------------
//Field of view (degrees) in which can see in front of us
//Measure in degrees from forward vector (left or right)
 
Search WWH ::




Custom Search