I want to make an explosion but it doesnt seem to work

Godot Version

4.2

Question

Hi, i have a game where you can select a gun and all the guns so far use the same bullet scene as a projectile. I wanted to implement a rocket launcher type gun that throws out a bullet like every othen gun but when that bullet touches an enemy, it damages every enemy in an area around it. I tried to increase the collisionshape of the bullet as soon as it colides with an enemy and then damage everything inside but it doesnt seem to work. This is the code inside my bullet

func _on_body_entered(body):
	$CollisionShape2D.scale = $CollisionShape2D.scale * 5
	explode()

func explode():
	var overlapping_bodies = get_overlapping_bodies()
	queue_free()
	for enemy in overlapping_bodies:
		print(overlapping_bodies)
		if enemy.has_method("take_damage"):
			enemy.take_damage(Global.dmg)
1 Like

For performance reasons, the result of get_overlapping_bodies() is not updated immediately, but only once per physics frame. So you can either wait a bit (in my tests, two physics frames were enough) with await get_tree().physics_frame, or simply use a second Area2D that is already initialized to the upscaled size.

4 Likes

To complement what @njamster said, i think is best you call queue_free after the for loop


$CollisionShape2D.scale = $CollisionShape2D.scale * 5

Also you should never change the scale of a collision shape, you need to change the shape size

From the Physics Introduction documentation:

Important

Be careful to never scale your collision shapes in the editor. The “Scale” property in the Inspector should remain (1, 1). When changing the size of the collision shape, you should always use the size handles, not the Node2D scale handles. Scaling a shape can result in unexpected collision behavior.

3 Likes