I’m making a Space Invaders type game, and I cannot for the life of me get the bullet shot by the player to be detected by the enemies. I’ve tried move_and_collide(), layers and masks, changing the bullet from a characterbody2d to an area2d to a staticbody2d and nothing seems to work.
Here’s the bullet.gd
extends Area2D
class_name Bullet
var speed = 400
var should_shoot = false
var enemy_should_shoot = false
func shoot_up(delta):
visible = true
position.y -= speed * delta
func shoot_down(delta):
visible = true
position.y += speed * delta
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
add_to_group("player_bullet")
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta: float) -> void:
if should_shoot:
shoot_up(delta)
if enemy_should_shoot:
shoot_down(delta)
func _on_visible_on_screen_notifier_2d_screen_exited() -> void:
queue_free()
#This function does not work
func _on_area_2d_body_entered(body: Node2D) -> void:
if body.is_in_group("enemies"):
body.queue_free()
#This function does not work
func _on_body_entered(body: Node2D) -> void:
if body.is_in_group("enemies"):
queue_free()
#This function does not work
func _on_area_entered(area: Area2D) -> void:
queue_free()
Doesn’t look like you connected any signals inside your Bullet scene. Are you sure the functions listed are connected correctly or are they only named _on_body_entered, not generated by the signal connection menu in-editor?
They are all generated by the signal connection menu (_on_area_2d_body_entered was a previous attempt to fix it, didn’t work, but it was connected properly)
I should probably clarify that the functions do functionally work, but they cant detect the invaders, putting the print statement (print("Colliding with: ", body))at the top of each function simply prints Colliding with Player:<CharacterBody2D#39678117334> once and never again. for _on_area_entered it only prints (replacing body with area) Colliding with Bullet:<Area2D#42647684595>.
What do you expect to happen versus what is really happening in the game? If the prints work, then they are colliding. Did you try to print after the if statement?
What I expect:
When my bullet touches the invader, it destroys it.
What happens:
It just passes through them.
Printing after the if statement just repeats the same text I showed above. Putting the print statement in the if statements causes nothing to be printed out.
I notice the GreenInvader has a bullet child are you doing the same thing for your player? Are you instantiating new bullets or re-using this one projectile? Did you connect the signals inside the bullet scene or the player’s scene?
If possible could you upload the entire project somewhere?
The Bullet that exists inside your player is essentially never used, you should delete it. Then notice that it was a different bullet than that of your Bullet.tscn, which does not have any signals connected. Your player instantiates the Bullet.tscn that does not have any connections.
The fix is to connect signals through the bullet scene. You may have to tweak the connected function logic to differentiate player shots from enemy shots.