Unity相关代码
public class FindEnemy2 : MonoBehaviour
{
public Transform A;
public Transform B;
// Start is called before the first frame update
void Start()
{
}
private float dotResult;
private Vector3 crossResult;
// Update is called once per frame
void Update()
{
dotResult = Vector3.Dot(A.forward, B.position - A.position);
crossResult = Vector3.Cross(A.forward, B.position - A.position);
//判断前后
if(dotResult>=0)
{
//右侧
if(crossResult.y>=0)
{
Debug.Log("右前");
}
//左侧
else
{
Debug.Log("左前");
}
}
else
{
//右侧
if(crossResult.y>=0)
{
Debug.Log("后右");
}
//左侧
else
{
Debug.Log("后左");
}
}
if(Vector3.Distance(A.position,B.position)<=5)
{
if(crossResult.y>=0&&Vector3.Angle(A.forward,B.position-A.position)<=30)
{
Debug.Log("右侧30度内");
Debug.Log("发现入侵者");
}
else if(crossResult.y<0&&Vector3.Angle(A.forward,B.position-A.position)<=20)
{
Debug.Log("左侧20度以内");
Debug.Log("发现入侵者");
}
}
}
}