Emit particles that follow mouse click?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By miss_bisque

I’m creating a basic game where certain sprites disappear when clicked. Id like to add some simple visual effect to the action of the object disappearing. I want some particles to trail the mouse click as its pressed on the screen. Similar to the light emitted when slashing a fruit in fruit ninja.

Does anyone know of an efficient way of doing this?

here is what my script looks like right now in case that helps.

I have a rigid body 2d node with a sprite and collision shape 2d as children. When the sprite is clicked on during the game, it disappears from the screen

extends RigidBody2D

var pos
var w = 80
var mousePos = Vector2()
var jump = randi()%50+250
var dir = randi()%200-50
var tex_ref_array = Global.tex_ref_array
var target_objects = Global.target_objects

# make objects bounce
func _ready():
	$Sprite1.texture = tex_ref_array[target_objects[randi()%target_objects.size()]]
	apply_impulse(Vector2(0,0) , Vector2(dir, -jump))

# make objects disappear when clicked
func _physics_process(delta):
	if Input.is_action_just_pressed("click"):
		mousePos = get_global_mouse_position()
		pos = global_position
		pos.x -= w/2
		pos.y -= w/2
		if mousePos.x >= pos.x and mousePos.x <= pos.x + w and mousePos.y >= pos.y \
		and mousePos.y <= pos.y + w:
				get_parent().targetObjectsPoints += 1
				queue_free()
	if global_position.y > 500:
		get_parent().fallingObjects +=1
		queue_free() #clear cache
:bust_in_silhouette: Reply From: jgodfrey

I’d probably do something like:

  • Create a scene consisting of your Particle System, configured how ever you want, with its Emitting and OneShot properties set to On
  • Preload that particle system scene in the above script
  • It looks like you already have click-processing code above. So, there, just instance the particle system scene, and move it to the location of the mouse cursor (mousePos above).
  • Add it to the scene

Note, you’ll probably want to parent the instanced particle system to a higher-level parent in the scene since you’re immediately queue_free()'ing the above node.

Or, you could wait until the particle system finishes before queue_free()'ing the node.

Thanks! That mostly worked except i’m curious as to what you mean by parenting it higher… I ended up instancing right before the mousePos variable is initialized in func _physics_process(delta). Would that take care of the que_free issue you mentioned?

extends RigidBody2D

var trail = preload("res://Scenes/MouseTrail.tscn")
var pos
var w = 90
var mousePos = Vector2()
var jump = randi()%50+250
var dir = randi()%200-50
var tex_ref_array = Global.tex_ref_array
var target_objects = Global.target_objects

# make objects bounce
func _ready():
	$Sprite1.texture = tex_ref_array[target_objects[randi()%target_objects.size()]]
	apply_impulse(Vector2(0,0) , Vector2(dir, -jump))

# make objects disappear when clicked
func _physics_process(delta):
	if Input.is_action_just_pressed("click"):
		var t = trail.instance()
		t.global_position = get_global_mouse_position()
		add_child(t)
		
		mousePos = get_global_mouse_position()
		pos = global_position
		pos.x -= w/2
		pos.y -= w/2
		if mousePos.x >= pos.x and mousePos.x <= pos.x + w and mousePos.y >= pos.y \
		and mousePos.y <= pos.y + w:
				get_parent().targetObjectsPoints += 1
				queue_free()
	if global_position.y > 500:
		get_parent().fallingObjects +=1
		queue_free() #clear cache

miss_bisque | 2020-11-13 00:09

My concern was with any of those queue_free() calls. Essentially, those are freeing the node the script is associated with. So, if you add the scene you instanced (t above) as a child of that node (as you seem to be doing above), when the node is freed it’s children will also be freed (so, the just-instanced t scene.)

I assumed the result of that would be that your newly instanced scene would be freed before the particle system had a chance to finish running.

If that were the case, my suggestion was to add the newly instanced scene as a child of something “higher” in the scene tree - something that wouldn’t be removed immediately like the current node could be (based on certain logic being true above).

jgodfrey | 2020-11-13 00:50