Enemies attacking each other

hi,
i have a group of enemies and i would like them to attack each other by choosing either a random target or the closest enemy thus forming duels in pairs.
how can i do it? this is my current event but obviously it doesn’t work the enemies all move in one direction
rrrrrrrrrrrr

1 Like

It can’t be done in a 1 liner. Add another sprite and name it Target. The try something like:

1 Like

thank you so much for sharing the events @MrMen ! what happens in both cases is a pile up of all the enemies… is it possible to create pairs of enemies that face each other?

In that case there’s a mistake in your events. I tested it with a dummy project, and the enemies paired up. Can you share a screen shot of your events?

1 Like

this is what happens in both cases
the enemies unite with each other
gif

Do your enemies all have a unique id? Keep track of the maximum id in a variable, and increase it and set enemy.id to it every time an enemy instance is created.

1 Like

How can I do this? Is this what you mean?

In that repeat event, you’ll need to set the enemy id, not set the scene variable id.

Change the last 2 events to:

sorry again @MrMen but the same thing always happens to me.
where am I going wrong?

In the third event:

set the value of enemy.id, not a target object. The target object doesn’t exist yet and only gets created further down.

sorry i couldn’t even copy :slight_smile:
i corrected it but the same thing still happens

You need to uncomment the event under “trova il nemico piu vicino senza pari al precedente”.

does not change by activating the event
Registrazione

Do you have enemy_id variable on the target object set to default to -1?

yes


these are the enemies’ variables instead

I attach the link of the project please if you have two minutes you can take a look thanks

This shouldn’t be enemy_id. It should just be id:

ok perfect now it works thanks again