using UnityEngine;
public class Shan : MonoBehaviour
{
public float radius = 5.0f; // 扇形的半径
public float angle = 30.0f; // 扇形的角度
void Update()
{
transform.Rotate(Vector3.up * 100 * Time.deltaTime * Input.GetAxis("Horizontal"));
// 执行扇形检测
DetectTargetsInSector();
}
void DetectTargetsInSector()
{
// 获取所有目标物体(假设目标物体有Tag "Target")
GameObject[] targets = GameObject.FindGameObjectsWithTag("Target");
foreach (GameObject target in targets)
{
Vector3 directionToTarget = target.transform.position - transform.position;
float distanceToTarget = directionToTarget.magnitude;
// 检查距离是否在半径范围内
if (distanceToTarget <= radius)
{
// 计算与前方的夹角
float angleToTarget = Vector3.Angle(transform.forward, directionToTarget);
// 检查是否在角度范围内
if (angleToTarget <= angle / 2)
{
Debug.Log("Target in sector: " + target.name);
// 在这里执行目标在扇形范围内的逻辑
}
}
}
}
}
10-20
09-04
632
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
11-25
1343
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
04-04