21 lines
882 B
C#
21 lines
882 B
C#
using UnityEngine;
|
|
|
|
namespace Saltosion.OneWeapon {
|
|
public class Util {
|
|
public static T GetClosestTo<T>(Transform target, float radius) where T : MonoBehaviour {
|
|
Collider2D[] NearbyColliders = Physics2D.OverlapCircleAll(target.position, radius);
|
|
float LowestDistance = float.PositiveInfinity;
|
|
T FoundTarget = null;
|
|
foreach (Collider2D Collider in NearbyColliders) {
|
|
float Distance = (Collider.transform.position - target.position).magnitude;
|
|
T TargetComponent = Collider.GetComponent<T>();
|
|
if (TargetComponent != null && Distance < LowestDistance && Collider.gameObject != target.gameObject) {
|
|
LowestDistance = Distance;
|
|
FoundTarget = TargetComponent;
|
|
}
|
|
}
|
|
return FoundTarget;
|
|
}
|
|
}
|
|
}
|