????????假设人物A向正前方释放一个技能,攻击范围为一个扇形,如何判断人物B是否在该范围内受到攻击。
? ? ? ? 1. 向量A的正前方forward 与 A指向B的向量direction 的角度offsetAngle小于扇形角度的1/2
? ? ? ? 2. 向量direction的模长magnitude,即A到B的距离小于半径
????????满足上面这两个条件即可判断B在扇形区域内,涉及的运算:点乘积、反余弦
/// <summary>
/// 判断target是否在扇形区域内
/// </summary>
/// <param name="sectorAngle">扇形角度</param>
/// <param name="sectorRadius">扇形半径</param>
/// <param name="attacker">攻击者的transform信息</param>
/// <param name="target">目标</param>
/// <returns>目标target在扇形区域内返回true 否则返回false</returns>
public bool IsInRange(float sectorAngle, float sectorRadius, Transform attacker, Transform target)
{
//攻击者位置指向目标位置的向量
Vector3 direction = target.position - attacker.position;
//点乘积结果
float dot = Vector3.Dot(direction.normalized, transform.forward);
//反余弦计算角度
float offsetAngle = Mathf.Acos(dot) * Mathf.Rad2Deg; //弧度转度
return offsetAngle < sectorAngle * .5f && direction.magnitude < sectorRadius;
}
扇形范围由扇形的角度和半径构成,定义变量:
//扇形角度
[SerializeField] private float angle = 80f;
//扇形半径
[SerializeField] private float radius = 3.5f;
//物体B
[SerializeField] private Transform b;
?定义bool类型变量flag,用来记录判断结果:
private bool flag;
private void Update()
{
flag = IsInRange(angle, radius, transform, b);
}
完整测试脚本:
using UnityEngine;
using UnityEditor;
public class Foo : MonoBehaviour
{
//扇形角度
[SerializeField] private float angle = 80f;
//扇形半径
[SerializeField] private float radius = 3.5f;
//物体B
[SerializeField] private Transform b;
private bool flag;
private void Update()
{
flag = IsInRange(angle, radius, transform, b);
}
/// <summary>
/// 判断target是否在扇形区域内
/// </summary>
/// <param name="sectorAngle">扇形角度</param>
/// <param name="sectorRadius">扇形半径</param>
/// <param name="attacker">攻击者的transform信息</param>
/// <param name="target">目标</param>
/// <returns>目标target在扇形区域内返回true 否则返回false</returns>
public bool IsInRange(float sectorAngle, float sectorRadius, Transform attacker, Transform target)
{
//攻击者位置指向目标位置的向量
Vector3 direction = target.position - attacker.position;
//点乘积结果
float dot = Vector3.Dot(direction.normalized, transform.forward);
//反余弦计算角度
float offsetAngle = Mathf.Acos(dot) * Mathf.Rad2Deg;
return offsetAngle < sectorAngle * .5f && direction.magnitude < sectorRadius;
}
private void OnDrawGizmos()
{
Handles.color = flag ? Color.cyan : Color.red;
float x = radius * Mathf.Sin(angle / 2f * Mathf.Deg2Rad);
float y = Mathf.Sqrt(Mathf.Pow(radius, 2f) - Mathf.Pow(x, 2f));
Vector3 a = new Vector3(transform.position.x - x, 0f, transform.position.z + y);
Vector3 b = new Vector3(transform.position.x + x, 0f, transform.position.z + y);
Handles.DrawLine(transform.position, a);
Handles.DrawLine(transform.position, b);
float half = angle / 2;
for (int i = 0; i < half; i++)
{
x = radius * Mathf.Sin((half - i) * Mathf.Deg2Rad);
y = Mathf.Sqrt(Mathf.Pow(radius, 2f) - Mathf.Pow(x, 2f));
a = new Vector3(transform.position.x - x, 0f, transform.position.z + y);
x = radius * Mathf.Sin((half - i - 1) * Mathf.Deg2Rad);
y = Mathf.Sqrt(Mathf.Pow(radius, 2f) - Mathf.Pow(x, 2f));
b = new Vector3(transform.position.x - x, 0f, transform.position.z + y);
Handles.DrawLine(a, b);
}
for (int i = 0; i < half; i++)
{
x = radius * Mathf.Sin((half - i) * Mathf.Deg2Rad);
y = Mathf.Sqrt(Mathf.Pow(radius, 2f) - Mathf.Pow(x, 2f));
a = new Vector3(transform.position.x + x, 0f, transform.position.z + y);
x = radius * Mathf.Sin((half - i - 1) * Mathf.Deg2Rad);
y = Mathf.Sqrt(Mathf.Pow(radius, 2f) - Mathf.Pow(x, 2f));
b = new Vector3(transform.position.x + x, 0f, transform.position.z + y);
Handles.DrawLine(a, b);
}
}
}
?
???欢迎关注公众号 “当代野生程序猿”
|