How do shot the 3 nearest enemy at the same time?

How do I check the 3 nearest enemy, and shoot them at the same time, and when enemy left not 3, let say the enemy left 2, the player still automaticly shoot those 2?

What is the expected result:
let say I want to shoot 3 nearest enemy automaticly. and if the enemy left 2, player automaticly shoot those 2 enemy, and if the enemy left only 1, player automaticly shoot it.


and shoot those 2 enemy left

Hi amar,

I’m not sure I understood you correctly.

Every time the player shoots (let’s say with a mouse click), you want it to be able to shoot the nearest enemies (max of 3) that are in a certain range from the player?

What do you mean by player shoots “automatically”? Do you mean if 1 or 2 enemies are left, the player shoots those enemies “anutomatically” without clicking anything?

1 Like

My immediate thought is to add a boolean variable to the enemy object (I’ve called it isChosen):




The enemy objects with isChosen set to true are the three closest to the player at any time. And it works for less than 3 enemies too.

2 Likes

Thank you for answer, as MrMen reply, the player would automatically shoot enemy near. the code he give is almost perfect.

Thank you for your answer. I look at your event and try by myself and it works.
There is only 1 thing, the problem is, when the enemy left only 2 or left only 1. the bullet player still shot 3 times, so there is an enemy who got hit by 2 bullets.

I am giving you a screenshot of my event, could you give me the answer for that?
so I want the player shot 2 times when the enemy left only 2

Use this for the repeat event. It’ll repeat a maximum of 3 times, or less if there are less enemies on the scene:

image

Thank you so much, the code works perfectly, and I learned some of the expressions that might be useful in the future.

1 Like