Evenly distribute/split homing projectiles between multiple enemies

“a sphere collision that constantly checks”
this could be somewhere between Ok too, PLEASE don’t do this collisions can be rather intensive. if you want to check spherical collision with regularity then you could have the Sphere Collider set to Generate Overlap Events (it is under Collision in the details window for the collider in question) then place its OnBeginOverlap() (you can right click the collider in the Hierarchy->Add Event->OnBeginOverlap), and its OnEndOverlap() (same process just pick OnEndOverlap). in the OnBeginOverlap() check for the relevant things that define a target, and add them to an array. in the OnEndOverlap() remove the target from the array (it is safe to remove something from an array that was never in the array)

the other method would be to every so often do a Sphere Trace Multi every so often this way you can specify your criteria at the point of the Trace, and you will get an array anyways. you can even have the start point and end point be the same for just a “within this radius”

once you have the array and it “canFire” then sort the array for your criteria (keep in mind that a sphere radius followed by a distance sort will return things even if it is behind, above, or below the checks source this would require an extra angle check to determine if it “is in front of”)

the image I posted would be at the point of firing, and I even included a function call to “FireProjectile()” (this will probably need to be an event as things like spawning actors need to be in Events as the engine will prefer to spawn Actors Asynchronously to prevent stutters but when you have object pooling it could be a function as you would just be placing the projectile at its starting point, orienting it, and then activating it)

  • first we check to see if the Length() of the Array is Greater then or Equal to the Max projectiles. True go to point 2, False GoTo point 5
  • we start a for loop Iterating (walking/traversing) through the array
  • on each of those Entities we call FireProjectile() with the Entity as the target
  • then we check to see if the index of Entities is greater then MaxProjectiles (looking at this again it is actually wrong, and should be MaxProjectiles - 2 as projectile 1 will be shot at index 0, Projectile 2 will be shot at index 1, … projectile 6 will be shot at index 5, but I am breaking at the end meaning I need a minus 2 if I put the check at the start it could be minus 1) if the test is “true” then we break out of the loop early
  • I set RemainingProjectiles equal to MaxProjectiles
  • start a while loop that will continue as long as RemainingProjectiles is greater then 0
  • inside the While Loop I start a For loop iterating over the array
  • calling FireProjectile() on the element
  • decrement RemainingProjectiles (this is the same as GetRemainingProjectiles()RemainProjectiles-1SetRemainingProjectiles() compressed into 1 node)
  • check to see if the amount of RemainingProjectiles is still Greater then 0 (which upon inspection should probably be a “less then or equal to” node instead as the current check is the opposite of what is needed)
2 Likes