1
00:00:03,160 --> 00:00:07,600
We've got the functionality for loading our scene, but at the moment it's a little bit jarring for

2
00:00:07,600 --> 00:00:07,990
the player.

3
00:00:07,990 --> 00:00:10,870
If you crash and bump instantly, you're teleported back.

4
00:00:10,870 --> 00:00:11,470
And we don't want that.

5
00:00:11,500 --> 00:00:15,610
We want to have a little bit of a delay so that we can potentially play some particle effects or do

6
00:00:15,610 --> 00:00:16,120
something else.

7
00:00:16,120 --> 00:00:20,410
Likewise, when we cross the finishing line, we want to have the player have a moment of, Oh, I actually

8
00:00:20,410 --> 00:00:22,090
finished the level and one this.

9
00:00:22,090 --> 00:00:24,260
So how do we go about creating a delay?

10
00:00:24,280 --> 00:00:30,640
Well, we've got two useful approaches we can use when we want to say, wait a moment in our game,

11
00:00:30,640 --> 00:00:35,590
one of those is Invoke, which will be using in this lecture and the other is CO routines which will

12
00:00:35,590 --> 00:00:37,380
be learning later on in the course.

13
00:00:37,390 --> 00:00:42,190
Invoke is a little bit easier to understand, it's more straightforward to use.

14
00:00:42,190 --> 00:00:45,730
So we're going to use, invoke and when we use invoke, we need to pass in.

15
00:00:45,730 --> 00:00:52,060
So we need to tell it the name of the method we want to call after a delay, as long as how long the

16
00:00:52,060 --> 00:00:56,320
delay is and we write it like this invoke this is the name of the method parentheses.

17
00:00:56,320 --> 00:01:01,900
And then within our quotation marks we have our name of the method comma and then whatever the delay

18
00:01:01,900 --> 00:01:02,710
might be.

19
00:01:02,710 --> 00:01:04,599
So let's go set up the structure for this.

20
00:01:04,599 --> 00:01:09,550
I'm going to jump back over into finish line in our script.

21
00:01:09,550 --> 00:01:16,300
And the place we want to put this is in here after we have our if this is the player.

22
00:01:16,300 --> 00:01:17,320
So we've triggered something.

23
00:01:17,320 --> 00:01:19,300
We've bumped into something on the finishing line.

24
00:01:19,300 --> 00:01:21,940
We're saying if it's the player, then do a thing.

25
00:01:21,940 --> 00:01:23,590
Well, currently we've got our code here.

26
00:01:23,590 --> 00:01:27,370
It's not a method, but we can turn this into a method.

27
00:01:27,370 --> 00:01:33,700
So what I'm going to do is just down below, above the very final curly brace, but below the one before

28
00:01:33,730 --> 00:01:33,940
that.

29
00:01:33,940 --> 00:01:37,750
I'm going to create ourselves a new method type in void, and then we'll give it a name.

30
00:01:37,750 --> 00:01:38,470
What are we doing here?

31
00:01:38,470 --> 00:01:41,170
Well, we're reloading the scene, so let's call it what we're doing.

32
00:01:41,170 --> 00:01:48,190
Reload scene as a method, name, parentheses, and then put in our curly braces.

33
00:01:48,190 --> 00:01:53,890
We're just going grab this code from in here, but cut that out and then paste it within our reload

34
00:01:53,890 --> 00:01:54,520
scene.

35
00:01:54,610 --> 00:01:57,460
Paste We're going to call the method in here using invoke.

36
00:01:57,460 --> 00:01:58,690
So invoke.

37
00:01:58,690 --> 00:02:04,930
And then in our quotations we're going to call this method reload scene and see because it's a string

38
00:02:04,930 --> 00:02:07,090
reference, we don't get any nice autocomplete.

39
00:02:07,090 --> 00:02:09,460
It's not saying, Hey, I think you want to type reload scene.

40
00:02:09,460 --> 00:02:13,360
It's just saying go for it and hey, do make sure you don't mess up the typing.

41
00:02:13,360 --> 00:02:14,050
That's all cool.

42
00:02:14,050 --> 00:02:18,400
We're happy to do it this way because we understand the risks and we're willing to take them on like

43
00:02:18,400 --> 00:02:22,510
rebels that we are comma and we'll set ourselves a 2/2 delay.

44
00:02:22,510 --> 00:02:25,120
So it's really, really clear that we're getting this delay.

45
00:02:25,120 --> 00:02:27,470
Save it up and click on play.

46
00:02:27,500 --> 00:02:31,300
Make sure you don't make the mistake I made just a second ago that I edited out of the video where I

47
00:02:31,300 --> 00:02:33,740
was wondering why was there not a delay when I crashed?

48
00:02:33,740 --> 00:02:34,810
And I'm like, you know what?

49
00:02:34,810 --> 00:02:37,570
You actually put that on the finishing line, you fall.

50
00:02:37,570 --> 00:02:40,300
Okay, one, two, and then bump reloads.

51
00:02:40,300 --> 00:02:42,010
Okay, so we've got our delay in there.

52
00:02:42,010 --> 00:02:42,970
That is awesome.

53
00:02:42,970 --> 00:02:44,530
Two is probably a little bit too much.

54
00:02:44,530 --> 00:02:46,450
We want to go and tune that at the moment.

55
00:02:46,450 --> 00:02:49,300
We've got a magic number, so this is buried within our code.

