1
00:00:00,000 --> 00:00:06,390
Welcome back my fellow game developers to a brand new extremely long video.

2
00:00:06,390 --> 00:00:08,250
I know you're sick of that.

3
00:00:08,250 --> 00:00:13,940
But as always, it's extremely worth it because we learn so much.

4
00:00:13,935 --> 00:00:16,385
First of all, we create an enemy.

5
00:00:16,380 --> 00:00:18,090
We add a range to it.

6
00:00:18,090 --> 00:00:20,490
We add a way to draw that range.

7
00:00:20,490 --> 00:00:24,280
And we also run the game. We can shoot the enemy.

8
00:00:24,275 --> 00:00:25,985
Obviously he doesn't die for now,

9
00:00:25,985 --> 00:00:28,135
but we can get into the range.

10
00:00:28,130 --> 00:00:31,060
You can see right here the player is in Chase range.

11
00:00:31,055 --> 00:00:33,205
It's being sent every single time.

12
00:00:33,200 --> 00:00:35,860
If we get out of this little bubble, there you go,

13
00:00:35,855 --> 00:00:38,935
you see that the player is out of range.

14
00:00:38,930 --> 00:00:40,900
So let's not waste any more time.

15
00:00:40,895 --> 00:00:44,905
And let's get started with this long video.

16
00:00:44,900 --> 00:00:50,450
Oh, so it's time for some enemies.

17
00:00:50,450 --> 00:00:51,970
Let's get started.

18
00:00:51,965 --> 00:00:54,775
The first thing that we're going to do is we're going

19
00:00:54,770 --> 00:00:57,800
to right-click create an empty game object.

20
00:00:57,800 --> 00:01:00,950
Make sure that its position is reset.

21
00:01:00,950 --> 00:01:03,370
And the rotation and scale and et cetera.

22
00:01:03,365 --> 00:01:10,375
And we'll just call this the enemy for now until we decide which enemy will want to use.

23
00:01:10,370 --> 00:01:13,700
Next thing we'll need to do is we'll need to go into the sprites.

24
00:01:13,699 --> 00:01:19,119
We're going to right-click create a folder in here for the enemies.

25
00:01:19,115 --> 00:01:23,125
And inside of the enemies will go into the resources and

26
00:01:23,120 --> 00:01:27,290
we are going to drag in the monster and creatures.

27
00:01:27,290 --> 00:01:29,660
And let's just put it in here.

28
00:01:29,660 --> 00:01:33,170
So we'll wait for everything to import. We'll open this up.

29
00:01:33,170 --> 00:01:35,780
And as you can see, we have four types of enemies

30
00:01:35,780 --> 00:01:38,590
that I've downloaded from open game arts.

31
00:01:38,585 --> 00:01:40,225
Of course, as always,

32
00:01:40,220 --> 00:01:42,560
you can go find your own enemies.

33
00:01:42,560 --> 00:01:47,470
These are the ones that I've used and they will be in the link, in the resources.

34
00:01:47,465 --> 00:01:49,225
And what should I choose?

35
00:01:49,220 --> 00:01:51,580
I think I'm going to choose four now,

36
00:01:51,575 --> 00:01:54,875
the, let's see which one.

37
00:01:54,874 --> 00:01:57,144
I really haven't thought about this.

38
00:01:57,140 --> 00:01:58,420
The enemies.

39
00:01:58,415 --> 00:02:01,375
Let's just choose the skeleton and be very generic.

40
00:02:01,370 --> 00:02:05,740
So as you can see, we have attack three death either et cetera, et cetera.

41
00:02:05,735 --> 00:02:09,055
So I'm just going to go into the idle.

42
00:02:09,050 --> 00:02:13,390
I'm going to make it multiple Sprite Editor apply.

43
00:02:13,385 --> 00:02:15,955
And let's go ahead and slice it up,

44
00:02:15,950 --> 00:02:18,590
not automatically, but by a grid size.

45
00:02:18,590 --> 00:02:22,610
And if you were a bit confused by what I did last time,

46
00:02:22,610 --> 00:02:26,990
I just check the length of the entire sprite.

47
00:02:26,990 --> 00:02:31,310
So it's 600 and I divide it by how many skeletons we have.

