范围检测

using UnityEngine;

/// 

/// 攻击范围检测

/// 

public class AttackCHeck : Singleton

{

/// 

/// 扇形范围检测

/// 

/// 自己的位置

/// 目标的位置

/// 攻击的半径

/// 攻击的角度

public static bool TheFanTest(Transform my, Transform target, float SkillDistance, float SkillJiaodu)

{

//与敌人的距离

float distance = Vector3.Distance(my.position, target.position);

//玩家正前方的向量

Vector3 norVec = my.rotation * Vector3.forward;

//玩家与敌人的方向向量

Vector3 temVec = target.position - my.position;

//求两个向量的夹角

float jiajiao = Mathf.Acos(Vector3.Dot(norVec.normalized, temVec.normalized)) * Mathf.Rad2Deg;

if (distance < SkillDistance)

{

if (jiajiao <= SkillJiaodu * 0.5f)

{

Debug.Log("在扇形范围内");

return true;

}

}

return false;

}

/// 

/// 长方形范围检测

/// 

/// 自己的位子

/// 目标的位置

/// 

public static bool RectangleDetection(Transform my, Transform target)

{

//计算玩家与敌人的距离

float distance = Vector3.Distance(my.position, target.position);

//玩家与敌人的方向向量

Vector3 temVec = target.position - my.position;

//与玩家正前方做点积

float forwardDistance = Vector3.Dot(temVec, my.forward.normalized);

if (forwardDistance > 0 && forwardDistance <= 10)

{

float rightDistance = Vector3.Dot(temVec, my.right.normalized);

if (Mathf.Abs(rightDistance) <= 3)

{

Debug.Log("进入攻击范围");

return true;

}

}

return false;

}

/// 

/// 半圆范围检测

/// 

/// 自己的位置

/// 目标的位置

/// 

public static bool SemicircleDetection(Transform my, Transform target)

{

//计算玩家与敌人的距离

float distance = Vector3.Distance(my.position, target.position);

//玩家与敌人的方向向量

Vector3 temVec = target.position - my.position;

//与玩家正前方做点积

float forwardDistance = Vector3.Dot(temVec, my.forward.normalized);

if (forwardDistance > 0 && forwardDistance <= 10)

{

if (distance <= 5)

{

Debug.Log("进入攻击范围");

return true;

}

}

return false;

}

/// 

/// 圆形范围检测

/// 

/// 自己的位置

/// 敌人的位置

/// 最小距离

/// 最大距离

/// 

public static bool CircleRange(Transform my, Transform target, float minDistance, float maxDistance)

{

float distance = Vector3.Distance(target.position, my.position);

if (distance <= maxDistance && distance >= minDistance)

{

return true;

}

else

{

return false;

}

}

/// 

/// 等腰三角形检测

/// 

/// 自己的位置

/// 目标的位置

/// 最大距离

/// 最大角度

/// 

public static bool TriangleRange(Transform my, Transform target, float maxDistance, float maxAngle)

{

Vector3 playerDir = my.forward;

Vector3 enemydir = (target.position - my.position).normalized;

float angle = Vector3.Angle(playerDir, enemydir);

if (angle > maxAngle * 0.5f)

{

return false;

}

float angleDistance = maxDistance * Mathf.Cos(maxAngle * 0.5f * Mathf.Deg2Rad) / Mathf.Cos(angle * Mathf.Deg2Rad);

float distance = Vector3.Distance(target.position, my.position);

if (distance <= angleDistance)

{

return true;

}

return false;

}

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容