Piercing bullet using Characterbody node?

Godot Version

4.6.1

Question

I’m trying to get bullets to pierce enemies when they have a piercing debuff, but since I’m using Character body based bullets, it might be a bit tricky. Since I’m calling a damage function through the bullet, I need the bullet to be able to pierce enemies and only damage them once. I tried doing this through an array that stored the id’s of enemies hit so they wouldn’t get hit again, but that lead to the bullets neither damaging nor going through the enemy. The code for that looked like this:

var enemies_hit : Array = []

var collision = move_and_collide(direction * speed * delta)

	if collision != null :
        if collision.get_collider().has_method("damage"):
			if collision.get_collider().pierce != true:
				collision.get_collider().damage(dmg, 0)
				queue_free()

			else:
				for i in enemies_hit.size():
					if enemies_hit[i] != collision.get_collider_id():
						collision.get_collider().damage(dmg, 0)
						enemies_hit.push_back(collision.get_collider_id())

If I’m doing this wrong or if this needs to use an area body node please let me know

You need to move the enemies_hit variable declaration outside of the function’s scope to the class scope. Otherwise you’re resetting it on every hit.

Also, your for loop at the end of the function is not doing what you want it to do. You don’t need any loop at all, just check enemies_hit.has(collision.get_collider_id()).

For the CharacterBody to pierce enemies, you have to add the colliding enemy as collision exception. You won’t even need an additional array then.

			else:
				collision.get_collider().damage(dmg, 0)
				add_collision_exception_with(collision.get_collider())
1 Like

Thanks for the replays! The array method didn’t really work because the bullets would still stop at an enemy without piercing. Thankfully the answer was a lot more simple