Bullet collision with multiple enemies

hi guys i have this type problem:
I’m using this code to create bullets that pierce through enemies


enemy damage is created multiple times, both when it hits and when it has been preenetated

is there another method or solution to solve my problem?

I can’t say that I fully understand your intentions. Question: When a collision happens, are you deleting the bullets?

I’m guessing the bullets go through the enemy and continue out the other side.

In which case, there are a number of scenarios/use cases to consider:

  1. When a bullet collides with an enemy
  2. When multiple bullets are in collision with an enemy
  3. When a bullet collides with 2 or more enemies simultaneously

My inclination on this is to give each bullet a unique id when it’s created - the simplest one is just an increasing value. On the enemy, have a structure variable that records each bullet as it collides with it. Use the bullet id as the key, the value can be left blank.

When a bullet collides with an enemy, check whether the id exists as a child of that structure variable. If it doesn’t, add it as a child and cause damage to the enemy. And if the child already exists, do nothing.

I think that should cover all 3 scenarios/use cases

sorry for my not perfect english but it’s not my first language … i think i understand how to do it:

that’s what you meant mr. men?
if enemies are attacked, only the first takes damage

No need for an apology. You are better at communicating in English than a number of the native English speakers on this forum :open_mouth: .


Yes, though I wouldn’t use the trigger once in the sub-subevent. It may not work the way you expect - I think it would just action once, and then never again until the scene is reloaded.

I tried to remove “trigger once” but the damage is always created to the first enemy.
these are the current events:

most likely I’m doing something wrong since I’m not very familiar with structure variables.
I searched the forum and was copying from an old post published by a user with the same problem as me

I still see the trigger once peeking at the bottom of the screen shot. That trigger once condition in the subevent has no other conditions in that event, so it will only ever trigger once during the game.

The check of the child exists in the structure is enough to ensure it only happens once for each enemy that is hit with the bullet. Remove that trigger once condition.


Also, do the collision between bullet and enemies first, then do the Repeat for each enemies as a subevent. It’s more efficient and faster for large numbers of objects.

If there are multiple live bullets, would he also need a for each bullet after the collision event? It’s confusing at times when a for…each is needed.

Collision with bullet
---- for each bullet
--------- for each enemy

The collision condition would “pick” any bullets that are in collision. So, that wouldn’t need a for…each before it but depending on what you do with the bullets, you might need a for…each for them.

1 Like

Yes, good point. There could be instances when there are multiple bullets and enemies involved in collisions.

1 Like

do you mean so?


it is not working, only the first enemy the bullet collides is taking damage

I think the problem might be with the variables or the wording of the conditions in the [Repeat for each enemy]

sorry for my insistence I did several tests but I think I’m doing something wrong with the variables.

I tried with and without the repeat for each enemy:

these are the screens of the applied variables:

enemies

bullet

Enemies should just have an object variable named “id”. It should not be part of a structure, and it should be a number.

Also, how are you ensuring each enemy has a unique id?


Bulletcollision should be a structure, with no children as default.

I’m so sorry, I’m finding it difficult to apply this logic… I tried to search the net but I couldn’t find anything like it.
do you have an example or a guide to solve this problem?
thank you