Hi,
I’m making a tower defense framework. I’m currently working on the targetting system - specifically, the “first” target. I know that this has been asked many times, and I’ve looked through most of them, but I’m looking specifically for the most efficient one that doesn’t strain the server / cause server lag.
This means that it will shoot the target that has travelled the furthest, that is still in its range. The range part is quite easy (just a simple magnitude check from the HRP), but the target that has travelled the furthest is a bit more difficult.
My general idea is that each enemy has a “progress” NumberValue. This will tell you how far the enemy has travelled. It is calculated like so:
The path that the enemies take is dictated by several waypoints. current_waypoint is the most recent waypoint that the enemy has encountered. distance_to_next_waypoint is the distance from the HRP to the next waypoint that the enemy will take. total_distance_to_next_waypoint is the distance from current_waypoint’s position to the next waypoint’s position.
Therefore, the “first” enemy is the one with the highest progress value.
There are only 2 questions that I have:
Is this the most efficient way to do this?
How should I go about updating the progress value without straining the server? (i.e. should I tie it to a Heartbeat event? Iterate though every single enemy? A mixture of both?)
If I were you, I’d add up the distances of all the waypoints to get the total length of the path. Then, knowing how long each waypoint segment is of the path, I add them up until I get to the enemy position, where I would then do:
Neither. You’d determine if the enemy dies ahead of time, and where the enemy would be at that time. You’d tell all the clients to visualize the enemy and that the enemy would die at that position.
In other words, you’d perform all the calculations in one frame and visualize them on the client.
Not really. You should use arrays instead since it’s much faster than having to navigate through all the instances.
I know I’m a bit late here, but don’t you think you are over complicating things? A very easy way to target the first enemy is to just make a table, and then whenever an enemy comes in range, you add him into the table, and once he leaves the range, you remove him from the table. Thus, if you want to target the first zombie, just attack the first zombie on the table, and if you want the last zombies just attack the last one on the table, it’s quite simple really.
The problem with this method is that not all zombies have the same speed. Thus some zombies could overtake others and the first zombie is no longer actually the first one. Don’t worry, though, I found a workaround that utilised what SubtotalAnt8185 said, and finished the demo.