48
00:02:31,310 --> 00:02:34,700
So we have four or one single row,

49
00:02:34,700 --> 00:02:37,060
so divided by four.

50
00:02:37,055 --> 00:02:41,215
That means it's 150 by 150, as always.

51
00:02:41,210 --> 00:02:43,190
For some reason, I'm surprised.

52
00:02:43,190 --> 00:02:46,570
So we go into the splice, slice.

53
00:02:46,565 --> 00:02:50,905
Splice, think that's movie splice, sure, slice.

54
00:02:50,900 --> 00:02:52,620
There we go. Apply.

55
00:02:52,624 --> 00:02:55,894
Now we have our little skeletons right here.

56
00:02:55,895 --> 00:02:57,475
I'm going to drag it.

57
00:02:57,470 --> 00:02:59,440
And the scene, whoa,

58
00:02:59,435 --> 00:03:01,835
that's a huge skeleton, isn't it?

59
00:03:01,835 --> 00:03:03,335
That's irony.

60
00:03:03,335 --> 00:03:04,645
If you haven't picked it up.

61
00:03:04,640 --> 00:03:07,610
Anyways, we'll make it 50. There we go.

62
00:03:07,610 --> 00:03:13,420
We still can't see him because we need to have and apply.

63
00:03:13,415 --> 00:03:21,875
Let's go ahead and create a layer in here which will be the enemy or enemies.

64
00:03:21,875 --> 00:03:24,445
And I'm going set the bullets to render in front of

65
00:03:24,440 --> 00:03:27,820
the enemy and also the player to render a plot of the enemy.

66
00:03:27,815 --> 00:03:30,655
And let's go ahead and set the enemy right here.

67
00:03:30,650 --> 00:03:31,760
So there you go.

68
00:03:31,760 --> 00:03:34,300
It's still a very small enemy.

69
00:03:34,295 --> 00:03:36,775
Let's save that. Let's make him a bit bigger.

70
00:03:36,770 --> 00:03:39,800
So an idle 50, Let's make them 32.

71
00:03:39,800 --> 00:03:41,260
Apply that.

72
00:03:41,255 --> 00:03:43,135
Maybe even bigger.

73
00:03:43,130 --> 00:03:46,930
Let's make him 16. So 16.

74
00:03:46,925 --> 00:03:47,785
Yep. There we go.

75
00:03:47,780 --> 00:03:48,110
Okay.

76
00:03:48,110 --> 00:03:50,000
That's too big.

77
00:03:50,000 --> 00:03:53,740
When D maybe 20.

78
00:03:53,735 --> 00:03:56,345
Yeah, I think 20 is good enough.

79
00:03:56,345 --> 00:03:59,195
That's changed this two points. See if that helps.

80
00:03:59,195 --> 00:04:01,555
Yep, there we go. He looks a lot better,

81
00:04:01,550 --> 00:04:02,720
still a bit grainy,

82
00:04:02,720 --> 00:04:04,240
but no worries about that.

83
00:04:04,235 --> 00:04:06,235
We have him for free.

84
00:04:06,230 --> 00:04:07,760
Anyways.

85
00:04:07,760 --> 00:04:10,010
Let's see, this is the body.

86
00:04:10,010 --> 00:04:12,940
This would be the body.

87
00:04:12,935 --> 00:04:16,165
And I'm going to child it to the enemy game object.

88
00:04:16,160 --> 00:04:19,610
So now we have the enemy right here and the body.

89
00:04:19,610 --> 00:04:22,090
Let's just set him to 00.

90
00:04:22,085 --> 00:04:23,755
The position is relative,

91
00:04:23,750 --> 00:04:27,470
of course to the enemy game objects or now we move around

92
00:04:27,470 --> 00:04:31,580
using the enemy and you can see that he is rendered behind the player,

93
00:04:31,580 --> 00:04:38,380
behind the gun and he should be also rendered behind the bullets. Go right.

94
00:04:38,375 --> 00:04:39,845
Now that we have this,

95
00:04:39,845 --> 00:04:42,205
next thing we need to do is obviously at

96
00:04:42,200 --> 00:04:46,750
some behavior or a way to control the behavior of our enemy.

