Find and cast Linetrace to Closest Object

Hey,
I’m trying to when I shoot a target it does a linetrace to that target and then if its a valid target, look the nearest valid Dummy objects (of the same class blueprint) and then shoot a linetratrace to that object, and continue repeating it for an X number of times.

I’m finding nearest object by looping through all objects of the same class (dummies) and comparing distances to the object that has hit

I managed to make it work shooting the first target (through the player character blueprint) and then it shoots to the nearest (dummy blueprint event dispatcher to level blueprint) but I can’t make it continue finding the next target. I’m pretty sure my problem is when I bind the event “Find Closest Object” (that is an Event Dispatch from the Dummy blueprint) I’m using Get Actor of Class (Dummies blueprint) and then it only works with the first object of that class it finds… And since I can’t bind an event to an Array I don’t know how to make the second dummy continue looking for the next closest dummy. I’m sure there is a simple way to do it but I’m losing my mind trying to figure out.

Does this make sense?

If I’m not mistaken, this has nothing to do with distance and more to do with order added to level.

In the blueprint you shared, I don’t see how you are comparing to get the closest one.

Could you share this blueprint?

Hey! Thanks for the reply.
So my issue is not actually finding the closest one. I’m actually able to find the one that is closest to the first object I shoot…
My issue is to continue looking for the next one after the second because when this event fires again the “FindAndZap” custom event is bound to “Get Actor of Class”, which only gets the first object of that class it finds… so every time this event runs it only applies to that same object.

I will try to show step by step what I’m doing:

  1. Player character blueprint shoots at the dummy and that does a linetrace and applies damage:

  2. The dummy blueprint, when it takes damage, applies the “damage” by changing the colour of its material (irrelevant for my problem but just wanted to explain), and then fires the Find Closes Object event dispatcher (so far so good)

  3. In the Level Blueprint, on Event Begin Play, I bind the Find Closest Object (with Get Actor of Class target) tp tje Find and Zap Closest custom event.



What happens in the game is that I fire on the first dummy, and they change material, if I fire on one specific dummy (the one that it was used as Target for the bound event) then it does find the closest dummy and does the live/sphere trace to them and “damages” the second one no problem… the issue is that when that second dummy is “damaged” the FindAndZap custom event does not try to find the NEXT closest target because the event is bound to that one Get Actor from Class target… that’s what I don’t know what to do. How do I make it so every time the FindAndZap event runs it finds the next closest target to the one that was just being damaged and not only that one actor.

I hope I made it clearer now!

Did a quick demo to test it out. this is what I ended up with: Chain closest within radius - YouTube

It’s a bit longer than I thought it would be :upside_down_face: so rather than sharing blueprints, though of just summing up the steps to build it. I’m doing this in an actor component + spawning another actor for the timed “jumps”. These are the steps:

  • You’ll need several arrays for this. the first one is ActorsToIgnore → this will hold the actors that have already chosen as closest. Your trace hit should add the first to this array.
  • I used Sphere Overlap Actors node with the ignore array connected. You can specify the specific class you want this node to return.
  • check if the overlap returned 0 to cancel. If not make an array ( OverlapedActors ) and loop through it. On each index get the distance of the last actor in the ignore array vs the overlap array. Then add that distance to a new float array.
  • When loop is complete, get index of smallest distance and use that index to add a reference from the overlaped array to the ignore array. This way we are building the ignore array so that for next loop, it skips calculating distance and we are also building it in order as found from first actor hit.
  • Now you need to check the count: I made 2 ints; one for current and one for total jumps. Add one to the current, then check with a branch: If current != total → clear both float array and overlaped actors array and call the loop again.

I found I needed 2 checks to avoid access none:

  • First one is right at bullet#3 when we check the overlaped actors: if array length == 0 → No more targets so reset current count, float array and overlaped actors and move on to jump with timer.
  • Second is when current count == total → Reset same as above and move on to jump with timer.

For this final part, I spawned a new actor that only recieves the ignore actors array and I reset all remaining variables for next time player interacts with a valid actors.

Thought spawning a new actor was the best solution because it allows to have multiple chains running at the same time, each with their own timer.

So it’s:

  1. Character traces
  2. Hits valid target and passes to component
  3. Component finds closest of first target, adds to array, then finds closes to second target ignoring what’s in the array, etc…
  4. Spawn actor with copy of closest actors in order and do stuff.

Hope it helps. Feel free to ask if you need clarification on something specific.

Just FYI: did not use a single delay node for this. Only a Timer by Event in the spawned actor.

1 Like

This is awesome. :slight_smile:
I ended up making it work in another way but I will try this way too just for practice haha.

I heard the unreal community was great but I didn’t known it was THIS great haha

Thank you so much!

1 Like

Stick around and perhaps consider giving back to the community. :wink:

Hey! I’m trying to figure out something similar but I am having trouble starting. Would you mind sharing your version that worked ? I would love to see the inner workings of your blueprints (if possible)

Thanks,