We've got the functionality for loading our scene, but at the moment it's a little bit jarring for
the player.
If you crash and bump instantly, you're teleported back.
And we don't want that.
We want to have a little bit of a delay so that we can potentially play some particle effects or do
something else.
Likewise, when we cross the finishing line, we want to have the player have a moment of, Oh, I actually
finished the level and one this.
So how do we go about creating a delay?
Well, we've got two useful approaches we can use when we want to say, wait a moment in our game,
one of those is Invoke, which will be using in this lecture and the other is CO routines which will
be learning later on in the course.
Invoke is a little bit easier to understand, it's more straightforward to use.
So we're going to use, invoke and when we use invoke, we need to pass in.
So we need to tell it the name of the method we want to call after a delay, as long as how long the
delay is and we write it like this invoke this is the name of the method parentheses.
And then within our quotation marks we have our name of the method comma and then whatever the delay
might be.
So let's go set up the structure for this.
I'm going to jump back over into finish line in our script.
And the place we want to put this is in here after we have our if this is the player.
So we've triggered something.
We've bumped into something on the finishing line.
We're saying if it's the player, then do a thing.
Well, currently we've got our code here.
It's not a method, but we can turn this into a method.
So what I'm going to do is just down below, above the very final curly brace, but below the one before
that.
I'm going to create ourselves a new method type in void, and then we'll give it a name.
What are we doing here?
Well, we're reloading the scene, so let's call it what we're doing.
Reload scene as a method, name, parentheses, and then put in our curly braces.
We're just going grab this code from in here, but cut that out and then paste it within our reload
scene.
Paste We're going to call the method in here using invoke.
So invoke.
And then in our quotations we're going to call this method reload scene and see because it's a string
reference, we don't get any nice autocomplete.
It's not saying, Hey, I think you want to type reload scene.
It's just saying go for it and hey, do make sure you don't mess up the typing.
That's all cool.
We're happy to do it this way because we understand the risks and we're willing to take them on like
rebels that we are comma and we'll set ourselves a 2/2 delay.
So it's really, really clear that we're getting this delay.
Save it up and click on play.
Make sure you don't make the mistake I made just a second ago that I edited out of the video where I
was wondering why was there not a delay when I crashed?
And I'm like, you know what?
You actually put that on the finishing line, you fall.
Okay, one, two, and then bump reloads.
Okay, so we've got our delay in there.
That is awesome.
Two is probably a little bit too much.
We want to go and tune that at the moment.
We've got a magic number, so this is buried within our code.
If we're like that level loading is taking too long.
I want to change that.
We want to put that in the inspector.
So that's going to be one of the bits of a challenge for you is to finish out invoke instead of using
a magic number.
At the moment I've got to just shoved into my code serialize a variable and use that to represent our
delay so that we can tweak that in the inspector and then also set up invoke with a serialized variable
of course for our crash detection as well.
I'll probably put my crash detection at half a second, maybe a second, but we'll play around with
it.
So there's your challenge.
Take that on and see you back here when you're done.
Righty o.
So first step, let's give ourselves a member variable which will serialize so serialize field.
This will be a float.
We know it's a float because the invoke is saying, Hey, I want you to give me a float.
So float will call this load delay.
You can call it whatever you want, just make sure it makes sense to you and we'll initialize that at,
I think 1/2 for our loading for our finish line stuff.
And then within here, just need to add load delay as our variable save that.
We don't need to check that because I'm confident it's going to work.
Famous last words.
I'm going to go over to crash detector, put in the code for this.
So we're going to type this out, which will be void reload scene, same stuff, same code.
I could have copy pasted it reload scene, but I'm going to type it in and practice my typing.
And then in here it's going to be scene manager load scene and then paste and now we're going to add
invoke just as we did before.
Parentheses, semicolon.
This will be reload scene.
Make sure it's exactly the same.
What's the delay that we want?
Well, we'll do another load delay as our variable and then semicolon.
We haven't created load delay yet.
So up at the top we're going to serialize field.
There will be a float and it'll be called load delay and we'll initialize that.
This is for when you're bonking your head isn't it.
Well initialize that as I think 0.5 if we don't want to have too long, we're like, I crash long enough
to play a particle effect and then take us back to the start.
So we'll save, make sure all these things work, click on the play.
I'll try crossing the finishing line first and see if that resets.
And then if I'm a little bit.
Coordinated.
I might even accidentally crash with a group and then cross the line.
One, two.
Yes.
Excellent.
And then let me hit my head bonk half a second.
Great.
There we go.
So we've added a little delay that gives us a lot of flexibility where we could play a sound effect,
we could play some particle effects, we could do all manner of things after you have that moment.
And of course, we can use invoke to delay calling a method in many places in our game, not just reloading
a scene.
So it's a really good tool for you to have in your tool kit now knowing that you can add this small
delay.
So good stuff.
I'll see you in the next lecture.