1
00:00:03,200 --> 00:00:08,990
In this lecture we're using foreign object of type to access our service effector to DX and to speed

2
00:00:08,990 --> 00:00:14,540
up our player woo so we can make our jumps a lot more interesting by having a boost in our game.

3
00:00:14,540 --> 00:00:15,890
So let's get started.

4
00:00:15,890 --> 00:00:18,260
Let's start by opening up our player controller script.

5
00:00:18,260 --> 00:00:23,970
Having a look in there, you can see that we've got a mechanism for getting a reference to a component

6
00:00:24,000 --> 00:00:26,420
that's called get component we're using in here.

7
00:00:26,420 --> 00:00:30,320
And this is when there's a component that is on the same game object.

8
00:00:30,320 --> 00:00:33,980
So player controller is on our barrier game object.

9
00:00:33,980 --> 00:00:36,380
You can see down here we've got our player controller.

10
00:00:36,380 --> 00:00:37,880
So we use get component.

11
00:00:37,880 --> 00:00:42,500
If we want to get any of the components we can see in here the rigid body or the transform, the capsule

12
00:00:42,500 --> 00:00:44,900
collider or the other script we've got on there.

13
00:00:44,900 --> 00:00:50,240
We can use get component and unity will know, aha, you're looking from within this same game object.

14
00:00:50,240 --> 00:00:54,230
But what if we have something that is on a different game object?

15
00:00:54,230 --> 00:00:59,210
So for example, here's Barry game object, here's our level sprite shape, different game object.

16
00:00:59,210 --> 00:01:04,340
And on this other game object, our level sprite sheet, we have these surface effects are 2D.

17
00:01:04,340 --> 00:01:05,840
So we want to be able to mess around with this.

18
00:01:05,840 --> 00:01:10,760
We want to increase or decrease the speed to increase or decrease our character.

19
00:01:10,760 --> 00:01:19,010
So the way that we can access that from one script or one game object, we can say find object of type

20
00:01:19,010 --> 00:01:25,250
and find object of type is good if you've only got one of those things in your scene.

21
00:01:25,250 --> 00:01:31,880
So we've only got one level sprite shape, which means we've only got one surface effector to DX.

22
00:01:31,910 --> 00:01:34,700
This particular approach wouldn't work if we had many of these.

23
00:01:34,700 --> 00:01:39,620
So if we're saying find object of type tree and we had a whole bunch of trees, then it would just go

24
00:01:39,620 --> 00:01:42,590
and grab the first one that it would find and it wouldn't be so effective.

25
00:01:42,590 --> 00:01:46,130
But we know in our scene we're only going to have one surface effector.

26
00:01:46,130 --> 00:01:52,340
So let's jump into our player controller script and we'll create ourselves a new variable up the top

27
00:01:52,340 --> 00:02:00,410
here, which will be of type surface effector to DX and we'll call it service effector to DX as a variable

28
00:02:00,410 --> 00:02:04,370
small s, so type largest variable small s.

29
00:02:04,400 --> 00:02:07,340
Now we need to say what's going to be stored in this variable.

30
00:02:07,340 --> 00:02:15,020
And to do that we'll start with surface effector to DX equals and we'll use find object of type.

31
00:02:15,020 --> 00:02:19,820
Now one thing you really need to be careful about is there's also a find objects of type.

32
00:02:19,820 --> 00:02:24,830
That's if we want to define a whole bunch of things or all the particular things of that type, and

33
00:02:24,830 --> 00:02:27,140
it has an S in there so that can trip you up.

34
00:02:27,140 --> 00:02:29,990
So we just want find object of type.

35
00:02:29,990 --> 00:02:33,320
We use our angle braces as we do for our get component.

36
00:02:33,320 --> 00:02:39,530
And within here we're looking for the surface effector to dx parentheses semicolon that will now when

37
00:02:39,530 --> 00:02:44,270
we click on play that will go and look for it'll snuffle around in our scene and say, Hey, I'm looking

38
00:02:44,270 --> 00:02:46,130
for a surface effector to DX.

39
00:02:46,160 --> 00:02:47,480
There it is right in there.

40
00:02:47,480 --> 00:02:52,700
And this stage you might be saying, Well, what's the difference between a component and a type?

41
00:02:52,700 --> 00:02:59,240
Because what we're looking for in here is fine object of type service effector to DX and components

