52 lines
1.1 KiB
C#
52 lines
1.1 KiB
C#
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
|
|
public class FishAOEDetector : MonoBehaviour
|
|
{
|
|
[Header("AOE Settings")]
|
|
public float radius = 1.5f;
|
|
public float lifeTime = 0.5f;
|
|
public LayerMask fishLayer;
|
|
public int gunLevel;
|
|
|
|
[Header("Debug")]
|
|
public bool drawGizmos = false;
|
|
|
|
private HashSet<Fish> hitFishes = new HashSet<Fish>();
|
|
|
|
void Start()
|
|
{
|
|
Detect();
|
|
Destroy(gameObject, lifeTime);
|
|
}
|
|
|
|
void Detect()
|
|
{
|
|
Collider[] cols = Physics.OverlapSphere(
|
|
transform.position,
|
|
radius,
|
|
fishLayer
|
|
);
|
|
|
|
foreach (var col in cols)
|
|
{
|
|
Fish fish = col.GetComponentInParent<Fish>();
|
|
if (fish == null) continue;
|
|
|
|
// 防止重复命中
|
|
if (hitFishes.Contains(fish)) continue;
|
|
|
|
hitFishes.Add(fish);
|
|
fish.OnHit(gunLevel);
|
|
}
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
void OnDrawGizmosSelected()
|
|
{
|
|
if (!drawGizmos) return;
|
|
Gizmos.color = Color.red;
|
|
Gizmos.DrawWireSphere(transform.position, radius);
|
|
}
|
|
#endif
|
|
} |