public static void RegisterEnemy(TreeEnemy enemy) { if (enemies == null) { enemies = new List <TreeEnemy>(); } enemies.Add(enemy); }
void HitEnemy(int xm, int ym) { TreeEnemy enemy = null; foreach (TreeEnemy e in enemies) { if (xm == -1 && e.transform.position.x < transform.position.x) { if (enemy == null || enemy.transform.position.x < e.transform.position.x) { enemy = e; } } else if (xm == 1 && e.transform.position.x > transform.position.x) { if (enemy == null || enemy.transform.position.x > e.transform.position.x) { enemy = e; } } else if (ym == -1 && e.transform.position.y < transform.position.y) { if (enemy == null || enemy.transform.position.y < e.transform.position.y) { enemy = e; } } else if (ym == 1 && e.transform.position.y > transform.position.y) { if (enemy == null || enemy.transform.position.y > e.transform.position.y) { enemy = e; } } } if (enemy == null) { return; // No enemy } if (Vector2.Distance(enemy.transform.position, transform.position) > enemyDistanceForSword) { return; // Too far away } Debug.Log("Hit enemy: " + enemy.name); }