42
00:02:59,240 --> 00:03:01,700
usually are a type as well.

43
00:03:01,700 --> 00:03:06,170
So when we add a component, it is a surface effect to 2D component.

44
00:03:06,170 --> 00:03:09,950
It is a thing that is of type surface effector to DX.

45
00:03:09,980 --> 00:03:11,390
That's how we can go and find it.

46
00:03:11,390 --> 00:03:16,580
So likewise, if we're looking to find something that is one of our scripts, if we jump back to Barry,

47
00:03:16,580 --> 00:03:21,650
if we're looking for a crash detector, we can look for the type of crash detector, because when we

48
00:03:21,650 --> 00:03:26,720
create this as a class, it is also making it of type crash detector.

49
00:03:26,720 --> 00:03:29,990
So with that logic, you could also look for something that is of type circle.

50
00:03:29,990 --> 00:03:32,300
Collider 2D or of type capsule.

51
00:03:32,300 --> 00:03:34,250
Collider 2D type, rigid body 2D.

52
00:03:34,250 --> 00:03:37,460
Those things will work as well if you're looking for find object of type.

53
00:03:37,460 --> 00:03:39,560
But for now we just want this service effector to DX.

54
00:03:39,590 --> 00:03:40,670
Now what are we going to do with it?

55
00:03:40,670 --> 00:03:44,600
Well, we want to be able to use this to speed it up and slow it down.

56
00:03:44,600 --> 00:03:46,760
First of all, I'm just going to tidy up our code a little bit.

57
00:03:46,760 --> 00:03:51,050
We've got this code in here that essentially is saying Rotate the player.

58
00:03:51,050 --> 00:03:55,010
So I'm going to create that as a new method that we will call within.

59
00:03:55,010 --> 00:03:55,580
Update.

60
00:03:55,580 --> 00:04:02,030
So I'm going to highlight all the way from our if statement down to where our curly braces are lined

61
00:04:02,030 --> 00:04:02,330
up.

62
00:04:02,330 --> 00:04:07,970
I'm going to hit control period or control full stop on the keyboard and then extract method.

63
00:04:07,970 --> 00:04:09,290
This will name it as new method.

64
00:04:09,290 --> 00:04:15,950
So double click on new method F two and I'm going to rename this to B Rotate player.

65
00:04:16,790 --> 00:04:20,720
So we don't have all these lines of code just sitting in update that makes update very messy.

66
00:04:20,720 --> 00:04:26,750
We want to have readability at all times, so rotate player, it's popped in the private keyword.

67
00:04:26,750 --> 00:04:28,370
I'm going to just remove the private keyword.

68
00:04:28,370 --> 00:04:29,450
We don't need that at the moment.

69
00:04:29,450 --> 00:04:30,440
We know that it's private.

70
00:04:30,440 --> 00:04:37,100
Next up, I want to create a new method which I will call respond to Boost.

71
00:04:38,420 --> 00:04:40,360
That's a way of saying we're going to boost the player.

72
00:04:40,370 --> 00:04:42,380
Now we've got the red squiggly line because it doesn't exist.

73
00:04:42,380 --> 00:04:49,190
So we could double click on it and then hit control period or control full stop and generate this new

74
00:04:49,190 --> 00:04:50,390
method which we will do.

75
00:04:50,420 --> 00:04:53,540
We don't want to extract, we want to generate a method.

76
00:04:53,540 --> 00:04:58,310
So we click on that and it creates the structure for us with a new method we don't need private.

77
00:04:58,340 --> 00:05:01,310
Once again, I've said that before and we've got respond to Boost.

78
00:05:01,310 --> 00:05:05,480
And then in here it's just a line that says, hey, you haven't put anything in there yet, so it's

79
00:05:05,480 --> 00:05:09,700
going to throw an exception just to remind us that we haven't put any code in, so we'll delete that.

80
00:05:09,710 --> 00:05:12,800
Now you could have just gone and typed void respond to boost.

81
00:05:12,800 --> 00:05:13,340
Curly brace.

82
00:05:13,340 --> 00:05:13,850
Curly brace.

83
00:05:13,850 --> 00:05:14,890
You could do that if you want to.

84
00:05:14,900 --> 00:05:18,950
You didn't have to go and do the little technique I showed you with the control full stop or control,

85
00:05:18,950 --> 00:05:19,430
period.

