Bullet dealing constant damage to player

hello people,

i’ve been having this issue where my enemy bullet is dealing constant dmg to the player and quickly killing him.

i had the code set up for the player bullet that works on enemies and its thise one

func _on_body_entered(body) -> void:
	queue_free()
	if body.has_method("take_damage"):
		body.take_damage()

i dont understand why transfering it to the enemy kills the player instantly. printing the health i’ve noticed that it goes down super fast and by the amount the bullet should be dealing so it kinda works but it doesnt stop.

help!
thanks for reading!

Hi,

Your bullet code looks fine to me. You could add a print inside it to make sure the function is triggered only once, but I’m pretty sure it is since you’re freeing the node. Like this:

func _on_body_entered(body) -> void:
    print('On body entered')
    queue_free()
    if body.has_method("take_damage"):
        body.take_damage()

Could you share the take_damage function from the player script? The issue may come from there.

hi, first of all thanks for the reply.

the function i copied from the enemy to take dmg is this one

func take_damage():
	health -= 1
	animated_sprite.play("hurt")
	$AnimTimer.start()
	await $AnimTimer.timeout
	animated_sprite.play("Idle")
	

i’ve then commented out everything except the “health -= 1” line so there’s literally only that.
could it be that the collision keeps going on after the bullet hits? i’m so confused lol

1 Like

It could. Actually, seeing the take_damage function, it looks like that’s the issue is indeed related to the detection itself, which is weird to me as your bullet is freed once it collides with a body.
Can you try adding a print inside _on_body_entered as I suggested, to make sure that part is behaving normally?

Area2D(EnemeyBullet.gd)::_on_body_entered
90.0
Area2D(EnemeyBullet.gd)::_on_body_entered
80.0
Area2D(EnemeyBullet.gd)::_on_body_entered
70.0
Area2D(EnemeyBullet.gd)::_on_body_entered
60.0
Area2D(EnemeyBullet.gd)::_on_body_entered
50.0
Area2D(EnemeyBullet.gd)::_on_body_entered
40.0
Area2D(EnemeyBullet.gd)::_on_body_entered
30.0
Area2D(EnemeyBullet.gd)::_on_body_entered
20.0
Area2D(EnemeyBullet.gd)::_on_body_entered
10.0
Area2D(EnemeyBullet.gd)::_on_body_entered
0.0

this is the print coming from the on body entered, printing the signal and the dmg done to the player.. this is just one bullet set to deal 10 dmg one shotting the player with 100 health (also going in the negatives but ive cut it out here)

Thank you. Next thing to check would be the bullet spawning it self; are you sure you’re spawning exactly one bullet? The issue may be related to multiple bullets spawning at the same time, they would be overlapping so it would be hard to see but that would result in many objects colliding at once.
You can share the bullets spawning code if needed.

1 Like

shooting func here:

func shoot():
	var bullet_instance = BULLET.instantiate()
	get_tree().root.add_child(bullet_instance)
	bullet_instance.global_position = muzzle.global_position
	bullet_instance.rotation = rotation
	bullet_instance.speed = 550

at the top im proloiading the scene:

const BULLET = preload("res://Scene/Enemybullet.tscn")

this is the exact thing i’ve set up for the player and it’s working against enemies dealing only one dmg

forgot to mention the enemy is shooting with a timer that autostarts and visually is working fine

func _on_range_player_in_range() -> void:
	await shoot_timer.timeout
	shoot()

What if you add a print here? To ensure the shoot function is not called more than expected.

func _on_range_player_in_range() -> void:
	await shoot_timer.timeout
    print('shoot')
	shoot()

Another thing you can do to try fixing the issue, is checking that the bullet has not been queued for deletion before applying damage, like this:

func _on_body_entered(body) -> void:
    if is_queued_for_deletion():
        return

    queue_free()
    if body.has_method("take_damage"):
        body.take_damage()

That would ensure the bullet can collide with only one body. Maybe not something you want, but that’s worth a try imho.

thank you so much for ur help!

i’ve managed to find the problem but it was related to the way i tranfered the script from the enemy to the player!

thank you so much tho you were really helpfull!

3 Likes