Method FindNearestEnemy
- Namespace
- GrindFest
- Assembly
- GrindFest.dll
FindNearestEnemy(string, string, string, float, float)
Finds the nearest hostile enemy within range that the hero can reach.
public static MonsterBehaviour FindNearestEnemy(string name1 = "", string name2 = null, string name3 = null, float maxDistance = 15, float howNear = 2)
Parameters
name1
stringPrimary name to search for. Monster name must contain this string. If empty, matches any name.
name2
stringOptional secondary name to search for. If provided, monster name must contain either this or name1.
name3
stringOptional tertiary name to search for. If provided, monster name must contain either this, name2, or name1.
maxDistance
floatMaximum search distance from the hero. Enemies beyond this range will be ignored.
howNear
floatHow close the hero needs to be able get to the enemy. Most melee attacks require you to stand at distance 2. Used for pathfinding checks.
Returns
- MonsterBehaviour
The nearest reachable enemy, or null if no valid enemy is found.
Remarks
The method filters enemies based on several criteria:
- Must be alive (not dead)
- Must be within maxDistance
- Must be hostile to the hero
- Must be reachable via navigation system
- Must match one of the provided name patterns if specified
var enemy = Hero.FindNearestEnemy(); // Look for enemies within 15 units
if (enemy != null)
{
Hero.Say($"Found an enemy {enemy.name} at {Vector3.Distance(Hero.transform.position, enemy.transform.position)} units away");
}
// Hero.Attack only zombies within 20 units
var enemy = Hero.FindNearestEnemy("Zombie", maxDistance: 20);
if (enemy != null)
{
Hero.Attack(enemy);
}