Get closest hit to origin in a multi sphere trace?

I’m trying to get the closest hit to the origin of a multi-sphere trace, but I can’t seam to find a clean efficient way of doing this without checking each and every result. Is there no order to the results? I assumed first index of the hit array would be exactly what I need, but it’s sometimes way off in no mans land.

I’ve a chain lightning ability and of course want it to chain to the nearest next target. Below is my collision check.

The top sphere trace was what I was originally using, but it always returns furthest point. The multi-trace below it is what I’m testing now.

You could make a function which takes the Out Hits array, grabs the Distance from hit result struct, puts into a local Float array, and then get the Min value from float array.

I don’t think there is any way to find a minimum value without comparing all the values, right?

Hope this helps on getting closest actor:

1 Like

I know I can intenerate through every hit, but was hoping to avoid that. Blows my mind there’s no built in functionality for this. Surely it’s a common check, lol. If I’ve to intenerate over the hits it will have to be moved to CPP as BP is ridiculously slow dealing with large loops as this could hit 30+ AI or more easily.

Built in or not, don’t think there is a way around looping through every actor to compare. Please do share if you find how.

1 Like

how much slower? how do you compare the difference?

It’s significantly slower. It’s easy to test. Make a simple bullet manager looping a large array of vectors and calculate their next destination based off their speed. Lets say there’s 100 bullets for example. This will be a lot slower in BP than CPP. It’s honestly the only bottleneck I’ve found in the BP VM thus far is that it’s terrible for looping arrays of data.

Anyway, solving this with the above BP solution for those curious for a solution. I however have implemented this in CPP. Works fine. Just weird there’s no internal solution for this.

how are you measuring the speed though? You described setup of the test but what do we actually measure?

You measure your game thread time. Look into using Unreal Insights if you haven’t been profiling your game so you can see how things are performing.

1 Like

Implemented a prototype in BP for anyone curious.

Basically take the hits, loop them, get the distance between trace start and the actor. Now add this to a Map variable with float key and the hit as the value. Once that’s complete get the keys from the map and sort them (I’m using LE Extended Standard Library here for this). Now loop the keys and add them to a sorted hits array using a foreach break and break the loop once you’ve the number of hits you want. You can optionally reserve the sort to get the furthest hits.