Best way to count projectile bounces?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By karltha

I’ve been working on a 2D sidescrolling game and recently set up a special bullet type (using KinematicBody2D) that ricochets off of surfaces using move and collide and bounce().
That was pretty simple, but I’m not quite sure how to do the next part.
I made a simple diagram of what I’m trying to do: Imgur: The magic of the Internet

Like it shows, I basically want them to ricochet 2 or 3 times and then call their destroy() function on the next impact. Right now they’ll just bounce endlessly until their lifetime timer runs out.
What would be the best way to approach this? Thanks in advance!

:bust_in_silhouette: Reply From: jgodfrey

Assuming you have a bullet scent that’s responsible for detecting the collisions, it’s really just a matter of adding some sort of counter variable to the bullet’s script. Then, on each “collision of interest” (a ricochet surface) you’d just increment that variable. And, as soon as the variable reaches the max number, just queue_free() the bullet. You’d need to decide how to detect “collisions of interest”, but you could just put all of those objects in a common group and use an is_in_group() check when you get a collision…

Thanks! That worked perfectly. I think I was just overthinking the problem and assuming I had to use some special function to count collisions.

karltha | 2022-08-25 20:35