56
00:02:49,300 --> 00:02:52,450
If we're like that level loading is taking too long.

57
00:02:52,450 --> 00:02:53,170
I want to change that.

58
00:02:53,170 --> 00:02:54,520
We want to put that in the inspector.

59
00:02:54,520 --> 00:02:58,780
So that's going to be one of the bits of a challenge for you is to finish out invoke instead of using

60
00:02:58,780 --> 00:02:59,710
a magic number.

61
00:02:59,710 --> 00:03:05,500
At the moment I've got to just shoved into my code serialize a variable and use that to represent our

62
00:03:05,500 --> 00:03:11,230
delay so that we can tweak that in the inspector and then also set up invoke with a serialized variable

63
00:03:11,230 --> 00:03:14,320
of course for our crash detection as well.

64
00:03:14,320 --> 00:03:18,430
I'll probably put my crash detection at half a second, maybe a second, but we'll play around with

65
00:03:18,430 --> 00:03:18,610
it.

66
00:03:18,610 --> 00:03:19,660
So there's your challenge.

67
00:03:19,670 --> 00:03:21,460
Take that on and see you back here when you're done.

68
00:03:21,460 --> 00:03:22,600
Righty o.

69
00:03:22,600 --> 00:03:28,090
So first step, let's give ourselves a member variable which will serialize so serialize field.

70
00:03:28,090 --> 00:03:29,320
This will be a float.

71
00:03:29,320 --> 00:03:33,370
We know it's a float because the invoke is saying, Hey, I want you to give me a float.

72
00:03:33,370 --> 00:03:37,180
So float will call this load delay.

73
00:03:37,180 --> 00:03:42,070
You can call it whatever you want, just make sure it makes sense to you and we'll initialize that at,

74
00:03:42,070 --> 00:03:46,090
I think 1/2 for our loading for our finish line stuff.

75
00:03:46,090 --> 00:03:51,520
And then within here, just need to add load delay as our variable save that.

76
00:03:51,520 --> 00:03:54,310
We don't need to check that because I'm confident it's going to work.

77
00:03:54,370 --> 00:03:55,600
Famous last words.

78
00:03:55,600 --> 00:03:58,450
I'm going to go over to crash detector, put in the code for this.

79
00:03:58,450 --> 00:04:05,830
So we're going to type this out, which will be void reload scene, same stuff, same code.

80
00:04:05,830 --> 00:04:10,780
I could have copy pasted it reload scene, but I'm going to type it in and practice my typing.

81
00:04:10,780 --> 00:04:17,740
And then in here it's going to be scene manager load scene and then paste and now we're going to add

82
00:04:17,740 --> 00:04:19,450
invoke just as we did before.

83
00:04:19,450 --> 00:04:21,519
Parentheses, semicolon.

84
00:04:21,550 --> 00:04:23,800
This will be reload scene.

85
00:04:23,800 --> 00:04:25,360
Make sure it's exactly the same.

86
00:04:25,360 --> 00:04:26,650
What's the delay that we want?

87
00:04:26,650 --> 00:04:31,090
Well, we'll do another load delay as our variable and then semicolon.

88
00:04:31,090 --> 00:04:32,770
We haven't created load delay yet.

89
00:04:32,770 --> 00:04:36,250
So up at the top we're going to serialize field.

90
00:04:36,280 --> 00:04:41,560
There will be a float and it'll be called load delay and we'll initialize that.

91
00:04:41,560 --> 00:04:43,150
This is for when you're bonking your head isn't it.

92
00:04:43,150 --> 00:04:50,170
Well initialize that as I think 0.5 if we don't want to have too long, we're like, I crash long enough

93
00:04:50,170 --> 00:04:53,350
to play a particle effect and then take us back to the start.

94
00:04:53,350 --> 00:04:57,430
So we'll save, make sure all these things work, click on the play.

95
00:04:57,850 --> 00:05:00,880
I'll try crossing the finishing line first and see if that resets.

96
00:05:00,880 --> 00:05:02,320
And then if I'm a little bit.

97
00:05:02,600 --> 00:05:03,170
Coordinated.

98
00:05:03,170 --> 00:05:07,550
I might even accidentally crash with a group and then cross the line.

99
00:05:07,550 --> 00:05:08,920
One, two.

100
00:05:08,930 --> 00:05:09,350
Yes.

101
00:05:09,350 --> 00:05:09,950
Excellent.

102
00:05:09,950 --> 00:05:13,280
And then let me hit my head bonk half a second.

103
00:05:13,280 --> 00:05:13,670
Great.

104
00:05:13,670 --> 00:05:14,120
There we go.

105
00:05:14,120 --> 00:05:18,800
So we've added a little delay that gives us a lot of flexibility where we could play a sound effect,

106
00:05:18,800 --> 00:05:23,240
we could play some particle effects, we could do all manner of things after you have that moment.

107
00:05:23,240 --> 00:05:28,970
And of course, we can use invoke to delay calling a method in many places in our game, not just reloading

108
00:05:28,970 --> 00:05:29,420
a scene.

109
00:05:29,420 --> 00:05:33,620
So it's a really good tool for you to have in your tool kit now knowing that you can add this small

110
00:05:33,620 --> 00:05:34,010
delay.

111
00:05:34,010 --> 00:05:34,910
So good stuff.

112
00:05:34,910 --> 00:05:36,590
I'll see you in the next lecture.

