Box Trace does not take closest actor

Hi all,
I thought the box trace would always take the closest actor, am I wrong?

Target switching works when the box trace is small enough so only 1 actor is in it, but I want it to be bigger and take the closest actor, but that is not working, is there a way to fix this?

Thanks

You’re not really tracing in a direction - we’re not giving the trace a chance to do what it’s supposed to since the start and end are the same.

This just plops a giant box and grabs whatever happens to be inside. If there are multiple actors, we have a problem. You might as well get overlapping actors instead. If you overlap more than one actor, you get the 1st one - not necessarily the closest one.

If there is a chance the volume trace will hit more than 1 target in the same frame, you may need another approach.

  • if tracing - use multibox trace, get all hit actors and sort by distance:

  • or, easier (imho), get overlapping actors with a component. Give the attacker a volume and:

You will still need to sort by distance.


I am wondering whether there’s a clever way to utilise sweeping for this :thinking: Try it like this maybe:

New location is where the attack / targetting would end up. This is in relative space so no need to add actor location.

Hey thanks I will try it out, how do I sort by distance?

Search for sorting by distance, has been done before.

I tried this but I cant get it to work: Sort array of actors by distance from player | Unreal engine Code Snippet

Not much else I can find on it

I have this:


Did I do something wrong?

and what am I supposed to do with the sorted by distance array?

It would be closer to this:

So like this?

What should I do with the sorted by distance array that comes out of the function?

get element 0

If all else went well that’s the closets actor.

Sadly did not work, 4 - YouTube

Sorry, can’t tell what is wrong without debugging this step-by-step. Did not look over the sorting function either.

Stat by ensuring that the multibox trace returns the expected results in the first place.

The multibox trace returns all the actors that are in the box trace, so I think that is correct.

Would you mind taking a look at the function when you have the time?
Would appreciated it a lot

This actually works:

Thanks for your time and helping me :slight_smile:

2 Likes

image

pff, did not know there was a node like that. Using my own libraries for so long that I forget to check what’s new. Sorry for sending you on a chase about sorting. At least we both learnt something.

1 Like

Yeah I didnt think of it either sorry for that. Happy it works now.