Nearest enemy idea?

I have an idea on how to do this, but I’m not sure if this is the best way, also my idea is not very complete. I need to chose the nearest one out of 5 enemies. My idea is the following:

Have a variable nearestEnemyDist which by default would be huge, like 10000

Than cycle between the enemies and check if the distance to the enemy is smaller than nearestEnemyDist and if it is, asign it to nearestEnemyDist and start targeting the enemy. The problem is how would I set back nearestEnemyDist if the turret (yes, I’m scripting a turret) kills the enemy or the enemy gets killed?

Anyway, is this even the right way to do it? I mean, it will work for sure, but isn’t there an easier or more effective way ?

It depends, should the turret be able to ‘see’ all of the enemies all of the time?

For speed purposes, I’d use tags to tag all of the enemies with an ‘enemy’ tag. Then I’d get a list of all of those, and set that nearestEnemyDist variable to the first distance. Then I’d loop through the rest and find the next closest one, if it exists.
Then set the current enemy to the nearestEnemyDist one.

When it is killed, I’d run the above function again. Don’t run it continuously though, unless you have multiple things that can kill baddies. Or use a timer to decide when to run it. Don’t just run it on Update though, or you’ll affect performance.

-Chilton

Well, enemies are added to an array when they get near the turret and then removed from the array if they are away. I’m using a sphere collider to check that.
Now two questions: How would you be finding the nearest enemy in the loop? Will you use my method I mentioned above, or some other way?

And how will you check if the enemy is killed. My idea is to do a check for the enemy’s health in the for-loop like
if(enemy range < nearestEnemyDist and enemy.GetComponent(“script”).health > 0). Would that be a good idea?

It updates only if enemies are in the sphere collider and updates only the enemies in it.

If you’re doing tower defense or it doesn’t matter which of the nearest ones the turret shoots at try this:

  1. create a grid (an array is fine x,y)

  2. each enemy is responsible for updating its presence in the array and which cell its in

  3. the turret picks cells of interest ie its own cell and neighbors and simply picks one from there (first in the list is fine for tower defense).

It works better with thousands of mobs than distance checks :slight_smile:

Thats fine I guess. You can also have enemies do the checking instead if you wanted.

Yes, I’m doing a FPS + Tower Defence. I liked the idea with the grid, but I can’t imagine whow it works. How do I define those cells and everything ?

Okay, I couldn’t understand the grid thing pretty well, so I tried doing it my way. I got a question. My turret doesn’t yet compare the distances, it just pickes the first object that gets in range and that’s OK, I have a lot of work to do. But one basic thing I want about the turret doesn’t work. I want if an enemy goes behind a wall or something, so the turret cannot raycast to it, the turret to ignore it and pick another enemy if in range.

Here’s the turret’s code so far:
Enemy game objects are added to the enemies array with another script.

private var enemies = new Array();
private var currentTarget : GameObject;
private var updateTimer : float = 0.0;

function Update () {
for (i= 0; i < enemies.length; i++ ) {
var hit : RaycastHit;

//if there are enemies in the array and there's no current target, than try to get one.
if(enemies[i]  !currentTarget){
// if the raycast hit something and that something is an enemy, not a wall for instance...
if(Physics.Raycast(transform.position, enemies[i].transform.position, hit, 200, 9)  hit.transform.gameObject.tag == "Enemy") currentTarget = enemies[i];
}
}

//if we have a target...
if(currentTarget){
transform.LookAt(currentTarget.transform.position);
if(updateTimer < Time.time){
Physics.Raycast(transform.position, currentTarget.transform.position, hit, 200, 9);
//Check if the raycast hits something, that's not an enemy
if(hit.transform.gameObject != currentTarget) currentTarget = null;
updateTimer = Time.time + 0.5;
}
}


}

Now what happens is that I have to bring the enemy really, really close to the turret in order for the turret to see it. Raycasts are long enough, don’t know what’s the problem.

And one more thing: if an enemy hides behind something, so that the trasform is hidden, but it’s head for example is still visible, how would the turret know that the enemy is still attackable ?