97
00:04:46,745 --> 00:04:51,005
So we're going to go into the assets, the scripts,

98
00:04:51,005 --> 00:04:52,735
we're going to right-click in here,

99
00:04:52,730 --> 00:04:55,490
create a new folder maybe,

100
00:04:55,490 --> 00:04:58,200
and let's call it the enemies.

101
00:04:58,204 --> 00:04:59,314
Enter.

102
00:04:59,315 --> 00:05:03,875
Maybe also create one in here for the player,

103
00:05:03,875 --> 00:05:06,085
which will be the player controller.

104
00:05:06,080 --> 00:05:12,100
And let's see, we'll also create one for the bullets and time now.

105
00:05:12,095 --> 00:05:16,295
So create a new folder for the bullets.

106
00:05:16,295 --> 00:05:19,675
And we are going to move these into the bullet.

107
00:05:19,670 --> 00:05:20,000
Why?

108
00:05:20,000 --> 00:05:21,470
Because we have the bullet,

109
00:05:21,470 --> 00:05:24,670
the player bullet controller and the impact effect.

110
00:05:24,665 --> 00:05:28,255
So and inside of the enemies will right-click,

111
00:05:28,250 --> 00:05:30,080
create a new C Sharp script,

112
00:05:30,080 --> 00:05:31,820
and we'll call it the

113
00:05:31,820 --> 00:05:36,460
And EMI controller hit Enter.

114
00:05:36,455 --> 00:05:39,745
Wait for this to compile. Okay, great.

115
00:05:39,740 --> 00:05:41,150
And now in the enemy,

116
00:05:41,150 --> 00:05:46,630
we can go ahead and add a component which is the enemy controller.

117
00:05:46,625 --> 00:05:48,955
And we're going to open up the script.

118
00:05:48,950 --> 00:05:54,640
Okay? So something you might have noticed that all the tabs are gone.

119
00:05:54,635 --> 00:05:56,425
That's not because I closed them,

120
00:05:56,420 --> 00:05:59,210
That's because we've changed the directory of them.

121
00:05:59,210 --> 00:06:00,650
So we moved, for example,

122
00:06:00,650 --> 00:06:06,260
the player controller from the folder scripts into the folder player scripts.

123
00:06:06,260 --> 00:06:07,400
That's why we don't have it,

124
00:06:07,400 --> 00:06:08,590
but don't worry, it's still,

125
00:06:08,585 --> 00:06:11,585
if we double-click, we can easily open it up.

126
00:06:11,585 --> 00:06:12,805
Let's continue on.

127
00:06:12,800 --> 00:06:16,780
What do we need in here inside of the NMI controller?

128
00:06:16,775 --> 00:06:18,565
While we need, first of all,

129
00:06:18,560 --> 00:06:22,130
a variable that will keep in check the enemies speed.

130
00:06:22,130 --> 00:06:27,140
And we will need a variable which will represent the enemies rigid body.

131
00:06:27,140 --> 00:06:30,050
So I'm going to do just that.

132
00:06:30,050 --> 00:06:31,840
These are very simple things.

133
00:06:31,835 --> 00:06:34,085
No need for a lot of explanation.

134
00:06:34,085 --> 00:06:35,935
This is a serialized field,

135
00:06:35,930 --> 00:06:39,610
a float, and the enemy speed.

136
00:06:39,605 --> 00:06:45,395
And another variable which will be a private rigid body 2D.

137
00:06:45,395 --> 00:06:46,955
And I just remembered.

138
00:06:46,954 --> 00:06:51,694
And EMI, rigid body.

139
00:06:51,695 --> 00:06:54,275
The body doesn't need to be capital.

140
00:06:54,275 --> 00:06:55,495
Save that.

141
00:06:55,490 --> 00:06:58,790
And I just remember that we forgot obviously to

142
00:06:58,790 --> 00:07:02,540
add a couple of things to the enemy, which is here.

143
00:07:02,540 --> 00:07:07,810
We are going to add a component which will be a box collider.

144
00:07:07,805 --> 00:07:12,905
And let's go ahead and make it.

145
00:07:12,905 --> 00:07:14,995
So maybe without the shield,

