Set Actor Closest To Player?

Hello I’d like to get the closest actor from my player and set it as a variable, I used get all actors of class along with a for each loop and compared the distance from the actor to my character using the get distance to node but it only seems to set only one of the actors and doesn’t update to the next closest actor.

I assume I’m using for each loop wrong or something. Any help would be appreciated thanks!

it only seems to set only one of the
actors and doesn’t update to the next
closest actor.

So what do you need? The closest actor or the 2nd closest? Or it does not work at all?

I’d like it to update the variable when another actor becomes the closest currently it will set whichever one is the closest first then it wont change after that even when that actor moves farther away from the player

I need the closest actor.

I’d like it to update the variable
when another actor becomes the closest
currently it will set whichever one is
the closest first then it wont change
after that even when that actor moves
farther away from the player

I need the closest actor.

Thanks for clarification, this should do the trick:

To make it more efficient you could have an overlapping sphere that checks only for actors in proximity. Checking against actors that are far, far away may not be necessary - although this would depend on how your game is structured, of course, and may make perfect sense.

If you have a fixed amount of actors to check against, it would also make sense to gather them in an array only once and then perform periodic checks on the results.

Image from Gyazo

Thank you!