In this lecture, we're going to hook up in the code so that when Barry crashes or crosses the finish
line, we trigger a boom.
Our particle effects.
The first thing to do is open up.
Hmm.
Let's see.
We'll work on finish line together, and then I'll set you the challenge of working on the crash detector
yourself.
So we have finish line open.
What we need to do, first of all, is create a reference for the particle system.
So what do I mean by that?
Well, when we tell unity to, hey, you bumped your head, play this particular thing, it's going
to say to us, okay, Rick, what particular thing do you want me to play?
So in this case, what I'm going to set up the reference is by jumping into my finish line script and
saying or typing serialised field and I'm going to create a reference to a particle system and that
particle system I am going to call finish effect semicolon.
And so if we save this, when we jump back into unity, we find where our finishing line script is.
It's on our finish line game object.
Now you can see we have this finish effect option.
So I'm going to grab the finish effect particle effect, drag that into my finish effect.
Now back over into our script.
Now that it's knows this finish effect, that's the variable that's storing our particle system, we
can trigger it at the right point.
The way we do that is on trigger enter.
Yep.
When we bump into something or when something bumps into us.
Sorry if it's the player we want to invoke our load seen with our bit of a delay.
And that's simply a case of accessing our particle effect.
So finish effect which we know is a particle system dot and then we've got a few options in here.
If you scroll through, you'll see one that is pretty aptly name, which is just play and then parentheses
because it's a method and semicolon.
It's as simple as that saying, Hey, play this particle effect, let's save that up and see if we have
any unexpected consequences of doing this.
I can think of one we might have now when I click on play.
Does anything happen at the start?
Yeah.
Boom.
We had a finish line effect before we even got there.
That's cool.
I'm glad that happened.
When we click on Finish Effect, you can see there's a setting down here which is play on Awake.
We want to uncheck that.
If we have play on Awake, then literally when we click the play button, it will say, Oh, I'm ready
to go and it'll play the particle system.
We don't want to do that.
So now when I click on play, let's see if this is going to work for us.
That didn't zoom, zoom, zoom.
And then as we cross the finish line, kaboom.
Okay, it's playing it in the scene view, but it's not playing it in the game view.
Why is that?
Well, my guess is if we click on the finish effect, scroll all the way down, find our order in layer,
set that to 100 just to make sure that's not going to be a thing at all.
Okay, so let's scroll.
You see the play on effect for that other particle effect.
Kaboom in the world is playing in our scene view, but not our world view.
It's a bit weird.
Has got me a little bit confused.
Let's see if we can figure this out.
Okay, so I've had a little bit of a poke around.
I think I know what the issue here is.
We click on the 2D button or click to change from 2D to 3D.
You can see that our particle effect is I don't even know where that is.
You know, it's behind the camera so we can see it when we're in scene view, but we can't see it when
we're in game view.
So the reason that's happening is our finish line has let's see the finish line, Z position or Z position
is at -35 and our particle effect is at zero.
So the particle effect is right on top.
There's some weirdness going on there.
So let's just sort that out.
Let's go to the finishing line.
Let's make this zero for the Z value.
And then you can see that the post is way off in the background.
I think those ones are.
So my apologies.
I didn't zero those out when we created those.
So you click on post, make the z value zero, click on top, make that zero as well.
Let's click on the finish effect.
It's at zero, the finish line.
So it's all at zero.
Nothing is being way off in the distance or the other side of the camera.
Now when we click on play, we should see that.
But we cruise along kind of.
We'd looking at it over that other side.
Yes, it's in front.
Boom, and then we reload.
Excellent.
So there we go.
We've got that figured out.
Job well done.
Click back on 2D to go back into 2D mode.
I'm going to save that up and I'm going to issue you a challenge.
The challenge is to trigger our crash particles so when the player crashes, trigger the crash particles
to play and make sure you pay attention to things like the order to sort all that out, as well as making
sure that there's no weirdness like play on Awake, for example.
So that's a challenge.
Take that on.
So you back here when you're done.
Okey dokey.
So the first thing I'm going to do because I noticed it before was click on Barre, click on the crash
effect.
And as soon as I click on it, we get this play on a wake.
So I'm going to turn off play on a wake bump.
So not playing when we are awaking.
Now I'm going to jump into my crash detector script and once again I'm going to serialize a variable
serialize field.
And this will be once again particle system and we will call this crash effect semicolon.
Now, where are we going to play this?
Well, we're going to play it when we bump into something.
You could do this before your invoke or after your invoke.
It doesn't matter so much.
But you do want to ask yourself, is there going to be a difference if I do a thing before an other
thing?
In this case, playing the particle system, I think is a good thing to come first.
Before we say, now, I'd like you to load the scene, but it shouldn't make a big impact, but we'll
pop it first nonetheless.
So we're going to say crash effect, dot, play, parentheses, semicolon and save jump back to unity.
Let's just have a look at our order.
So Barry's on zero border, top and bottom run zero, crash effect zero.
So that should all be fine.
Good stuff.
Let's do a dump.
Dump, and then we'll just spin backwards and crash.
Nothing happened.
Okay, the crash variable.
Okay, this is cool.
So we've seen an exception and we haven't talked about this a lot, but this is something you might
be seeing in your code from time to time.
Down the bottom you see our little red thing pop up.
So we go to the console that will give us more information as saying the variable crash effect of crash
detector is not been a That's because Rick forgot to go back over into the Barry script, the crash
detector script, see how we've got crash effect.
I need a drag crash effect right onto there to say this is now referencing that I made such a big deal
about references before than I went and forgot to reference this.
Now when we click on play it actually knows what to trigger when we crash.
So crash bump.
There we go.
Okay, one thing you'll notice as we move, the particle effect is moving with us.
Do we want that?
Do we want the the crunch to be following or staying in the world?
I think I want to try having it stay in the world.
So if I crash just here, then the crash particle stages there rather than flowing along with me.
So we'll just try that.
We'll see what works better.
If you click on Crash Effect, there's a setting in here which is simulation space.
You see it right near the top or right near the top of all of our modules, simulation space, change
that from local to world, which means that the particle effects will stay wherever they are in the
world.
They won't continue to travel along locally, in other words, locally with Barry.
So let's see if that look is any different.
Kind of might be the same, to be honest.
So we'll go crash kind of stays in the world and but for now I think that's fine.
Okay so we're now triggering our particle effects when we go through the finish line and when we go
bump.
And you've learned how to use the play method when it comes to particle systems.
Okay.
I'll see you in the next lecture.