public class CollisionDetectionFunc
{
//normalized 规范化 Mathf.Acos 通过弧度求夹角 Mathf.Rad2Deg弧度到度的转化常亮
/// <summary>
/// 矩形攻击
/// </summary>
/// <param name="atker">攻击者</param>
/// <param name="atked">被攻击者</param>
/// <param name="width">矩形宽度</param>
/// <param name="height">矩形长度</param>
/// <returns></returns>
public static bool RectAtk(Transform atker, Transform atked, int width, int height)
{
//求出攻击者指向被攻击者的向量deltaVector
Vector3 deltaVector = atked.position - atker.position;
//向量deltaVector 和 攻击者的正方向单位向量的点乘
float dotForword = Vector3.Dot(deltaVector.normalized, atker.forward);
//是否在前方height(长度)处以内
if (dotForword > 0 && dotForword < height)
{
//是否在左右width以内
float dotRight = Vector3.Dot(deltaVector,atker.right);
if (Mathf.Abs(dotRight) < width)
return true;
}
return false;
}
/// <summary>
/// 扇形攻击
/// </summary>
/// <param name="atker">攻击者</param>
/// <param name="atked">被攻击者</param>
/// <param name="angle">攻击范围角度</param>
/// <param name="radius">攻击半径</param>
/// <returns></returns>
public static bool SectorAtk(Transform atker, Transform atked,float angle,float radius)
{
//求出攻击者指向被攻击者的向量deltaVector
Vector3 deltaVector = atked.position - atker.position;
//向量deltaVector 和 攻击者的正方向单位向量的点乘
float dotForword = Vector3.Dot(deltaVector.normalized, atker.forward);
//求出角度
float tempAngle = Mathf.Acos(dotForword) * Mathf.Rad2Deg;
//判断角度是否在攻击范围角度内并且向量的长度是否小于攻击者的攻击半径
if (tempAngle < angle * 0.5f && deltaVector.magnitude < radius)
return true;
return false;
}
}