146
00:07:14,990 --> 00:07:20,860
usually it's better to keep the box colliders a bit smaller than the actual body.

147
00:07:20,855 --> 00:07:25,795
This will make the player of your game wanted to become more accurate.

148
00:07:25,790 --> 00:07:27,610
And we can also add the shadow, by the way,

149
00:07:27,605 --> 00:07:30,875
under the enemy, which is a fairly good idea.

150
00:07:30,875 --> 00:07:33,005
I'm going to add another component,

151
00:07:33,005 --> 00:07:35,725
which will be the rigid body 2D.

152
00:07:35,720 --> 00:07:40,850
Obviously we are going to make the gravity scale 0.

153
00:07:40,850 --> 00:07:43,120
Save that, and we'll close both,

154
00:07:43,115 --> 00:07:44,285
both of these down.

155
00:07:44,285 --> 00:07:48,305
Move the script under the two components,

156
00:07:48,305 --> 00:07:49,735
or we can just leave it there.

157
00:07:49,730 --> 00:07:51,500
What's the speed of the enemy?

158
00:07:51,500 --> 00:07:54,170
What's the speed of the player first, okay,

159
00:07:54,170 --> 00:07:58,000
It's then, let's make the enemy speed 5.5 of that.

160
00:07:57,995 --> 00:07:59,485
Save that. Okay, great.

161
00:07:59,480 --> 00:08:01,520
So now we have the enemy.

162
00:08:01,520 --> 00:08:04,220
We have a rigid body and a collider on him

163
00:08:04,220 --> 00:08:07,040
so we can shoot him and interact with him physically.

164
00:08:07,040 --> 00:08:12,340
Now what you want to do is we want to start moving the enemy towards the player,

165
00:08:12,335 --> 00:08:13,775
but not all the time,

166
00:08:13,775 --> 00:08:17,095
only when the player gets close enough.

167
00:08:17,090 --> 00:08:21,080
So we need a variable for the range at

168
00:08:21,080 --> 00:08:25,250
which the player will be at an order for the enemy to stop chasing him.

169
00:08:25,250 --> 00:08:30,940
And we also need the direction in which the enemy will be moving.

170
00:08:30,935 --> 00:08:32,885
So first thing first,

171
00:08:32,885 --> 00:08:35,425
we're going to create a serialized field,

172
00:08:35,420 --> 00:08:41,750
which will be a float with the player or the player chase range,

173
00:08:41,750 --> 00:08:44,890
let's call it the player chains range.

174
00:08:44,885 --> 00:08:49,855
So the range which the player should be to start chasing him.

175
00:08:49,850 --> 00:08:54,320
And we'll create a private factor three.

176
00:08:54,320 --> 00:09:03,980
And this will be the direction to move in the direction to move and save that.

177
00:09:03,980 --> 00:09:05,060
Okay, great.

178
00:09:05,060 --> 00:09:08,030
Let's go back in here.

179
00:09:08,030 --> 00:09:11,480
And what's the chase range? And you know what?

180
00:09:11,480 --> 00:09:12,890
Let's keep it for later on.

181
00:09:12,890 --> 00:09:16,970
And I'll show you a very nifty way of how we can draw.

182
00:09:16,970 --> 00:09:20,930
So how are we going to know what the chase ranges?

183
00:09:20,930 --> 00:09:22,130
So I can, for example,

184
00:09:22,130 --> 00:09:26,590
start making the change range five or ten, or 21.

185
00:09:26,585 --> 00:09:27,845
But what does it mean?

186
00:09:27,845 --> 00:09:30,235
What does it mean the chase range in here.

187
00:09:30,230 --> 00:09:33,500
So do we have to every time calculate these little boxes

188
00:09:33,500 --> 00:09:36,890
in here to determine how far or close declare is.

189
00:09:36,890 --> 00:09:38,770
Well, of course not.

190
00:09:38,765 --> 00:09:41,225
What we're going to do is we're going to draw

191
00:09:41,225 --> 00:09:44,935
a circle in here that will only appear in the scene mode,

192
00:09:44,930 --> 00:09:47,570
but not inside of the game mode.

193
00:09:47,570 --> 00:09:49,550
How are we going to do this?

