Best method for finding actors within a distance from player?

Hi,

I’ve got a bunch of items and they have sphere Triggers on them. When the player overlaps with the sphere the item highlights. However, I’m thinking having a lot of items in a scene running sphere triggers is probably not optimal. So, looking for some advice on the most optimal way to do it?

Some alternative options I can think of…

  1. Have the sphere trigger on the player, not the items, so it’s only one object checking for the overlaps not many of them.

  2. Have the player run a sphere overlap a few times a second (I don’t need it ticking every frame).

I here that the most performant option is often to get the squared distance from the objects, but I’m not sure how I’d do that without an overlap sphere to identify them first??

Any advice?

Many thanks!

Seems like you’ve answered your own questions. Look into collision channels so the overlap only bothers with what it needs to rather than everything.

Thanks, yeah I’m thinking it’s one of the two options.

Although now I think about it, overlap triggers get a single actor, not an array of all actors so I think my option #2 is likely better.

So, I’m thinking the best method here might be to run an overlap sphere maybe 3 times a second and collect all actors that it collides with (as they will have a specific collision setting to filter them).

Also, I assume sphere triggers run every frame?

How do you want it to work? Actors highlight (or do what they need to do) when you:

  • get close them, automatically
  • press a button

All still with proximity in mind, ofc.


How close is this:

It seems you are pre-optimizing. You can be sure that attaching functions to events such as OnOverlapBegin and OnOverlapEnd (something along those lines), will be the most performant option there is.

Your fear of getting only one object per event is wrong as your callback will get called per every object overlapped. So if you want to keep an array of objects within the given distance then all you need is to push on Begin and remove on End.

Avoid ticks, avoid timers. These are not things you want to be inconsistent.

Performance wise you can make custom channel reserved just for this.

1 Like

Yeah, they just highlight and I add them to an array. Don’t need the closest or anything, just all within a distance of the player so I can highlight them.

Here’s what I’m currently thinking (from the player BP)…

Have a sphere overlap on the player instead and:

Now it runs only when you actually overlap (again, collision channel) and you have an array of actors close to the player. Would that work?

so I can highlight them.

You do not even need an array then. Send the message to the actor on Begin / End overlap and tell it to act accordingly.

In the same thread, a couple of posts below, there is a step-by-step that does the above, and more.

2 Likes

Super helpful, I’ll try that, thanks very much!!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.