Godot Version
4.5.1
Question
how to make a rigidbody explode when hit by another rigidbody
so my game here is a nice little angry birds clone, and im trying to add a tnt thing
basically, im trying to make it so that, when the tnt is hit by another rigidbody2d, it explodes, and launches other rigidbodys nearby away
here is the tnt scene:
does anyone know how to add this? thank you
Hello!
I just (literally in the middle of reading this post) made something very similar. My idea is to have an Area2D as the “explosion”, and when the explosion goes off, it checks for bodies in its CollisionShape2D. Then it iterates through them and applies a force to all RigidBody2Ds.
func explode():
var in_explosion: Array[Node2D] = explosion_area2d.get_overlapping_bodies()
for node: Node2D in in_explosion:
if node is RigidBody2D:
var force_direction: Vector2 = (node.global_position - global_position).normalized()
(node as RigidBody2D).apply_impulse(force_direction * explosion_force)
# Explosion force could be calculated based off of distance from the
# explosion, but I haven't figured out that yet.
Here is it in action: (mine activates manually)
1 Like
thank you thank you!! ill try to add it!!
again, thank you for helping, but im struggling a bit to add the explosion, do you mind helping?
i attached this script to the RigidBody2D (TNT)
(the “drag” input is just left click)
i also added an Area2D to the RigidBody2D

but when i test the game and click it, nothing happens
am i doing something wrong? again, thank you for helping
Based on your code, two of the three yellow warning lines are about standalone expressions and one is about unused parameters, am I correct?
When calling functions, place () at the end, like explode(), otherwise it is just a standalone expression instead of a function call
(Standalone expressions are lines of code that do not do anything.)
func _ready():
explode() # Put "explode()" instead of "explode"
func _input(event):
if Input.is_action_pressed("drag"):
explode() # Put "explode()" instead of "explode"
Also, since you are using _input(event), you should use the “event” parameter, otherwise it goes unused, which creates a warning line.
func _input(event):
# event instead of Input.
if event.is_action_pressed("drag"):
# You can use Input, but using "event" is how it is supposed to be used.
explode()
Hope this helps! (I apologize for taking so long to respond.)
2 Likes
thank you!!! it works now!!! thank you so much!!!
2 Likes