194
00:09:49,550 --> 00:09:55,010
If we go back and here we have something called the draw gizmos.

195
00:09:55,010 --> 00:09:57,460
And you can find that in the Unity documentation.

196
00:09:57,455 --> 00:09:59,065
And the here we go,

197
00:09:59,060 --> 00:10:02,770
you can see that we have something under the mono behavior,

198
00:10:02,765 --> 00:10:05,095
which is onDraw gizmo select.

199
00:10:05,090 --> 00:10:08,150
And you can see right here implement the onDraw gets selected to

200
00:10:08,150 --> 00:10:11,680
draw gizmo object is selected.

201
00:10:11,675 --> 00:10:16,675
Notice what we said, if the object is selected and you can see right here,

202
00:10:16,670 --> 00:10:20,090
we have the gizmos test and how we can use it.

203
00:10:20,090 --> 00:10:22,390
So we create a void onDraw gizmo.

204
00:10:22,385 --> 00:10:28,675
And in here we can create the gizmo color so we select which color we want to be drawn.

205
00:10:28,670 --> 00:10:30,590
And what do we want to draw,

206
00:10:30,590 --> 00:10:32,150
which is the sphere.

207
00:10:32,150 --> 00:10:36,860
So we can click on the troposphere and we can see it right here,

208
00:10:36,860 --> 00:10:38,710
which is the public draws fear,

209
00:10:38,705 --> 00:10:42,695
draws a solid sphere with center and a radius.

210
00:10:42,695 --> 00:10:44,915
So let's implement this.

211
00:10:44,915 --> 00:10:52,975
And use it to draw our own circle or sphere around the enemy and know the range of it.

212
00:10:52,970 --> 00:10:57,470
And if we look closely at the method and here we can see that draws fear,

213
00:10:57,470 --> 00:11:00,440
we need a center and we need a radius.

214
00:11:00,440 --> 00:11:02,240
So what should the radius be?

215
00:11:02,240 --> 00:11:05,990
Think about it while we create this method.

216
00:11:05,990 --> 00:11:13,540
So in here, we're going to say on draw gizmos selected.

217
00:11:13,535 --> 00:11:16,465
And we are first of all going to choose a color.

218
00:11:16,460 --> 00:11:20,960
So it's going to be the gizmos dot color

219
00:11:20,960 --> 00:11:25,450
and it's going to be equal to the color, let's say red.

220
00:11:25,445 --> 00:11:27,145
Save that.

221
00:11:27,140 --> 00:11:35,120
Next we are going to access the gizmos dot Draw wire, sphere.

222
00:11:35,120 --> 00:11:40,450
So why are sphere look at that draws a wireframe sphere with a center and the radius.

223
00:11:40,445 --> 00:11:42,355
So it's a bit different than the sphere,

224
00:11:42,350 --> 00:11:45,910
but because we are playing in 2D,

225
00:11:45,905 --> 00:11:47,875
we don't need to draw the entire sphere,

226
00:11:47,870 --> 00:11:50,050
we just need the wire around it.

227
00:11:50,045 --> 00:11:53,155
And let's see what should be the center.

228
00:11:53,150 --> 00:11:56,900
The center obviously should be the transform dot positions or

229
00:11:56,900 --> 00:12:01,360
transport is the transport of the object which has the enemy controller.

230
00:12:01,355 --> 00:12:03,295
I know I've said it a million times,

231
00:12:03,290 --> 00:12:05,830
but for those of you that are still confused,

232
00:12:05,825 --> 00:12:09,505
I know that I was a bit confused when I when I started doing

233
00:12:09,500 --> 00:12:13,690
this a couple of years ago, a lot of years ago.

234
00:12:13,685 --> 00:12:16,655
And I remember, so where does this come from?

235
00:12:16,655 --> 00:12:21,055
Well, it's the transform of the object that has the enemy controller on it,

236
00:12:21,050 --> 00:12:24,010
which means it's this one, V enemy.

237
00:12:24,005 --> 00:12:28,375
This is the transformer talking about back in here.

238
00:12:28,370 --> 00:12:29,930
What is the radius?

239
00:12:29,930 --> 00:12:31,700
Well, have you thought about it?

240
00:12:31,700 --> 00:12:33,700
What should be the radius?

