If just recently started using godot and ofcourse I can't even figure out the simplest of problems. I'm making a simple arcade shooter and I am trying to get the bullets to detect when they've hit an enemy, but for some reason they don't.
-My bullet is an Area2D with an AnimatedSprite2D and a CollisionShape2D. -My enemies are a node with a Sprite2D and an Area2D which has a CollisionShape2D.
This is my bullet code:
extends Area2D
const SPEED = 300
func _physics_process(delta: float) -> void:
position.y -= SPEED * delta
func _on_body_entered(body: Node2D) -> void:
print("destroyed with bullet")
body.queue_free()
self.queue_free()
I hope someone can help me
I've checked that they're on the same collision layer and I've also tried to use rigidbodies with no luck. I've also seen basically every question ever asked about this, but none of their solutions worked for me.
When hitting an enemy it should print "destroyed with bullet" but it doesn't.
Godot's body_entered
signal only gets emitted when a PhysicsBody
or TileMap
enteres the Area2D
. To get the overlap of two Area2D
Nodes you would want to use the area_entered
signal. Your code should look something like this:
extends Area2D
const SPEED = 300
func _physics_process(delta: float) -> void:
position.y -= SPEED * delta
func _on_area_entered(body: Node2D) -> void:
print("destroyed with bullet")
body.queue_free()
self.queue_free()
Don't forget to connect the Signal with the method.