Closest enemy in a direction (left, right, forward, back)

Hi there, so i have a system set up where there is an arrow that rotates to the input direction of the player. So i was wondering if anyone knew how to see what the closest enemy in that direction was? (It’s for a targeting system if that helps!)

Hello, if that arrow helps you just to know what’s in front of the player just use GetActorForwardVector, if not, I can’t give a clear answer on that side.

To know what’s in the front of the arrow or what you are using for the direction, you could do a raycasting to find targets and see which one is the closest one

Simple logic for Raycasting:

  1. Get Actor Location (Start Point).
  2. Get Actor Forward Vector, multiply by desired distance, add to Start Point to get the End Point.
  3. Line Trace by Channel with Start and End Points.
  4. Branch (to check if the line trace hit something).
  5. Break Hit Result (to access hit details).
  6. Perform your logic based on the hit (e.g., check if the hit actor is an enemy).

Further adjustments may be needed to make it work, hope it helps :slight_smile:

Another option for targeting:
ClosestInCone


2 Likes

did not check the previous replies so apologies if repeating something

The first question i would like to know is if you already have reference to all of the enemies

if the enemies are already known, it is easy to check from them, rather than query from the player

whether enemy references are in an array, or you have enemies check from their instance and then report their distance and direction to a manager, in either case it is just simpler because you 100% know that you are querying all of the enemies and there is no room for an odd edge case, compared to if you do an environment query

So if you check each enemy, the distance check is simple enough, adn for direction you just use a dot product test to check for alingment with a vector that is relative to the player. You can use arrow components to help visualize and set them to be visible in the game.

you can filter out any enemies not meeting the dot product threshold, and then from the remaining, sort distance array to find the nearest.

There is also some pretty stupid but simple ways as well. Everybody likes elegant code, but sometimes, especially if you are a beginner, something that is more simple may be best because you can count on it to work.
So my “too simple to fail” solution would be add four colliders to your character to represent each direction quadrant. You can make a collision channel so that these only test for collision with enemies. Then when you want to test for nearest enemy, you can just identify which quadrant and then get all the overlapping actors, and then sort them by distance.

It may seem like having those colliders would be inefficient, and it probably is, but if you filter them by channel and also only do the distance check by an event I would expect that you dont notice any problems from it. It should work fine for a beginner project, I would think.

2 Likes

I’m offended… :face_with_raised_eyebrow:

The problem with line trace is that it may very well miss enemies that aren’t STRAIGHT ahead.

I would do what @BIGTIMEMASTER suggests, and iterate all enemy pawns.
Quickly reject those that are further than X total distance away, and those that are “behind” in the dot product with the forward vector.
Now, score each of the remaining enemies by proximity, and draw an arrow towards the closest one. Scoring might include visibility queries, penalties for off-axis location, and so forth.
To reduce number of visibility queries, start with the closest scoring actor, and calculate its visibility, if it’s visible, pick that one, so you don’t have to test others; repeat if not.

1 Like

I super like this sort of solution…
It’s so talented to figure out something like this

The first soluton worked really well thanks!

well i call it simple, but the complicated work is in the collision components themselves. Epic did the hard stuff, I just try to use it without adding too much of my own stuff to make a mess of things :slight_smile:

1 Like

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