using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.EditorTools;
using UnityEngine;
using UnityEngine.Rendering;
public class NewBehaviourScript :MonoBehaviour
{
public float radius = 5;//半径
public float angle = 90;//角度
private void OnDrawGizmosSelected()
{
// 绘制扇形区域
Vector3 pos = transform.position;
float halfAngle = angle / 2.0f;
Vector3 front = Quaternion.Euler(0, -halfAngle, 0) * transform.forward;
Gizmos.DrawLine(pos, pos + front * radius);
for (int i = 1; i <= 32; i++)
{
Vector3 side = Quaternion.Euler(0, angle / 32.0f * i - halfAngle, 0) * transform.forward;
Gizmos.DrawLine(pos, pos + side * radius);
}
Gizmos.DrawLine(pos, pos + Quaternion.Euler(0, halfAngle, 0) * transform.forward * radius);
// 检测范围内的敌人
Collider[] colliders = Physics.OverlapSphere(pos, radius);
foreach (Collider collider in colliders)
{
if (collider.CompareTag("Enemy"))
{
Vector3 direction = collider.transform.position - pos;
float angleDiff = Vector3.Angle(transform.forward, direction);
if (angleDiff < angle / 2.0f)
{
Debug.Log("在范围内 :" + collider.gameObject.name);
}
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class Shanxing : MonoBehaviour
{
// 扇形中心点位置
Vector3 center;
// 扇形半径
float radius;
// 扇形方向
Vector3 direction;
// 扇形角度,单位为度
[Range(0f, 360f)]
public float angle;
private void OnDrawGizmos()
{
center = transform.position;
radius = 2;
direction = transform.forward;
angle = 90;
// 计算扇形对应的角度范围
float halfAngle = angle / 2;
Quaternion rotation = Quaternion.LookRotation(direction);//看向的方向
Vector3 left = rotation * Quaternion.AngleAxis(-halfAngle, Vector3.up) * (Vector3.forward * radius);
Vector3 right = rotation * Quaternion.AngleAxis(halfAngle, Vector3.up) * Vector3.forward * radius;
// 绘制扇形
Gizmos.color = Color.yellow;
Gizmos.DrawLine(center, center + left); // 左边界线
Gizmos.DrawLine(center, center + right); // 右边界线
Handles.color = Color.yellow;
Handles.DrawWireArc(center, Vector3.up, left, angle, radius); // 绘制扇形弧度
// 球形检测范围内的敌人
Collider[] colliders = Physics.OverlapSphere(center, radius);
foreach (Collider collider in colliders)
{
if (collider.CompareTag("Enemy"))
{
Vector3 direction = collider.transform.position - center;
float angleDiff = Vector3.Angle(transform.forward, direction);
if (angleDiff < halfAngle)
{
Debug.Log("在范围内 :" + collider.gameObject.name);
}
Handles.color = Color.blue;
Gizmos.color = Color.green;
Gizmos.DrawLine(center, center + left); // 左边界线
Gizmos.DrawLine(center, center + right); // 右边界线
Handles.DrawWireArc(center, Vector3.up, left, angle, radius);
}
}
}
}
第二种加了碰到敌人可以变颜色!