判断对象是否在视野内,有两种方式:
第一种:
- 不设置固定的目标,
- 使用LayerMask,设置寻找对象的Layer,
- 使用Physics.OverlapSphere方法,以给定的位置为圆心,按照设定距离投射一个球体,返回与投射的球体相交的碰撞器。
第二种:
- 设置固定的目标,
- 计算当前对象与目标对象的距离direction.magnitude小于设定视野距离,
- 计算当前对象的位置到目标点的方向与当前对象的前进方向的夹角Vector3.Angle(direction, transform.forward)小于设定视野角度,
- 使用Physics.Linecast,如果有任何碰撞器与起始点与终点之间的射线相交,返回true。如果在视野内,返回目标对象。
using UnityEngine;
public class CanSeeObjectScript : MonoBehaviour
{
public Transform targetObject;
public Vector3 positionOffset;
public LayerMask objectLayerMask;
private Transform objectInSight;
public float fieldOfViewAngle;
public float viewDistance;
private void Update()
{
if (CanSeeObject())
{
Debug.Log("找到目标");
}
else
{
Debug.Log("没有找到目标");
}
}
private bool CanSeeObject()
{
if (targetObject == null)
{
// 如果目标对象为空,按照设定距离投射一个球体,检查在视野内与投射的球体相交的碰撞器
objectInSight = WithinSight(transform, positionOffset, fieldOfViewAngle, viewDistance, objectLayerMask);
}
else
{
// 如果目标对象不为空,则确定目标对象在视野内
objectInSight = WithinSight(transform, positionOffset, fieldOfViewAngle, viewDistance, targetObject);
}
if (objectInSight != null)
{
return true;
}
return false;
}
public static Transform WithinSight(Transform transform, Vector3 positionOffset, float fieldOfViewAngle, float viewDistance, LayerMask objectLayerMask)
{
Transform objectFound = null;
// Physics.OverlapSphere 检测场景中与投射的球体相交的碰撞器
var hitColliders = Physics.OverlapSphere(transform.position, viewDistance, objectLayerMask);
if (hitColliders != null)
{
float minAngle = Mathf.Infinity;
for (int i = 0; i < hitColliders.Length; ++i)
{
float angle;
Transform obj;
if ((obj = WithinSight(transform, positionOffset, fieldOfViewAngle, viewDistance, hitColliders[i].transform, false, out angle)) != null)
{
// 这个对象在视野内,并且角度小于任何其他的对象,设置位于当前对象最前面的对象到objectFound
if (angle < minAngle)
{
minAngle = angle;
objectFound = obj;
}
}
}
}
return objectFound;
}
public static Transform WithinSight(Transform transform, Vector3 positionOffset, float fieldOfViewAngle, float viewDistance, Transform targetObject)
{
float angle;
return WithinSight(transform, positionOffset, fieldOfViewAngle, viewDistance, targetObject, false, out angle);
}
private static Transform WithinSight(Transform transform, Vector3 positionOffset, float fieldOfViewAngle, float viewDistance, Transform targetObject, bool usePhysics2D, out float angle)
{
// 计算当前对象的位置到目标点的方向
var direction = targetObject.position - (transform.position + positionOffset);
// 计算当前对象的位置到目标点的方向与当前对象的前进方向的夹角
angle = Vector3.Angle(direction, transform.forward);
if (direction.magnitude < viewDistance && angle < fieldOfViewAngle * 0.5f)
{
// 目标对象需要在当前对象的视野内
if (LineOfSight(transform, positionOffset, targetObject, usePhysics2D) != null)
{
//如果在视野内,返回目标对象
return targetObject;
}
else if (targetObject.GetComponent<Collider>() == null)
{
// 如果linecast没有碰撞到任何对象,并且目标对象没有collider,没有什么阻碍
if (targetObject.gameObject.activeSelf)
return targetObject;
}
}
// 如果目标对象没有在视野内,返回空
return null;
}
public static Transform LineOfSight(Transform transform, Vector3 positionOffset, Transform targetObject, bool usePhysics2D)
{
RaycastHit hit;
// transform.TransformPoint方法,在原来位置的基础上加上偏移位置
// Physics.Linecast 如果有任何碰撞器与起始点与终点之间的射线相交,返回true
if (Physics.Linecast(transform.TransformPoint(positionOffset), targetObject.position, out hit))
{
if (hit.transform.Equals(targetObject))
{
//如果在视野内,返回目标对象
return targetObject;
}
}
return null;
}
}
标签:positionOffset,对象,float,transform,Transform,Unity,视野,targetObject From: https://blog.51cto.com/u_6871414/5896973