现在我们有一个函数告诉我们一个特定的对象是否在人工智能的视线内,我只是测试每个对象,由一个相机位置为原点,以相机远平面的距离为半径进行重叠范围检查。我为每个对象调用之前的SeeGameObject函数并将在视线内的对象保存在一个数组中。 public bool SeeGameObject(GameObject go)
{
// if object has a renderer and visible by any camera and is in this camera frustum
else // Only if gameobject has no mesh (= almost never) (Very approximately checking points using the renderer bounds and not the mesh bounds)
{
gobounds = new Vector3[9]{
Vector3.zero,
new Vector3(extents.x,extents.y,extents.z)*coefreduc,
new Vector3(extents.x,extents.y,-extents.z)*coefreduc,
new Vector3(extents.x,-extents.y,extents.z)*coefreduc,
new Vector3(extents.x,-extents.y,-extents.z)*coefreduc,
new Vector3(-extents.x,extents.y,extents.z)*coefreduc,
new Vector3(-extents.x,extents.y,-extents.z)*coefreduc,
new Vector3(-extents.x,-extents.y,extents.z)*coefreduc,
new Vector3(-extents.x,-extents.y,-extents.z)*coefreduc
};
}
foreach(Vector3 v in gobounds)
{
// test if it can see gameobject
if(GeometryUtility.TestPlanesAABB(GeometryUtility.CalculateFrustumPlanes(_camera), new Bounds(v+center, Vector3.zero)) // if point in viewing frustrum
&& (!Physics.Linecast(transform.position, v+center, out hitInfo) || hitInfo.collider.gameObject == go )) // if nothing between viewing position and point