赞
踩
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;
}
②扇形监测
///
/// 伞形攻击范围
///
/// 攻击方
/// 被攻击方
/// 伞形角度
/// 伞形半径
///
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;
}
③圆形监测
///
/// 不定点式圆形攻击
///
/// 被攻击方
/// 技能释放位置
/// 半径
///
public bool CircleAttack(Transform attacked,Transform skillPosition, float radius)
{
float distance = Vector3.Distance(attacked.position, skillPosition.position);
if (distance < radius)
{
return true;
}
return false;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。