86
00:05:19,430 --> 00:05:23,030
Now I'm going to give you a little bit of framework in here and then challenge you to finish things

87
00:05:23,030 --> 00:05:23,270
off.

88
00:05:23,270 --> 00:05:32,510
I'll just put a comment which is saying, if we push up, then speed up, otherwise stay at normal speed.

89
00:05:32,510 --> 00:05:33,860
That's what we're trying to do in here.

90
00:05:33,860 --> 00:05:37,250
So that's going to require if statement, it's going to require our input.

91
00:05:37,250 --> 00:05:40,700
It's a little bit similar to rotate player, but that's what we're trying to aim for here.

92
00:05:40,700 --> 00:05:45,050
I'm going to give you a little bit of a hint with a couple of variables we create up at the top.

93
00:05:45,050 --> 00:05:48,410
I'll pop this under our serialised field float talk amount.

94
00:05:48,410 --> 00:05:56,060
I'll make a serialize field which will be of type float as well, and we'll name this boost speed and

95
00:05:56,060 --> 00:06:03,500
I'll initialize that at I think 30 F and then we'll make ourselves another serialized field also of

96
00:06:03,500 --> 00:06:10,490
Type Float and we will call this base speed and we'll initialize that at probably 20.

97
00:06:10,490 --> 00:06:14,570
If base speed is well, that's how fast we want to go when we're not boosting and boost.

98
00:06:14,570 --> 00:06:18,110
Speed is while you got it how fast we want to go when we are boosting.

99
00:06:18,110 --> 00:06:20,000
So we've got all the structure in here.

100
00:06:20,030 --> 00:06:30,470
The last little hint I'll give you is we need to access the surface effect two D and change its speed.

101
00:06:30,470 --> 00:06:31,220
So there we go.

102
00:06:31,220 --> 00:06:33,050
There's some clues for you.

103
00:06:33,050 --> 00:06:33,980
So here's your challenge.

104
00:06:33,980 --> 00:06:35,660
Make the player boost.

105
00:06:35,660 --> 00:06:37,820
Finish our code so that you can boost.

106
00:06:37,820 --> 00:06:43,130
In other words, speed up your player by increasing the speed of our effector when the player is pushing

107
00:06:43,130 --> 00:06:44,480
up on the keyboard.

108
00:06:44,480 --> 00:06:49,040
So you'll need an if statement and an else statement, you'll need to get the player input.

109
00:06:49,040 --> 00:06:53,510
So you know when the player is pushing up on the keyboard and you'll need to change the speed of the

110
00:06:53,510 --> 00:06:54,000
effector.

111
00:06:54,020 --> 00:06:54,980
So this is your challenge.

112
00:06:54,980 --> 00:06:58,010
Take that on C back here when you've given it your best shot.

113
00:07:01,480 --> 00:07:01,900
Okay.

114
00:07:01,900 --> 00:07:03,360
So we've got a few clues in here.

115
00:07:03,370 --> 00:07:06,340
Let's start with the sort of shape we've got down below.

116
00:07:06,400 --> 00:07:17,740
If we've got input, dot get key just as we're doing down below and key code dot up arrow think we'll

117
00:07:17,740 --> 00:07:18,010
use.

118
00:07:18,010 --> 00:07:21,760
So just as we've done down below, what do we want to do if that's the case.

119
00:07:21,760 --> 00:07:28,060
Well, curly braces in this instance, we want to be modifying or changing something to do with our

120
00:07:28,060 --> 00:07:29,890
service effector to D.

121
00:07:29,890 --> 00:07:34,510
So I didn't give you all the clues on this, but if you go to your surface effect the two RD and have

122
00:07:34,510 --> 00:07:38,680
a look here, you can see that there is a field which is called speed.

123
00:07:38,680 --> 00:07:39,910
So we want to change the speed.

124
00:07:39,910 --> 00:07:42,880
We want to do exactly as we're doing with our mouse and keyboard here.

125
00:07:42,880 --> 00:07:44,440
But we do this in our code.

126
00:07:44,440 --> 00:07:50,530
So our variable surface effect, the 2d dot and I believe there's a speed excellent.

127
00:07:50,530 --> 00:07:56,440
That's going to equal what will it equal if we're pushing up, it will equal our boost speed, boost

128
00:07:56,440 --> 00:07:58,930
speed semicolon.

