In this lecture, we're taking on a meatier challenge where we're going to have particles that play
when the character is touching the ground and turn off when the player is in the air.
So let's get started.
I'll start by showing you a new concept, and then I'm going to give you a nice, big, meaty challenge
that brings together a lot of the stuff we've been talking about in this section.
So you don't need to follow along with adding these things.
I'm just showing it to illustrate, we've got two sprites, one on the left, one on the right, and
each of them has a box collider on them.
So if I turn off that sprite renderer.
Yeah, you can see there's the box collider on the right one and the left one has the same.
Now, what we know already is that when this one or when that one moves and goes bump and bumps into
that, you can see the green line on here.
I've left it like that.
So you can see the green line.
This one also has its green line, which is the collider and it goes bump.
At that very moment we get an on collision enter event that happens that unity knows about and then
we can use that moment to do stuff.
We can say play a particle effect or end the level or do whatever or crash the player.
So there's a moment bump that's on collision.
Enter 2D we've been working through.
There's also another thing we know is that when they are no longer touching, there's another event
which is on collision exit.
So it works exactly the same way.
But one is when they touch and the other is when they stop touching.
It's the same with trigger volumes.
If the one that I'm wiggling here is a trigger as opposed to a collider, then as you pass through it
you'll say that's an on trigger enter.
And as you finally leave it just there, you'll get an on trigger exit event.
And we can use that to say, okay, when is Barre the snowboarder?
I'm just going to delete these guys don't need them anymore.
Thanks very much.
When is Barre, the snowboarder, touching the ground?
That's when his collider is touching with the ground.
Collider We know at the very moment that happens on trigger enter and then if Barre gets some sweet
air off one of the jumps, then we get an on collision exit moment.
So there's a little bit of theory here is the meaty challenge, I promise.
First of all, I'd like you to create a new script called Dust Trail.
Okay?
So we're going to put all of our logic in this new script, and then I want you to make particles that
can be scattered along behind the player.
So just a little simple particle system doesn't need to be super fancy.
And then here's the cool thing only play the particles when the player is touching the ground.
And this is the main meat of the challenge.
And if that's enough for you, if you're like, Yeah, I got this, Rick, no worries, then pause the
video.
Take that on if you'd like a couple of hints.
Here are some hints for you consider using on collision into 2D and on Collision Exit 2D.
The theory that I was just telling you about, I was telling it to you for a reason.
It wasn't just random pondering from an old man for collision events, for entering and exiting.
We can use other game object tag to find out what it is that we're colliding with now.
I think in the past when we were bumping into triggers, we only used other tag.
But if it's a collision event because we're getting information about the game object, that's what
other is all about.
Other game objects tag might be what you need to use and then consider using particle system stock.
We've used particle system play, so I'm pretty sure you can figure out what particle system stop is
because I know you're a clever cookie.
So there's our challenge.
It's a nice, meaty one.
Give it your best shot.
See you back here when you're done.
Okay.
So I'm going to make a particle system really quickly.
Might look a bit ugly, but click on Barry Right Click Effects Particle System.
I rename the particle system to be, say, dust particles.
I know they're not technically dust, but kind of the thing we're doing is pushing dust behind us.
Kind of snow, I guess.
But we'll call it dust, pop it in the back of where we want to come out.
And I'm just going to change the duration down to something quite small.
So point one, leave on looping.
That's fine because we want it to play, play, play, play until we tell it to stop, start lifetime.
They're going a little bit too far at the moment.
We'll put the Lifetime down to 0.5.
I'll do some variation later on, but for now we're just getting the macro amount start speed.
I think that's fine as well because I'll be moving away.
We want them to be kind of sticking around in the world emission.
I'm going to increase this to say 50.
I'm going to go find my renderer down the bottom, change the material, click on the little selector
to the square thingy.
That's good.
They're now too big.
So I'll reduce the size of this from one down to, say, 0.2.
Okay, so far, so good.
Now I'm going to change the simulation space from local to world.
So that means as we're cruising along, let me see if I can just simulate this for you here.
As we're cruising along, we want them to stay where we leave them in the world.
We don't want them to follow along with Barry.
If I was to have them on local as I move this around, you can see the entire particle system moves
around with me.
So I want to leave that in world space.
Let's click on play.
Just see if it's generally in the ballpark.
I don't want to finesse this too much at the moment because I want to get into the code, but let's
see so you can actually see it.
Okay.
Yeah.
So I mean, there's little particles flying along behind as we cruise along looks okay.
I'm going to change it.
So instead of it being in a emitted in a circle like this, let's change it the shape.
So change the shape to be a cone, I think.
And you can see it doesn't quite look like a cone.
Click on to DH.
You can see the cone is pointing into the screen into the Z or Z axis.
So if we change the the Y, yep.
There we go.
Now we can have it pointing backwards.
So I'm just going to do this at -90, you know, decrease the initial start point of it.
That would be the radius.
Drop that down a little bit, just kick it up a bit and we'll see how this is feeling.
Okay.
Not too bad.
I'm just going to change one last thing and then move on.
I'm going to change the start.
Speed down to say two.
There we go.
Some little dust particles.
Good enough for now.
We can finesse that a bit more later on.
But we've got something and it's called dust particles and it's sitting on Barry.
Next thing I want to do is to create my script.
So create C sharp and we'll call this dust trail.
You know, first of all, what I'll do is add dust trail where I want it to be added.
So I'm going to add dust trail as a script on Barry.
So I will scroll down and just drag dust trail onto Barry.
You might be saying, why not just put all of these things in the player controller?
Rick We could just shove all of our code in there.
You could do that.
But then it becomes a big old mess every time you go into the player controller script.
If you've got four or five or six or seven different things that it's doing, you're like, Where do
I find it?
What's what's doing what?
How does it work?
So if you've got these things as separate scripts, once you've set up the dust trail, it doesn't need
to get in the way anymore if you're doing work in the player, controller and so on.
So this is part of good coding practice is for each class or each script.
Try to have it, do just one thing, have one purpose to it.
Back to dust trail.
Now first thing I want to do is point to or get a reference for our particles.
So I'm going to serialize field and this will be particle system and the particle system I'm going to
call dust particles.
Now, pretty sure I'm not going to use start or update, so we'll delete those and I'm going to create
myself an on collision enter to DH just remove the private tidy up the formatting a little bit.
And you know, while I'm here, I'll also add my on collision exit to DX.
So we've got our shape all ready and raring.
I'm also going to jump over into my world, click on my level, sprite shape and tag that as ground.
It's already tagged as ground.
Oh, look at that.
We didn't need to have that as a clue that was already done.
So yeah, for us, I like looking at the visuals when I'm trying to figure out what the code is going
to do if the player is touching the ground play particles, if the player is not touching the ground,
don't play particles.
So we want to make sure, first of all, that we're touching the ground.
So if other remember we get the information about the other thing that we're touching are the dot game
object tag equals equals and then we want to have ground.
Make sure we spell that correctly.
Then what do we want to do?
We want to find our dust particles.
So dust particles and we want to play them.
Man There we go.
Parentheses, semicolon.
If we're touching the ground play and then if we exit, if we are not touching the ground.
So if other game object tag equals equals ground.
So in other words, if we've just gone and exited the ground, then what do we want to do?
Dust particles, dot stop and let's save that up and see if by some miracle I haven't forgotten a step.
What we do need to do is find Barry.
We've got our dust script, we've got our particle system.
We want to grab our dust particles, drag that over there.
So it's very clear this is what we want to be popping out behind us.
Make my screen a little bit bigger so we see what's going on here.
If we're touching the ground, do we have particles?
Okay, the particles started before we landed.
We might want to look at that.
And then we get some sweet air, but no particles whatsoever.
Get some speed on that.
Awesome.
So they're turning on and off based upon whether we're touching the ground just with a little bit of
simple logic there.
So hopefully you went okay with that challenge.
Let's do one last thing here where I'm going to click on the dust particles and we just need to turn
off the plate on a week.
We only want to have them play when we start touching the ground.
Let's have a look.
Touching the ground.
Okay.
So if you wanted to, you could go and finesse your particles a bit more.
I might do a little bit of that off camera, but not enough that you need to worry about just with that
finessing of it.
So good stuff.
I look forward to seeing you in the next lecture.