Unity3D编程学习 小知识_扇形攻击_2018Oct

当需要判断一物体是否位于当前物体前方扇形范围内时  运用距离差和角度差实现判断

//扇形攻击 实现类型_1
public bool UmbrellaAttact( Transform attacker ,Transform attacked ,float angle, float radius)
{

   Vector3 deltaA = attacked.position - attacker.position;

   float tmpAngle = Mathf.Acos(Vector3.Dot(deltaA.normalized, attacker.forward)) * Mathf.Rad2Deg;

   if (tmpAngle < angle * 0.5f && deltaA.magnitude < radius)
   {
     return true;
   }
   return false;
}

//扇形攻击 实现类型_2
//距离差为半径 角度差为扇形角度的一半
Transform Target;

float _Dis = Vector3.Distance(Target.position, transform.position);
float _Angle = Vector3.Angle(Target.position-transform.position,transform.forward)

if (_Angle < x && _Dis < y)
{
   Debug.logFormat("在范围内");
}

原文地址:https://www.cnblogs.com/RainPaint/p/9829505.html