129
00:07:58,930 --> 00:08:04,480
I could have just had that equal throw in here so I could have gone 30 F like that or just 30.

130
00:08:04,480 --> 00:08:08,230
But then if we want to change it, we have to come and find the exact spot in our code.

131
00:08:08,230 --> 00:08:13,540
So by having it serialized, we can find that within our inspector, which makes life a lot easier.

132
00:08:13,540 --> 00:08:18,220
So if we're pushing up, speed up, but if we just do that once we push up once we'll, you know, will

133
00:08:18,220 --> 00:08:19,180
stay sped up.

134
00:08:19,180 --> 00:08:23,230
So we need to say else, which is another way of saying otherwise.

135
00:08:23,320 --> 00:08:25,570
And in this instance, we don't need to say else.

136
00:08:25,570 --> 00:08:28,960
If we're just saying if you're doing that, do a thing.

137
00:08:28,960 --> 00:08:34,330
Otherwise the default, the rest of the time, we want you to do something else so we don't need to

138
00:08:34,330 --> 00:08:40,659
say ls if the player isn't pushing up arrow, we're just saying otherwise do this thing, prevent our

139
00:08:40,659 --> 00:08:48,910
curly braces and we will set our surface effects, speed back to our base speed and then semicolon.

140
00:08:48,910 --> 00:08:56,050
I'll just remove my notes got down here make sure that we're calling respond to boost within update.

141
00:08:56,050 --> 00:09:00,070
Yes we are click on save back over into unity.

142
00:09:00,070 --> 00:09:05,110
What I'm going to do just to test that this is working is I'm going to make my speed crazy fast.

143
00:09:05,110 --> 00:09:10,480
I find that's an easier way as opposed to being like, am I sped up so I'm going to make it 100 in my

144
00:09:10,480 --> 00:09:11,470
inspector here.

145
00:09:11,590 --> 00:09:12,730
Let's click on play.

146
00:09:13,670 --> 00:09:15,110
Cruising, cruising, cruising speed.

147
00:09:15,110 --> 00:09:15,680
Whoa!

148
00:09:15,690 --> 00:09:16,130
Woo!

149
00:09:16,130 --> 00:09:18,470
I think we can conclude that that is working now.

150
00:09:18,470 --> 00:09:22,420
We'll pop this back to 30 and we'll see if that's the right value for us to have.

151
00:09:22,430 --> 00:09:24,860
We want to make sure that when we're pushing, we speed up.

152
00:09:24,860 --> 00:09:26,660
When we stop pushing, we slow down again.

153
00:09:26,660 --> 00:09:28,220
Yes, we are definitely doing that.

154
00:09:28,220 --> 00:09:29,990
And does it help going over that jump?

155
00:09:30,200 --> 00:09:32,420
Oh, that was so excellent.

156
00:09:32,420 --> 00:09:33,470
That's exactly what I wanted.

157
00:09:33,470 --> 00:09:36,590
So now you can have the player speed up and do some sweet jumps.

158
00:09:36,590 --> 00:09:37,130
Woo!

159
00:09:37,130 --> 00:09:42,230
And I think for me, I'm going to leave it that the player can just push speed whenever they want to.

160
00:09:42,260 --> 00:09:48,080
They can boost whenever they want to, as opposed to having some sort of time or or some sort of amount

161
00:09:48,080 --> 00:09:48,920
like boost amount.

162
00:09:48,920 --> 00:09:53,270
But this is something as you get more confident, you can come back to this project and you can put

163
00:09:53,270 --> 00:09:54,560
in all sorts of mechanisms.

164
00:09:54,560 --> 00:09:58,160
You can have some visualization on your screen of how much boost you've got left.

165
00:09:58,160 --> 00:10:00,230
And when you boost, then it decreases.

166
00:10:00,230 --> 00:10:01,730
So there's a lot you can do with this.

167
00:10:01,730 --> 00:10:04,190
You can push yourself, you can challenge yourself to take it further.

168
00:10:04,190 --> 00:10:09,290
But for now, I'm pretty happy with this implementation where we've used find objective type to access

169
00:10:09,290 --> 00:10:14,990
the service effector, which is on a different game object from the game object that we're listening

170
00:10:14,990 --> 00:10:18,020
to, the input from our player so that we go, That's all for this lecture.

171
00:10:18,020 --> 00:10:19,400
I will see you in the next one.

