In this video, we'll add a smoke trail behind the player's character.
We'll make it so when they move fast enough, the smoke appears,
but if they stop, it stops emitting.
Let's get started.
To get started we open the Godot scene
from the obstacle course, part 2 directory.
We go down Godot.tscn,
and we want to add a new particles 2D node.
Let's get started by setting the texture,
so expand the textures category in the Inspector,
right click on texture, quick load,
and we're looking for smoke.png.
It's going to be an ellipse like this one with a slight shadow.
We'll start by adding the process material
to animate the particles sprite.
Right click on the material slot and select new particles material.
Click to expand it.
We want to do a couple of things.
First we want the particles to have some velocity
away from the character,
Not gravity, this time we want the particles to move behind the character
to be like pushed.
For that we're going to start by expanding the gravity category
and setting the gravity to zero on all three axes.
The particles stop moving,
and then we're going to expand initial velocity.
This is going to be the initial movement speed of our particles,
and we're going to increase it a little bit,
so something like 150, there is enough,
and we can randomize it a little bit like so.
You can see that the particles all go to the right.
Instead, we want them to emit going up to the back of the character.
We're going to change the direction.
We have to set the X direction to zero
and Y to, is it minus one?
In this case it's minus one.
It's a bit particular because as I mentioned in a previous video,
these particles are designed to work in 3D.
In this case a negative direction makes the particles go up,
which can be a bit confusing, but that's how it is.
All right, so we have the initial velocity.
The next thing that we want is perhaps
the particle to be scaled down a little bit.
They are too big.
We go down to the scale category and set the scale to something like .5.
Yeah, we can have the smallest scale be like this
and then add a bit of scale random to have some larger particles
to have more variation as you can see.
Next, we're going to add a bit of animation, two types of animation.
First, we're going to have them scale up
and then back down like before, so the particles don't disappear
as they are doing right now.
Click on the scale curve slot, create a new curve texture
and expand it, expand the curve.
We're going to do the same as before.
Lower the first point, lower the last point as well,
and right click around 25% of the particles lifetime,
and we're going to have the particles scale up the leftmost point,
I want to be a bit larger.
You want something roughly like what you are seeing on screen.
They don't start tiny, they scale up
and then back down a little bit and maybe not as much.
That's for the scale animation.
Next I want to animate the color of these particles.
Expand the color category and we're going to use a color ramp.
Like the scale curve, the color ramp allows you to animate
the shade of the particles over time.
Click on the slot and create a new gradient texture.
You are going to see them turn black.
We're going to expand the gradient, create a new gradient resource in there
and now you can see the particles animate from black to white.
Click to expand the gradient and open the gradient editor.
It works with what we call color stops.
Those are the little vertical pins or boxes that you can see.
You can click and drag them to shape the gradient
and make, for example, the particles black for a longer time.
We're going to pull this pin to the left of the gradient.
On the right side the box with the dark tone
represents the selected pin.
If I click on the white one you can see the slot turns white.
I'm going to select the black box again,
then click on the color to open the color picker
and make it white as well.
It works like the modulate properties of nodes where the color that you choose
is going to multiply and darken the tone of the texture.
What we want to do here is to start transparent,
to have the smoke take its color after a little bit of time
and then fade away.
We'll start by clicking the color slot
and make the leftmost color fully transparent.
You can see now the particles fade in.
Then we're going to move the pin on the right to the left.
You click and drag on it to do that.
You have a bit of a fade in that's really short at the start.
Then we're going to create another pin on the right
by clicking on the gradient,
select the color picker and make it transparent as well.
You have fade in, fade out.
Now I want this tone to be preserved for a bit of time,
so I'm going to click near it to create a new color pin
and move that pin to the right
to maintain the opaque tone for a longer time.
In the gradient editor, where you click
is going to create a new pin that samples the color at the place you click.
If I click more to the right, it's going to make
an almost transparent white.
But if I click very close to my pure opaque white tone,
it's going to make a new pin that's not too transparent.
I can move it to the right to keep the particles opaque for some time.
The particles are a bit too white.
I want to make them a bit gray, purple, something like that.
I'm going to click on my color pins, click on the color picker there,
and maybe select one of the tones in the color palette in there.
Not make them as blue,
I'm going to select some grays.
Same thing for the second color pin, I'm going to go into the grays in there.
I'm playing a bit with the colors because there again,
this is something that you eyeball.
What I feel is that now the particles are a bit too dark,
so I'm going to brighten those a little bit,
and perhaps this one as well.
There you go.
I'm satisfied with the current look of things,
except for at the start, you can see it's a bit brighter.
The reason is that our pins on the left and on the right
are fully transparent, but they are fully transparent white tone,
so we have a transition from white to that darker gray.
I'm going to select the pin there and make it just a bit gray,
so we don't have that jump in tone at the start,
and same thing at the end when it fades away,
going to make the tone a bit more gray.
The last thing that I want to add is a bit of damping like before,
to make the particles change velocity over time,
because right now it's constant and it doesn't look amazing.
Expand the damping category
and we're going to crank it up until the particles start to stop a bit.
We can randomize that value as well, so some particles fly farther than others.
It makes the motion a bit more chaotic and slightly more realistic, let's say.
Next up we are going to do two things.
First, as you can see the particles are showing in front of the character.
We want them to be behind the character instead.
We can do that in the inspector.
We can go down to the visibility category
under Canvas item, and then click show behind the parent.
By default the nodes that are children of another show in front of the parent,
but we can reverse that easily with this property.
Lastly, when you select the Godot node
and you move it, you can see how the particles move with the character.
We don't want that, we want the particles
to stay where they are and just animate.
There's a feature of the particles 2D node
that controls that.
We select the node, go back to the top of the inspector.
I'm going to fold the process material
and textures because we're I'm done with those,
and then expand the drawing category.
You can see this property local coordinate.
What this does is that it makes the motion relative to the parent node,
the good old character here.
But if we turn it off you don't see anything changing at first
except that if you move the character,
now the particles emit in some place
and they are going to move relative to that place.
Okay, the thing is that when you do that,
when you start moving the character, you can see that even though
when it's not moving there are enough smoke bubbles,
when you stop moving there aren't enough.
We need to select the particles again
and crank up the amount at the top of the inspector,
could say something like 20.
Now that should work a bit better.
You can see the smoke trail is a little bit nicer.
That's how our particles are working.
Now we want to change the name perhaps for our code.
We'll call them smoke particles.
If you try the game now you'll see one problem
it's that the smoke is always showing up even when the character is not moving.
That doesn't look too amazing.
We're going to use code to toggle that on and off.
I'll go back to the Godot scene to match your setup,
and there we can click on the script icon
to open the script editor.
We're going to add a new on ready variable to get our smoke particles.
On ready var, smoke particles
and I'm going to use the dollar sign to get the smoke particles node.
Then we only need one line.
It's a line to toggle the smoke particles emitting property.
If we turn it off, the particles stop emitting.
If we turn it on, they keep emitting.
We're going to start with a value of off or pause in code.
Then in the script, we can put that at the end
of the physics process function,
we can say smoke particles that emitting,
that's the property's name and code,
is equal to,
and we're going to use a comparison
that will evaluate to a value of true or false.
The way we can detect the character speed easily
is using the velocity.
We can say velocity.length,
length is a function of the Vector2 class that returns the Vector's length,
is greater than some value.
One thing we can do is if the character is moving too slowly,
for example, at speed slow, we don't show the particles,
but if it's above that threshold, then we can start showing the particles.
We can use a value like speed default divided by two,
something like that.
Now you can run the scene actually and move Godot
and you'll see that when I move the character after a short while,
it starts having these smoke particles.
If I stop the particles stop emitting.
With that you have a nice particle trail.
In the next video, we'll complete the series
with a racetrack overview animation to make the game feel a bit nicer.