How do I set up collision detection of multiple bullets for the player?

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

I’m working on a rhythm bullet dodging game so I’ve been making separate bullets to ensure they fire at the right time, but I need a way to have the player detect a collision and take damage. All of the bullets are Area2D nodes so is there a way to set up collision detection without doing it for each individual bullet?

:bust_in_silhouette: Reply From: jgodfrey

When you say that you’ve “been making separate bullets”, what do you mean?

You probably want to simply instantiate a single bullet scene when/as needed, so really no different than managing lots of things in Godot.

And, if you do that, you really just need to set up the single scene to work as desired in conjunction with the player scene and then every created instance will “just work”.

So, no need to set up collision detection “for each individual bullet”.

I’ve just been copying and pasting the bullet and then the code and changing it so it shoots out and moves in a certain direction. I’m fairly new to most of this so I’m not sure what instantiating is but this method does seem like a much more efficient way than what I was trying to do.

SaphyrX173 | 2023-05-03 19:39

Yeah, that’s definitely not the way to go. Likely, you just need a few scenes, like Player, Bullet, and maybe Main (or similar).

You can create the Player scene completely independent from the others and attach a script to handle “player things” like movement.

Next, create a single Bullet scene complete with an appropriate Sprite2D, CollisionShape2D, and script for handling “collisions” with the player.

The Main scene can be your “game”. In it, you’d place an instance of you player.

And, when/as appropriate, you’d instantiate new copies of the Bullet scene, which would come with all the behaviors of the single Bullet scene you already defined - including appropriate collision detection.

You might wan to spend a little time watching some tutorial videos and looking through the docs. In the long run, it’ll save you a lot of effort.

jgodfrey | 2023-05-03 20:05