[Solved] Projectile Ricochet Behavior

Hi all!

I’ve been trying to do a projectile ricochet attack for my game but I’m stumped as to how to get it to work.

An example of what I’m trying to achieve is where after hitting the first enemy the projectile would then ricochet to other nearby enemies a set amount of times before being destroyed. Similar to a homing projectile but I’m looking to get it to home on to multiple enemies one enemy at a time.

Any help would be greatly appreciated!

Thank you!

The simplest approach is to pick the nearest object. The only challenge is to filter out the last hit object so it doesn’t target itself. I don’t like using IDs. This uses the last x() position. It’s a floating number so it should be as unique as an ID without the setup/maintenance. You could always add the last y().

As far as counting hits, it could be a variable, a timer or even a tween that slowly shrinks the projectile. That would work nicely if it was a fireball.

This is likely to create a ping pong effect if there are only 2 objects close together but it would also seem unnatural if the projectile steered around one enemy to target another.

Thank you so much! I tried implementing it in my game and it’s working great!

1 Like

Thank you again Keith for the example. It has been working great as I said however, I’m having an issue now where when as my enemies get grouped together as they move towards the player, the projectile will shoot off away from the enemies after hitting one.

I’ve tried to figure out why this would be happening and as far as I can tell the projectile looks like it isn’t registering the collision after hitting the first enemy due to them maybe being to close? I could be wrong but that’s about all I can tell for right now.

If you have any thoughts as to what it may be I’d appreciate it!

I agree. It sounds like the collision masks might be overlapping. With a trigger once the collision would have to go from true to false before triggering again. I’m not at my PC to experiment. I don’t currently have any ideas. Maybe someone else does. I’ll think about it in the meantime.

Maybe move by tweens or use object IDs so you could “filter” collision or maybe link the projectile to the target to filter the objects. IDK. I’d have to test it.

Thank you for the response! I’ll keep testing it as well and see if I can come up with anything that may work. If you think of anything in the meantime I appreciate all the help!

So I found a sort of solution. Not sure if this is the best way for this but it seems to be working well for my purposes.

My best solution for the overlapping collision masks was to make a hitbox for each enemy and use that for the projectile collision. I made the hitbox smaller than the collision mask I gave for each enemy as well as a smaller collision mask for the projectile and that gave just enough space for the trigger once to reset.

Hopefully this is helpful to someone looking for a solution to a similar issue!

1 Like