Finding nearest visible actor

As you can see in the picture, there’s multiple “balls” on the screen. How can I get which actor is the closest visible actor? I.E I might be closer to one of the “balls” but if I were not looking at it, it shouldn’t matter and should only check the closest one within what my camera is seeing

Also, these “balls” are in an array :slight_smile:

Hey @Lioad96!

Hope this helps :innocent:


Oh wait reading the question once again, seems like you also wanned to check if there’re any obstructions in the view right? Here’s an updated version:


Coming back to this, I think you might have also meant closest as in world location rather than screen position. In that case, it’s just this:

Wanned to add this one as well just in case :blush:


Also wanned to add one last thing, just like how we added a visibility check to our first approach where we were comparing the screen positions, you can also do the same in this case where we’re comparing the world locations. Here:

Be sure to clear our array before the loop if it’s not a local variable btw.

Hope these help!

Hi and welcome to the forum :slight_smile:

There is a node called WasActorRecentlyRendered and WasComponentRecentlyRendered. Enter a tollerance of 0.0. But it only tells you if it was the camera frustum (view cone of your camera) the last frame. So if the actor is in the camera frustum and occluded by another object it would still be true.

Also if you want to compare distances in 3D space, use the DistanceSquared (Vector) node with the Actor/Component GetWorldLocation node as input.

By the way: The squared part is only a mathematical simpler way, since the actual distance would need a square root computation. And you don’t need to do that if you are just comparing the distances.

Easiest eay is to set up a cone in game that has custom collision and is set to match the camera fustrum (because camera FOV can change).

The custom collision detects the objects.

Then the code shared above can be used to figure out the nearest.

The cleanest way however is to just have the collision code update who the nearest is on sweep and keeping that “nearest" set as a variable. And yes, this would be something you run on tick because the other actors can move within the cone as the player stays still changing who the closest is on you…