Targeting the enemy the furthest on the path

Hello, im making a tower defense however I cant figure out how to do the targeting. When using the fire bullet action with the distance between 2 objects condition a tower will target the closest enemy and I dont know how to change it to the enemy that has walked on the path the most.

I had an idea to make a variable that constantly adds to a number when an enemy is on the path and towers would target the enemy with the highest number. How would I make that? Is that even possible with multiple enemy types? Or is there a better way to do this?

Thank you for helping!

How are you currently do the targeting?

You could use the pick nearest object condition. If there are different enemy objects then you could put them into an object group and pick from the group. It doesn’t have to be the nearest object to the turret, it could be the nearest to say the entrance or a hidden object at the end of the path.

How are you moving the enemy? Pathfinding? Are the enemy objects being spawned or are they already all in the scene?

You could use a variable to store an incremental ID. Then you could target the object with the lowest ID using the for each instance event using the object variable, ascending with a limit of 1.

Here’s an example of both methods.

I’ve played with the tower defense genre. Firing through or over enemies towards another target can be a problem. You can use a tween instead of the fire behavior. Then only check for a collision between the bullet and an enemy when the tween is finished. You’d basically be targeting a position instead of an enemy. You could use predictive targeting to target a position ahead of the enemy.

You could use the fire behavior and link the bullet to the target but then it’s tough to apply damage to another object if the projectile misses it’s target.

With a larger projectile you could apply damage to all of the enemies within a certain range of the projectile. You could use the collision mask of the projectile or the distance from it. Using the distance, you could apply a damage amount based on proximity.

I played around with projective damage as well. When a target is fired at, you reduce a targeted object’s projected health using an object variable. That way you can skip the object if it’s predicted health is below zero. Why waste projectiles. If the projectile misses the target, you would need to add the projected damage back so it could be targeted again. You would apply the real damage when the projectile collides with the object.

If you think about it, the road may be curved, and the enemy is walking near the tower, but they are far away from it, and it will be counted as if they have almost reached the tower, and that’s why they’ll shoot him, but the one who’s a little further away from the tower but is almost there will be safe.

Hi thanks for the reply! The enemies move with pathfinding and are spawned into the scene. The first method checking which enemy is the closest to the exit would not work because the path is not a straight line, it has turns. Can you explain method 2 a bit more? Im not sure I completely understand. Thanks

Add an ID variable to the enemy objects put them into a group. Use the group name instead of the object name.

This assumes the enemies stay in formation and move at the same speed.