//检测扇形角度 float distance = Vector3.Distance(target.transform.position, transform.position); Quaternion right = transform.rotation * Quaternion.AngleAxis(30, Vector3.up); Quaternion left = transform.rotation * Quaternion.AngleAxis(30, Vector3.down); Vector3 n = transform.position + (Vector3.forward * distance); Vector3 leftPoint = left * n; Vector3 rightPoint = right * n; Debug.DrawLine(transform.position, leftPoint, Color.red); Debug.DrawLine(transform.position, rightPoint, Color.red);
Vector3 targetDir = target.transform.position - transform.position;
Vector3 forward = transform.forward;
float angle = Vector3.Angle(targetDir, forward);
if (angle< 30.0f)
{
//攻击代码
}