How to target first enemy in td game

i have multiple enemies in my td game and i don’t know how to target the first enemy.

Hi, you could use the “distance between two objects” condition. You could also have a hidden collider-object that detects which enemy gets into the fire range of the tower.

1 Like

You could use the pick nearest object. I’m guessing TD is Tower Defense. If not ignore everything else I typed.

I haven’t played one in a while. I’d play or watch videos of some games and learn their different strategies.

I’m sure it’s a mix of proximity and strength of attacker and remaining health points. Equal troops might fight each other but you also need to focus on really powerful distant weapons. You would need to reassign troops as the enemy got closer.

What would be tougher would be keeping track of not just health of a target but expected health after damage. You wouldn’t want 30 units shooting at the same target if one shot is enough.

You could monitor the health using a predicted health object variable. Deduct damage from it as soon as a unit is shot at. Then base the need for any other attacks on the predicted health value instead of the actual health. The actual health would be calculated on collision along with adjusting the estimated value toward the real value.

1 Like

how do you think i could use that to target one enemy if i have multiple different ones in range of tower

I honestly don’t have the best strategy. At the simplest level, I’d use a for each object to go through the towers. But it’s going to involve a lot of logic.

All objects are draggable and I drew a line simple to make it more visible.

A more powerful logic would compare the strength of the shooter to the health or at least max health of the targets. If there’s moveable troops, they would override the other decisions when they reached a certain distance.

thx for the help i will figure something out for multiple enemies

I was really intrigued by this. I decided to keep trying to figure it out. This is far from done and may contain errors but the concepts are there. Click as fast as you want to fire once per tower. The tower will target the weakest. It should probably target the closest but I decided to target the weakest and they target in the same order.

Click to fire once from each tower. The enemies are draggable for debugging.

Project File
doug13579/Gdevelop-Tower-defense-example (github.com)

1 Like

thank you so much this is very helpful.

1 Like

It was a challenge but fun. I started with the bullet behavior but then switched to force but when I was trying to deal with missed shots, I decided to try tweens which works perfectly. Linking everything together is also a main ingredient. Linking worked great for making sure a projectile collides with its intended target.

I was going to use IDs but the health level is a perfect way to pick a target. I’m not sure about continually putting the health in an array. There might be another way to choose the weakest and strongest. Although, this organizes things. At minimum, I would only update the array when necessary.