I am double hit by an ennemy

Godot Version

4.2.2

Question

First, please ignore my bad englisch please.
Sometimes when my player get hit by an enemy, he got the double or the triple of the damage he is supposed to.
For the collisions with the ennemies, I wrote this code :

func check_collison():
	if collision_with_enemies:
		for i in get_slide_collision_count():
			var collision = get_slide_collision(i)
			if collision.get_collider().name == "Worms":
				health -= 10
				collision_with_enemies = false
				invicibility_cooldown.start()
	else:
		pass

NB : the invicibility cooldown is a timer and is code is :

	func _on_invicibility_cooldown_timeout():
		collision_with_enemies = true

What is the problem ?

You need to break out of the for loop to prevent additional hits.

if collision.get_collider().name == "Worms":
	health -= 10
	collision_with_enemies = false
	invicibility_cooldown.start()
	break
1 Like

thanks

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.