I am trying to make shooter game, i have already made shooting mechanic. Now i am trying to make explosive barrel but script i made is not working. My projectile is characterbody2d(“Bullet”) on mask 2, layer 2, with collisionshape2d and sprite 2d. Explosive barrel(layer 1,2 mask 1,2):
Script in characterbody2d:
func _on_hitbox_body_entered(body):
print(“detected”) #It shows detected in console only once(triggered by something background, not bullet)
if body.name == “Bullet”: #projectile name is “Bullet”
print(“Bullet”) #never printing
body.queue_free()
Wybuch()
else:
if body.name == “Wybuch”: #Reakcja łańcuchowa, Wybuch to sprite
if body.visible == true:
Wybuch()
Is this a bug? I tried relaunching godot engine and it still isn’t working
Oh and on photo is th warning that i dont have collisionshape, but after adding it, nothing changes, still not working, but now projectiles collide with barrel and stop, and also it detects collisionshape2d and prints another detected
If the on_body_entered() is not working as intended, then there are two reasons:
The body is on a different collision layer. The colliding object may mask a different collision layer and cannot detect the body.
the code exists but the signal is not connected yet (the method does not have a green icon on the left side)
I would like you to do the following:
on the line that says print(“detected”), please set a breakpoint and then start the game. Once it stops at that point, proceed one line and then check if the body.name is truly bullet? I believe it’s not Bullet but Bullet1 or a random hash.
Please change the expression to if body.name.begins_with("Bullet"): to include all Bullet duplicates on the same scene.
Thank you, it works now. It turned out it was because bullet was on different layer than area2d, i didn’t see before that there was layer and mask in Area2D(i looked for it in CollisionShape2D and parent like CharacterBody2D)