Collision Detection on a RigidBody Projectile

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

Hello,

I am making a to be multiplayer FPS. My current system is that the game’s weapons are children of the player and a signal is required to go from the player to the weapon. Right now I am trying to make a grenade launcher type thing that shoots capsule pills that will latter explode. I have gotten the player to tell the gun to shoot the projectiles, but when it comes to writing code for the projectile itself I am at a loss. I don’t know how the rigidbody projectile can efficiently and cleanly tell the shooter the damage it dealt and other information, and how to make it check if it collided with another player so I can tell it to explode and then subtract the health of that player. Signals don’t seem to be the solution to this because I would have to link a signal to every player every time a projectile is fired and the amount of players it made to be flexible with creating player and bot instances so me implementing multiplayer will be easier.

I don’t know how to make the rigidbody projectiles subtract the health of other player instances it collided with.

:bust_in_silhouette: Reply From: dustin

try using groups for the projectile-player collision detection.

the way you do this is you add the player instance to a group (this is set in the node tab next to the signals button). you should see a text edit area and a button that says “add”.
in the text edit area, type in the name of the group you want the player to be in. something like “players” will work fine.

then, in the projectile collision detection, instead of using signals to check if you are colliding with the player, you should check if the colliding node is part of the “players” group.

something like this:

func _physics_process():
   bodies = get_colliding_bodies()
   for body in bodies:
     if body.is_in_group("players"): #check colliding body is in the "players" group
        body.damage(damage) #whatever u want to do (i.e, damage the player)

this’ll make sure that only the players will recieve damage, nd not anything else.

note: the group name used in the collision detection should be the same name you put into the player nodes group name.