BloodAndGore/Assets/Scripts/AI/Triggers/EnemySighted.cs

25 lines
1004 B
C#

using UnityEngine;
namespace Saltosion.OneWeapon.AI.Triggers {
public class EnemySighted : Trigger {
public float Radius;
public Enemy ClosestEnemy { get; private set; }
public override bool IsTriggered(Enemy subject) {
Collider2D[] NearbyEnemies = Physics2D.OverlapCircleAll(subject.transform.position, Radius);
float LowestDistance = float.PositiveInfinity;
bool FoundEnemy = false;
foreach (Collider2D Enemy in NearbyEnemies) {
float Distance = (Enemy.transform.position - subject.transform.position).magnitude;
Enemy EnemyComponent = Enemy.GetComponent<Enemy>();
if (EnemyComponent != null && Distance < LowestDistance && Enemy.gameObject != gameObject) {
LowestDistance = Distance;
ClosestEnemy = EnemyComponent;
FoundEnemy = true;
}
}
return FoundEnemy;
}
}
}