Hello everybody! I am still new to Gdevelop and game development in general. I am making a top-down 2D shooter where I want to implement a piercing bullet mechanic.
How do I ensure that the collision of the bullet and the enemy only checks once for each individual enemy instance?
For example, a bullet has a variable “pierce” with a value of 2. An enemy has a variable “health” with a value of 2. When they collide, “pierce” = 1, “health” = 1. The idea is that the bullet continues its way to an enemy behind the first enemy, even though the first enemy is not dead. When the bullet collides with the second enemy, “pierce” = 0, and the bullet is deleted. The problem arises when two enemy objects are standing right next to each other, which makes their collisions interlap with each other. Hence, the bullet counts these two collisions as one, and damages only the first enemy, piercing through both of them and continuing its way. I hope that the attached screenshots will help to illustrate what I mean by that.
I have tried different variations of the “For each object condition”, but it does not seem to help. I will attach the current version of my events, which may be useful.
Since I am a new user, I can only attach one media to the post, so here’s a collage with all related illustrations.
I am going with opacity check/change for bullets cause i don’t need another var to detect collision
Condition
Enemy opacity equal to 255
Bullet opacity grater equal or greater than 253
Bullet in collision with enemy
Action
Change enemy opacity set to 254
Change bullet opacity subtract 1
Wait 0.5
Change enemy opacity set to 255
Wait here is for enemy to cool down before he can get another hit
We switch opacity to 254 so it don’t spam action over and over (we made fake trigger once)
And then change it back to 255 after 0.5 sec so enemy can take hit again
Where we subtract 1 from opacity of bullet so now it is 254
When bullet hit 2nd enemy its opacity will be 254 and will be switched to 253
And you can now have separate event for bullet
Bullet opacity = 252
Delete bullet
Well that is for hitting 3 enemies not 2 but you get the logic
And you just shove in there your HP var subtraction for enemy to lose health
ALSO in 1st event we check if bullet opacity is greater or equal to 253 even so we are deleting it at opacity 252
BECAUSE in case of miracle where bullet would hit more enemies and game would not reach event for deleting bullet this way it will only be able to damage 3 enemies and not 4th
EVEN if it would somehow live long enough to collide with 4th enemy
Connecting the piercing mechanic to the opacity is such a simple yet effective idea that it is very easy to overlook. Thank you @ZeroX4 it works perfectly!