Attack nearest enemy

Hello everyone!

I was wondering, I have my combat system up and ready to go and does well but I wanna be able to attack the closest person to me in the direction I’m facing, so I don’t attack somebody that comes in for an attack and ends up closer or if I hit my enemy in a field of enemies and start jumping from one enemy to another.

Here is my implementation of finding the closest target in the limited angle

Using vector 2d - gun rotation range X and Y as angle limitations left and right

Thanks, I’ll look into that pathway and test it out to the best I can :grin:

Same here Can’t Fix This From Past Few Months Tellthebell

The easiest thing would be to give the player an extra sphere collider.
Each enemy beginning overlap with it, registers itself as actor to an array of actors innthe player… And removes himself when leaving the overlap.

And the player, on firing/attacking, just foreach iterates the array, gets his location and the actor location of the current iterated enemy… Gets the distance of both… And the shortest distant enemy will be returned as target.

Why the collider?
To not iterate over every enemy in the whole level map, just the closest ones.