241
00:12:33,695 --> 00:12:37,565
Obviously, it's the player chase range.

242
00:12:37,565 --> 00:12:39,305
We are going to save that.

243
00:12:39,305 --> 00:12:42,505
We are going to go back into Unity.

244
00:12:42,500 --> 00:12:46,760
And well, whereas the circle you might be asking, well,

245
00:12:46,760 --> 00:12:50,660
if you remember a couple of videos ago we turned off the gizmos.

246
00:12:50,660 --> 00:12:51,890
Maybe you already saw it,

247
00:12:51,890 --> 00:12:55,390
you didn't notice, but if we click it, there we go.

248
00:12:55,385 --> 00:12:56,665
And why did we turn it off?

249
00:12:56,660 --> 00:13:00,520
Because we had this camera and annoying icon of the camera.

250
00:13:00,515 --> 00:13:03,085
What we can do is we can open the gizmos.

251
00:13:03,080 --> 00:13:06,590
You can see that we can deselect a lot of things in here,

252
00:13:06,590 --> 00:13:11,030
but one of the most important ones is we can make these icons are a bit smaller.

253
00:13:11,030 --> 00:13:12,470
So there you go.

254
00:13:12,470 --> 00:13:16,810
We no longer see the annoying camera go, correct?

255
00:13:16,805 --> 00:13:19,965
So look at that big, huge circle.

256
00:13:19,969 --> 00:13:21,409
Let's make it a bit smaller.

257
00:13:21,410 --> 00:13:24,820
And you can see that it immediately changes.

258
00:13:24,815 --> 00:13:26,855
I'm going to make it yea big.

259
00:13:26,855 --> 00:13:28,675
I'm going to set the player there.

260
00:13:28,670 --> 00:13:32,780
And the next thing that we need to do is we need to calculate the distance

261
00:13:32,780 --> 00:13:37,180
between the player and the enemy and see if it's less than the range.

262
00:13:37,175 --> 00:13:40,115
If it's less, we can start chasing the player.

263
00:13:40,115 --> 00:13:41,825
If it's not less than,

264
00:13:41,825 --> 00:13:43,915
we do not chase the player.

265
00:13:43,910 --> 00:13:47,300
So what's the first step to do?

266
00:13:47,300 --> 00:13:49,190
Well, we have the range,

267
00:13:49,190 --> 00:13:52,300
we have the transform position of the enemy.

268
00:13:52,295 --> 00:13:55,735
The next thing we need is a reference to the player.

269
00:13:55,730 --> 00:13:59,080
How are we going to get the reference of the player?

270
00:13:59,075 --> 00:14:07,245
Well, we first of all need to understand a method that I'm going to show you,

271
00:14:07,245 --> 00:14:11,835
which is the object dot find objects of type.

272
00:14:11,830 --> 00:14:13,600
And what this does,

273
00:14:13,600 --> 00:14:19,450
the object t means that it depends on what the object you are looking for.

274
00:14:19,450 --> 00:14:21,190
So when you find the object,

275
00:14:21,190 --> 00:14:23,860
d becomes that type of object.

276
00:14:23,860 --> 00:14:29,860
So the object, the first act of loaded object that matches the specific type.

277
00:14:29,860 --> 00:14:36,720
So it returns the first loaded object of the type types or you can give it your type.

278
00:14:36,715 --> 00:14:38,265
And if we look here,

279
00:14:38,260 --> 00:14:39,730
we can see a couple of examples.

280
00:14:39,730 --> 00:14:43,960
So let's say we want to find the Canvas renderer.

281
00:14:43,960 --> 00:14:45,880
If you don't know what the Canvas renderer is,

282
00:14:45,880 --> 00:14:48,600
don't worry, we will look into it later on.

283
00:14:48,595 --> 00:14:51,785
But for now, look the Canvas renderer.

284
00:14:51,785 --> 00:14:56,905
We create a canvas renderer pipe variable with the name Canvas,

285
00:14:56,900 --> 00:15:00,740
and we use finite object of type and we get the canvas render.

286
00:15:00,740 --> 00:15:03,410
So now we get a reference to it.

287
00:15:03,410 --> 00:15:05,770
And because we only have one player,

