How to add particle effect to line2D?

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

I currently have the script below to draw lines as user touches the screen. Wanted to add particle effect to the end of the line while user is drawing (pretty much where user is touching on the screen). Really appreciated for the help! I was only able to find particle effects like object explosion etc.

func _input(event):
    if event is InputEventMouseButton:
        _pressed = event.pressed

        if _pressed:
            _current_line = Line2D.new()
            _lines.add_child(_current_line)

    if event is InputEventMouseMotion && _pressed:
        _current_line.add_point(event.position)
        linePoints.append(event.position)

    if event is InputEventMouseButton && !_pressed:
        addCollisionShape(linePoints)
        linePoints.clear()
:bust_in_silhouette: Reply From: Zylann

You need first to create a particle system that continuously emits (which is the default behavior I think), as for when the user is drawing. See 2D particle systems — Godot Engine (stable) documentation in English for getting started.

Once you have such particle system node, you may set its position to where the user is drawing, and set its emitting property from your script, when you consider the user is drawing or not.

If you want to emit only when the user’s mouse/finger is pressed, you can start emitting when receiving a press event and stop when unpressed.

if you want to emit particles only when the user’s mouse/finger is moving, you might not want to do this all from inside _input because _input is only called when stuff changes. So perhaps start emitting when you receive InputEventMouseMotion while pressed, and set a timer variable. This will keep being set while the user moves. Then, in _process, you could decrement the timer with delta and once it reaches zero, you set emitting to false. Maybe it could also be done with a Timer node.

Thank you! I did create a particles2D and thought there could be a way to attach this to line2D.

ohpoloha | 2023-07-07 04:43