How do I create this combat mechanism?

I am making a game where enemies spawn randomly from both sides of the screen to attack the base in the middle. I want to create a mechanism where the player can tap and the hero will go to the nearest enemy and with a continued tap attack that enemy while letting other enemies pass. And after that enemy dies another tap will send the hero to the next enemy and so on. What would be the easiest way to achieve this?

1 Like

i think you could do a for each enemy loop get absolute value of enemy.X() - hero.X()
Then you have to check if the result is lower than the previous loop value :thinking:
so you will know which enemy is closer

1 Like

I’m sorry I’m not sure that I follow. Could you elaborate please?

yes is just get the distance in pixels with enemies in the x coordinate, maybe you also need to check y coordinates too.
Do you substract the x cord of he hero from each enemy x coordinate