How to create trail effect on 2D sprite using the sprite's texture

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

I am trying to implement a trail effect on my sprite during certain states, similar to what you see below:

enter image description here

As pointed out in the answer below, there is a helpful discussion of this effect using Particles2D, in the documentation, but it doesn’t answer all my questions.

It seems like the documentation tutorial uses a static image for the texture of the trailing particle. My sprite uses an AnimationPlayer. How do I go about using the previous or current frame of the animation as the particle texture? I may also need to change the orientation of the particle to reflect the orientation of my sprite (i.e. flip it left or right).

I tried using:

texture = player.get_node("Sprite").texture

in my Particles2D node, but it assigned the particle my entire spritesheet, rather than the current frame of it.

:bust_in_silhouette: Reply From: AzorMachine

Here it is, an example from the docs. If you have a doubt about it, feel free to ask it!

It seems like that tutorial uses a static image for the texture of the trailing particle. My sprite uses an AnimationPlayer. How do I go about using the previous or current frame of the animation as the particle texture? I may also need to change the orientation of the particle to reflect the orientation of my sprite (i.e. flip it left or right).

Diet Estus | 2018-03-17 19:59

I tried using:

texture = player.get_node("Sprite").texture

in my Particles2D node, but it assigned the particle my entire spritesheet, rather than the current frame of it.

Diet Estus | 2018-03-18 02:54

Any idea? I’m sure it’s simple, just can’t figure it out.

Diet Estus | 2018-03-20 20:17

The docs are showing a static texture. It doesn’t show how to make it update as the main sprite updates.

Zakkle | 2018-04-24 13:58

:bust_in_silhouette: Reply From: zhangyao

I just figured a bit silly way to address this in AnimatedSprite since I just encountered the same problem, not sure if it will work for your AnimationPlayer.

The trick is to create as same number of animations as sprites, or frame you have, then load different sprite textures one by one into in each trail. Now you have one animation for one trail.

Then, in the script, you use “$Trail.emitting = true/false” to enable different trails you want to have in a certain moment. Remember to disable the rest completely and save the one you want to the display, and you could make if functions or for loop to make different conditions for which to frame of trail to display.

For example, mine is a pretty simple code:

if velocity.x != 0:
    $AnimatedSprite.flip_v = false
    if velocity.x > 0:
        $Trailup.emitting = false
        $Trailleft.emitting = false
        $Trailback.emitting = false
        $AnimatedSprite.animation = "right"
        $Trailright.emitting = true
    elif velocity.x < 0:
        $Trailup.emitting = false
        $Trailback.emitting = false
        $Trailright.emitting = false
        $AnimatedSprite.animation = "left"
        $Trailleft.emitting = true
elif velocity.y != 0:
    if velocity.y > 0:
        $Trailback.emitting = false
        $Trailright.emitting = false
        $Trailleft.emitting = false
        $AnimatedSprite.animation = "up"
        $Trailup.emitting = true
    elif velocity.y < 0:
        $Trailup.emitting = false
        $Trailright.emitting = false
        $Trailleft.emitting = false
        $AnimatedSprite.animation = "back" 
        $Trailback.emitting = true

I created 4 trails for my four animations.

I hope this can help :slight_smile:

:bust_in_silhouette: Reply From: Mrpaolosarino

Fuck these enums and brain destroyers whatsoever, here’s a very simple tutorial for it :):

That was amazingly helpful!

samjmiller | 2021-04-29 15:20