![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | wolvertox |
I’m just learning how to code so please excuse any blatant errors I may have made.
I want the enemy to take damage and die after it’s health reaches 0.
The bullet seems to function as intended, destroying itself after making contact with an enemy but the enemy doesn’t take any damage. The enemy is a kinematic body 2d and not an area 2d like the bullet because I want the enemy to directly interact with the player.
-enemy code
extends KinematicBody2D
func _ready():
add_to_group(“enemies”)
export (int) var speed = 50
export (int) var health = 1
signal enemy
func _physics_process(delta):
global_position.y += speed * delta
func damage(amount: int):
health -= amount
if health <= 0:
if has_signal(“bullet”):
damage(1)
queue_free()
-bullet code
extends Area2D
export (int) var bulletspeed = 300
var velocity = Vector2()
-signal bullet
func _physics_process(delta):
global_position.y -= bulletspeed * delta
func _on_VisibilityNotifier2D_screen_exited():
queue_free()
func _on_Bullet_body_entered(body):
if body.is_in_group(“enemies”):
emit_signal(“bullet”)
queue_free()