288
00:15:05,765 --> 00:15:06,955
we can use this.

289
00:15:06,950 --> 00:15:08,770
If we had multiple players,

290
00:15:08,765 --> 00:15:10,595
we couldn't have because,

291
00:15:10,595 --> 00:15:14,675
well, obviously we find only the first act of one.

292
00:15:14,675 --> 00:15:17,375
So look this method up,

293
00:15:17,375 --> 00:15:20,935
find it, study it, understand it.

294
00:15:20,930 --> 00:15:24,140
And then you will have to do this challenge.

295
00:15:24,140 --> 00:15:27,170
You'll need to find the player object.

296
00:15:27,170 --> 00:15:31,610
So create a variable of type transformed to represent the player we want to

297
00:15:31,610 --> 00:15:38,300
chase and start use find objects of type 2, find the player.

298
00:15:38,300 --> 00:15:41,470
The type should be the player controller.

299
00:15:41,465 --> 00:15:46,655
Why? Because we need to find some specific type of the player.

300
00:15:46,655 --> 00:15:49,295
So how do we identify the player?

301
00:15:49,295 --> 00:15:51,425
He's just like all the other objects.

302
00:15:51,424 --> 00:15:52,824
What makes him unique?

303
00:15:52,820 --> 00:15:57,260
It's not just the name you can find through the name or the body or the on.

304
00:15:57,260 --> 00:15:59,520
We need to use something specific.

305
00:15:59,524 --> 00:16:03,354
And that's something specific is the player controller script.

306
00:16:03,350 --> 00:16:09,920
That's why the type of the object you want to find is the player controller.

307
00:16:09,920 --> 00:16:13,780
And finally use dot transform on the object you find and

308
00:16:13,775 --> 00:16:17,975
assign it to the variable we've created, y dot transform.

309
00:16:17,975 --> 00:16:22,435
You should know this because the type that we are going to create is the transform.

310
00:16:22,430 --> 00:16:23,720
Why is it transform?

311
00:16:23,720 --> 00:16:25,640
Well, because we want to get the distance.

312
00:16:25,639 --> 00:16:26,929
How do we get the distance?

313
00:16:26,930 --> 00:16:29,270
Well, obviously because it's a transform.

314
00:16:29,270 --> 00:16:31,310
So pause the video right now.

315
00:16:31,310 --> 00:16:34,200
Go do the challenge.

316
00:16:34,210 --> 00:16:37,220
Okay, so first thing, first,

317
00:16:37,220 --> 00:16:45,430
we are going to create a private transform and the player to chase.

318
00:16:45,434 --> 00:16:47,684
Now inside of start,

319
00:16:47,680 --> 00:16:56,160
we are going to say that the player to chase is equal to find objects of type.

320
00:16:56,155 --> 00:17:00,615
And the type that we are looking for is the player controller.

321
00:17:00,610 --> 00:17:04,990
And when we do find him because declared of two Chase as a transform,

322
00:17:04,990 --> 00:17:09,340
we're just going to access the transform immediately from me.

323
00:17:09,340 --> 00:17:11,100
So I hope you did the challenge.

324
00:17:11,095 --> 00:17:12,795
I hope it wasn't too hard.

325
00:17:12,790 --> 00:17:15,190
I know there was a bit of tricks in here,

326
00:17:15,190 --> 00:17:18,640
even though it's the first time you use find object of type,

327
00:17:18,640 --> 00:17:21,480
I'm sure that you were at least capable

328
00:17:21,475 --> 00:17:24,805
of doing this part if you couldn't have figured out this,

329
00:17:24,805 --> 00:17:29,525
don't worry about it was due time, will understand everything.

330
00:17:29,525 --> 00:17:35,195
But now, the next step is to go ahead and calculate the distance.

331
00:17:35,195 --> 00:17:39,265
So I'm going to create first of all, an if statement.

332
00:17:39,260 --> 00:17:43,250
And then I'm going to write vector 3 dot.

333
00:17:43,249 --> 00:17:46,549
And you can see that we have a method in here which is the distance and

334
00:17:46,550 --> 00:17:50,110
it returns the distance between a and B.

335
00:17:50,105 --> 00:17:52,615
So we're going to open this up.

