Hellow guys.
I’m making my 2D game in Godot 4.2.2 and I’m facing a problem.
I have a script that spawns a laser and a script that spawns stars, I have a level scene, on it, using a script, stars appear and with the help of another script they fly into a telescope, and in the telescope, lasers spawn with a script, and then they fly to where I point.
I want that when the laser and the star come into contact, the laser is destroyed and the star’s HP is taken away.
To do this, I added an area_2D node to the star and connected it using its signal (func _on_area_2d_body_entered(body) with a script that moves the stars, and in this signal I wrote:
if body.name == ‘laser’:
hp -= 60
if hp <= 0:
queue_free()
but nothing works for me, I inserted print(123) into the function and found out that I don’t even get into this function.
I have nothing on the laser stage, only the scene itself in the form of Sprite2D and the code on it that controls the flight.
plz help
You need a different node type for laser if it is going to be detected as a body. But, I think you want to use Area2D for that one too, if you are not going to handle physics but just want to move it in 2d space.
It will also require a shape (CollisionShape or CollisionPolygon) for detecting the collision.
The Star also needs a shape for the collision, if you haven’t already added that…
Also…
This looks wrong:
‘laser‘
Expected to be:
'laser'
Notice the subtle difference between: ‘ and '
That may be because you have pasted your code without code-formatting using ```.