当前位置:   article > 正文

Unity判断某个物体是否在某个规定的区域_unity判断物体在范围内

unity判断物体在范围内

Unity自带的两种写法:
①物体的位置是否在某个区域内
Vector3 pos = someRenderer.transform.position;
Bounds bounds = myBoxCollider.bounds;
bool rendererIsInsideTheBox = bounds.Contains(pos);
②物体的矩形与区域的矩形是否交叉
Bounds rendererBounds = someRenderer.bounds;
Bounds colliderBounds = myBoxCollider.bounds;
bool rendererIsInsideBox = colliderBounds.Intersects(rendererBounds);

此处为转载:https://blog.csdn.net/Htlas/article/details/80084598
Unity的矩形、扇形、圆形监测
①矩形监测:
在这里插入图片描述
///
/// 矩形攻击范围
///
/// 攻击方
/// 被攻击方
/// 矩形前方距离
/// 矩形宽度/2
///
public bool RectAttackJudge( Transform attacker , Transform attacked ,float forwardDistance ,float rightDistance)
{
Vector3 deltaA = attacked.position - attacker.position;

     float   forwardDotA= Vector3.Dot(attacker.forward, deltaA);
    
     if (forwardDotA > 0 && forwardDotA <= forwardDistance)
     {
         if (Mathf.Abs(Vector3.Dot(attacker.right, deltaA)) < rightDistance)
         {
             return true;
         }
     }
     return false;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

②扇形监测
在这里插入图片描述
///
/// 伞形攻击范围
///
/// 攻击方
/// 被攻击方
/// 伞形角度
/// 伞形半径
///
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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

③圆形监测
在这里插入图片描述
///
/// 不定点式圆形攻击
///
/// 被攻击方
/// 技能释放位置
/// 半径
///
public bool CircleAttack(Transform attacked,Transform skillPosition, float radius)
{
float distance = Vector3.Distance(attacked.position, skillPosition.position);
if (distance < radius)
{
return true;
}
return false;
}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/390828
推荐阅读
相关标签
  

闽ICP备14008679号