纳金网

标题: 矩形和扇形攻击的代码 [打印本页]

作者: may    时间: 2019-12-24 19:52
标题: 矩形和扇形攻击的代码
布尔凯索的分享
  1. /// <summary>
  2.     /// 矩形攻击
  3.     /// 判断npc是否在player的攻击距离内
  4.     /// </summary>
  5.     /// <param name="player">player</param>
  6.     /// <param name="npc">npc</param>
  7.     /// <param name="width">攻击区域的宽</param>
  8.     /// <param name="height">攻击区域的高</param>
  9.     /// <returns></returns>
  10.     public bool ReckAtc(Transform player,Transform npc,int width,int height)
  11.     {
  12.         Vector3 deltaVect = npc.position - player.position;
  13.         //点乘得到是否在前方,如果大于0,就是在前方。
  14.         //同时的到的也是npc位于player前方的相对位置。
  15.         float aDotForward = Vector3.Dot(deltaVect, player.forward);
  16.         //npc位于player前方的相对位置<height,代表在攻击距离内
  17.         if (aDotForward > 0&&aDotForward<height)
  18.         {
  19.             float aDotRight = Vector3.Dot(deltaVect, player.right);
  20.             if (Mathf.Abs(aDotRight) < width)
  21.             {
  22.                 return true;
  23.             }           
  24.         }
  25.         return false;
  26.     }
  27. -----------------------------------------------------------------
  28.     /// <summary>
  29.     /// 判断是否在三型区域伞形区域
  30.     /// </summary>
  31.     /// <param name="player"></param>
  32.     /// <param name="npc"></param>
  33.     /// <param name="angle">给定的角度,不能超过这个角度的一半</param>
  34.     /// <param name="radius">给定的伞形半径</param>
  35.     /// <returns></returns>
  36.     public bool UmbrellaAtc(Transform player,Transform npc,float angle,int radius)
  37.     {
  38.         //player指向npc的向量
  39.         Vector3 deltaVect = npc.position - player.position;
  40.         //拿到两个向量间的夹角
  41.         float tmpAngle = Vector3.Angle(deltaVect,player.forward);
  42.         //拿到a向量在forward的膜
  43.         float aDotForward = Vector3.Dot(deltaVect, player.forward);
  44.         //判断如果两个向量间的夹角小于给定角度的一般并且a向量的膜小于伞形的半径return true否则false
  45.         if (tmpAngle < (angle / 2) && aDotForward < radius)
  46.         {
  47.             return true;
  48.         }
  49.         return false;
  50.     }
复制代码

作者: 毛毛虫    时间: 2019-12-26 16:26
谢谢分享,学习一下




欢迎光临 纳金网 (http://www.narkii.com/club/) Powered by Discuz! X2.5