Trying to make a 2d explosion

Godot 4.3

I want to be able to click on the screen and spawn an explosion that blasts objects and the player away from the point of explosion. So it would be similar to rocket jumping except you click where the explosion happens. It would be side-view and their would be gravity if that changes anything.

I’ve looked at tutorials but none of them seem to work. Can you guys help? By the way I’ve only been using godot for 2 months so I’m pretty new.

1 Like

If your objects are RigidBody2D, then you can use RigidBody2D.apply_impulse() method, with the Vector pointing away from the clicked position.

If you need anything more specific - let me know.

Yeah, that seems like what I want but since I’m pretty new to godot I don’t really know how to do that. Like what is the code supposed to look like?

Very simple implementation of such a scenario would be the following.
This is the Scene tree:


This is a script attached to the “Player” node

extends Node2D


func _unhandled_input(event: InputEvent) -> void:
	if event.is_action_pressed("click"):
		for node: Node in get_parent().get_children():
			if node is RigidBody2D:
				var mouse_position: Vector2 = get_viewport().get_camera_2d().get_global_mouse_position()
				var direction: Vector2 = node.global_position - mouse_position
				var distance: float = direction.length()
				var impulse_power: float = 500.0
				var range: float = 500.0
				if distance < range:
					node.apply_impulse(direction.normalized() * impulse_power)

I have additionally created an InputAction “click” in the Project Settings.

And this is how it looks like in action.

2 Likes