BloodAndGore/Assets/Scripts/Util.cs

21 lines
882 B
C#
Raw Normal View History

2019-08-03 18:14:47 +02:00
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;
}
}
}