Having tower shoot first enemy

  1. What do you want to achieve? Keep it simple and clear!
    Have my tower defense tower attack the enemy that is closest to the end and is in the tower’s range.
  2. What is the issue? Include screenshots / videos if possible!
    I don’t know how I would detect the enemy that is first, I would just like to be pointed in the correct direction.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Yes, and I have managed to detect the enemy closest to the end of the map, but I do not know how I would get this data across to the towers, Would a universal script for tower attacks be better in this situation?

Thankyou, I am quite new to the DevForums so sorry if this is too much to ask.

1 Like

I’d say the easiest solution would be to keep track of new enemies inside of an array and remove them once they are defeated or otherwise removed.

In that case, you can simply do array[1] to find the first enemy.

Another option would be to simply add a tag or some other identifier to each enemy that spawns using an every increasing +1 number. Then just find the lowest number among a given range.

This would work, but due to some enemies being faster than others, if one overtakes the other, the whole thing would be messed up.

In that case, I would create a whole system around enemy progression throughout what I assume is a track.

First method off the top of my head would be to compare checkpoints reached and the distance between the next checkpoint to see which is closer between checkpoints. For the most accuracy, checkpoints should be in a line and not across curves.

I’m sure there’s a more accurate method, but I can’t think of it atm.

1 Like

You can compare enemy distances easily by setting them an initial spawn time and their walkspeed .

2 Likes

That’s a good idea. All you’d need to know is the length of time it would take to reach the end. Nodes are another option for that.

Yup! I already have nodes inside of the game to direct the enemies, so it would fit nicely.

1 Like

Ngl you wouldn’t even need to know the length of time it takes to reach the end. The product of the spawn time and walkspeed tells everything on a scalar quantity.

I suppose you’re right, as long as you know how long said enemy has existed.

1 Like

Oh yeah I forgot since it’s tower range based you’d have to region3 all the enemies inside the zone or some other detection and compare all the enemies inside.

1 Like

That doesn’t really change that either method would work, so it’s all good.

1 Like

I will have to read up about this, but it sounds like a good idea. This is basically the thing i need to find out.

1 Like

Although now that I think about it, walkspeed might not be enough if something were to change the enemy’s speed down the line. You’d have to keep track of that unless you use nodes.

1 Like

In that case you’d have to manipulate a quadratic equation into that (if the walkspeed decreases/increases quadratically) which isn’t as hard as you think it is.

1 Like

Perhaps, but you’d still have to keep track of changes. Which it’s not like that would be very difficult, but I feel like nodes would be easier and perhaps more user friendly.

1 Like

Do you have any good links/resources where I could learn about region3?

1 Like