Find nearest angle to actor in array, only in left or right direction

I’m making a battle system where the player selects an enemy to focus, and the player presses Q or E to switch between them, targeting the nearest right or left enemy. I can already get the delta rotation between the player and the actor, and select the shortest one, however I have no idea on how to filter out the actors if they are to the left, or the right of the actor.

Any help would be appreciated! :slight_smile:

Hello !

Can you make a little sketch of the situation to show an example of the situation ?

Is it something like this (where the player is in green and the enemies on red) ?


Yes, that is the essence of it. I took some screenshots and just did a diagram of how it works. Hopefully this makes it clearer.

Thanks for the help!

Well, several solutions are possible, and I will explain a simple one. If you think it’s not enough accurate for your needs, tell me :slight_smile:


Current : Current selected target
Right : the item just at the right
Left : the item just at the left
The red arrow : represents a vector from the Player to the Target
The green arrow : represents the right vector of the [Player to the Target] vector
The yellow arrow : represents projections 2D of the location on the right vector

Here is the principle : you have to figure out what is the right axis for your player to be allow to determine what is the right and what is the left for the player.
I think the best for you is the right vector of the vector (Player to CurrentTarget).
To calculate it, you can use some maths :

We project the pawn location and the item location to make calculus on 2D. After that, we find the vector (Player to Target) and, using cross product, find the famous Right Vector.

From here, you have to find, among all targets, the one that is just to the left or just to the right.
For left, you have to fill the conditions :

  • the dot product is < 0
  • has the highest dot product among the other items
  • the item is not the current selected one

For right, it is the inverse :

  • the dot product is > 0
  • has the lowest dot product among the other items
  • the item is not the current selected one

You also have to care about the item locations to be projected since we are using 2D maths :
Here is the blueprint to find the left item :

Here are some images of the result. Using only β€˜Q’ to select the left item.

I will make some comments to add more images and try to upload a zip of the project

Here is a link to the projet :

It has been achieved on 4.10 but it still should work on 4.12.
The logic implementation is still a little dirty but it will should you how it works.

1 Like

Great explanation :slight_smile:

Got it all working! Thanks for the big help