Evenly distribute/split homing projectiles between multiple enemies

Sorted by distance array solves distribution, for any number of projectiles you want shoot and any number of targets. So you can have multiple turrets shooting multiple targets. However that would need some more thinking about how to make distance function.

Let me explain (assuming single turret, that shoots 6 projectiles).

  • first it sorts enemy targets by distance.
  • so if there is more targets than projectiles, it will shoot 6 closest targets.
  • if there are more projectiles than targets, it will shoot all targets at first,
  • then remaining projectiles will be shot at closest targets.
  • and loop trough targets until all projectiles are fired.

So if there are 6 projectiles and single target, it will loop 6 times each time shooting at that single target.

For two targets it will loop 3 times, shooting at closest then other one, then repeating

For 4 targets and 6 projectiles, it will shoot all 4 of them, then loop it and with 2 remaining projectiles it will shoot second time at two closest targets.

So just make sorting by distance array. I suggest map variable that as index has distance (rounded to closest int), and as variable, it has either target location or target actor reference.

Uh, forget MAP variable, you cannot have 2 entries with same key.

So only way is to make struct then sort array of it by float as key.

PS. just found some great source for sorting in blueprints:

pps. However i think C++ would be much better for this.

and that sorting topic has link to FREE functions made in blueprints, that has SORTING:

So get that, use it to sort targets.

2 Likes