336
00:17:52,610 --> 00:17:54,050
What is the a?

337
00:17:54,050 --> 00:17:59,750
It's going to be the transform dot position. And what is the B?

338
00:17:59,750 --> 00:18:03,960
It's going to be the player to chase dot position.

339
00:18:03,964 --> 00:18:05,154
There you go.

340
00:18:05,150 --> 00:18:10,060
And what is the condition if the distance between the transformed position,

341
00:18:10,055 --> 00:18:12,325
which is the enemy and the player to chase,

342
00:18:12,320 --> 00:18:18,520
which is the player and it's less than V player chase range.

343
00:18:18,515 --> 00:18:22,235
Then we are going to be moving our enemy.

344
00:18:22,235 --> 00:18:24,205
But that is for another video.

345
00:18:24,200 --> 00:18:27,710
For now, I'm just going to use the debug dot log.

346
00:18:27,710 --> 00:18:34,600
And I'm going to say player is in Chase range.

347
00:18:34,604 --> 00:18:41,894
And an else statement which we'll say that duplicate this line, move it down.

348
00:18:41,890 --> 00:18:44,160
And it's going to say,

349
00:18:44,155 --> 00:18:52,515
player out of Chase is out of chase range.

350
00:18:52,510 --> 00:18:56,000
Save that back in our game.

351
00:18:57,960 --> 00:19:02,640
Let's know what I just want to make something.

352
00:19:02,635 --> 00:19:05,475
I'll make this public just for now,

353
00:19:05,470 --> 00:19:07,870
just so we can see what happens.

354
00:19:07,870 --> 00:19:10,600
So you'll notice that on the enemy right

355
00:19:10,600 --> 00:19:13,440
now we have a player to chase, but there is nothing.

356
00:19:13,435 --> 00:19:15,435
We are not going to add anything.

357
00:19:15,430 --> 00:19:18,280
We're just going to run the game. There you go.

358
00:19:18,275 --> 00:19:21,085
You can see that now the doom player, if I click on it,

359
00:19:21,080 --> 00:19:23,440
there is the reference and now the skeleton,

360
00:19:23,435 --> 00:19:26,395
our enemy has a reference to the player.

361
00:19:26,390 --> 00:19:27,770
I can even shoot him.

362
00:19:27,770 --> 00:19:29,870
So I'm going to go into the console.

363
00:19:29,870 --> 00:19:31,550
What is this false?

364
00:19:31,550 --> 00:19:32,360
Let's double-click.

365
00:19:32,360 --> 00:19:36,620
Okay, So this one and player is out of chase range.

366
00:19:36,620 --> 00:19:38,890
Let me set the game window next to

367
00:19:38,885 --> 00:19:43,165
the scene when those so we can see clearly the circle in here.

368
00:19:43,160 --> 00:19:46,100
So if I come close enough,

369
00:19:46,100 --> 00:19:50,350
There we go, you can see that now player is in Chase range.

370
00:19:50,345 --> 00:19:53,575
If I go out, player is out of range.

371
00:19:53,570 --> 00:19:56,530
If I go and play as NJ strange,

372
00:19:56,525 --> 00:19:59,585
there you go, everything is working perfectly fine.

373
00:19:59,585 --> 00:20:01,045
So I hope you enjoy it.

374
00:20:01,040 --> 00:20:04,120
And the next video will be actually moving our enemy,

375
00:20:04,115 --> 00:20:06,745
not just printing something to the debug, to the log.

376
00:20:06,740 --> 00:20:08,740
Let me remove this debug dot log.

377
00:20:08,735 --> 00:20:09,505
Save that.

378
00:20:09,500 --> 00:20:13,120
Let me make this private again because we don't need to see it.

379
00:20:13,115 --> 00:20:14,755
I just wanted to demonstrate.

380
00:20:14,750 --> 00:20:17,620
Don't forget to stage all your files.

381
00:20:17,615 --> 00:20:19,735
And before I go,

382
00:20:19,730 --> 00:20:21,860
What did I also want to tell you all.

383
00:20:21,860 --> 00:20:22,990
I hope you enjoy it.

384
00:20:22,985 --> 00:20:24,695
See you next time.

