So I’ve got a turret and multiple enemies and I’d like to keep track of all enemies within the firing range. I’ve considered a couple ways to do this, one would be to just calculate the distance from each turret to each enemy at every step. Put that in an array for each turret I guess, then sort it. Seems a little expensive though, and maybe over complicated.
What I thought I could do instead was just attach a sensor object to each turret and then listen for a collision with the sensor. That’s fine, but I’m running into a problem: collisions only happen twice, once when the enemy enters the collision zone and once when it leaves. There doesn’t seem to be any way to track continuous collisions (no collisions occur inside the zone) and the collision on entering is the same as the collision on leaving (postCollision doesn’t fire at all in this case). So I could do something like add an enemy to the target array on the first collision (when they enter the zone) and remove them on the second collision (when they exit). Then calculate distances to each enemy in the target array, then sort them, etc.
The problem that I see with this method is basically just a matter of unpredictability - suppose an enemy spawned inside the zone, or suppose the enemy was killed and removed by another turret before they exited. There are a lot of potential situations like that which might result in odd behavior and the only resolution I see is be very careful and cross my fingers and hope that I don’t miss anything when I’m implementing my fixes for these problems.
Is there a better way to do this? What I’d really like is a sensor object that fires every tick for every object within range.
Side question: is there a good way to deal with line-of-sight in this scenario? It seems like a related problem. [import]uid: 106115 topic_id: 28401 reply_id: 328401[/import]
[import]uid: 52491 topic_id: 28401 reply_id: 114659[/import]