# Evenly distribute/split homing projectiles between multiple enemies

**URL:** <https://forums.unrealengine.com/t/evenly-distribute-split-homing-projectiles-between-multiple-enemies/1282837>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine, Blueprint, question\
**Created:** [September 4, 2023, 3:01pm UTC](https://forums.unrealengine.com/t/evenly-distribute-split-homing-projectiles-between-multiple-enemies/1282837 "2023-09-04T15:01:11Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![Nawrot](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/nawrot/32/618260_2.png) [@Nawrot](https://forums.unrealengine.com/u/Nawrot)\
**Post date:** [September 4, 2023, 8:57pm UTC](https://forums.unrealengine.com/t/evenly-distribute-split-homing-projectiles-between-multiple-enemies/1282837/4 "2023-09-04T20:57:11Z")

</div>

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:

> [@Sorting algorithms in Blueprints](https://forums.unrealengine.com/t/sorting-algorithms-in-blueprints/1135992):
>
> It is common knowledge that blueprints do not include sorting functions. In my previous experiences, I have been able to create simple sorting functions easily. However, during my current project, I faced the challenge of sorting large data arrays and found that the sorting process was either very slow or unsuccessful. Since I wanted to avoid using C++ code or third-party development, I independently researched this issue. After conducting some research, I have decided to share my findings in t…

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:

> **[LE Extended Standard Library in Code Plugins - UE Marketplace](https://www.unrealengine.com/marketplace/en-US/product/low-entry-extended-standard-library?sessionInvalidated=true)**
>
> This plugin provides you with blueprints that add general functionality and improvements.

So get that, use it to sort targets.

---

_[View the full topic](https://forums.unrealengine.com/t/evenly-distribute-split-homing-projectiles-between-multiple-enemies/1282837)._
