1
00:00:00,000 --> 00:00:03,180
Welcome back, my fellow
game developers.

2
00:00:03,179 --> 00:00:04,669
In this video, we added

3
00:00:04,665 --> 00:00:06,755
a shooting range.
It's pretty simple.

4
00:00:06,750 --> 00:00:07,920
We've already done this.

5
00:00:07,920 --> 00:00:10,380
You can see right now
the enemy no longer

6
00:00:10,380 --> 00:00:13,040
shoots us unless we stop
in this red circle.

7
00:00:13,035 --> 00:00:14,175
When we do stuff in,

8
00:00:14,175 --> 00:00:17,125
he starts throwing
his sorts arrows.

9
00:00:17,120 --> 00:00:19,570
If we're out, he
does not throw them.

10
00:00:19,565 --> 00:00:21,745
Now, let's not
waste anymore time.

11
00:00:21,740 --> 00:00:24,050
This video isn't just
about creating the range.

12
00:00:24,050 --> 00:00:26,230
We also had an extra,

13
00:00:26,225 --> 00:00:28,465
let's call it lesson.

14
00:00:28,460 --> 00:00:31,210
And here, an extra
point to create.

15
00:00:31,205 --> 00:00:33,385
We use the knowledge
of our methods.

16
00:00:33,380 --> 00:00:35,030
So let's not waste any more time

17
00:00:35,030 --> 00:00:37,640
talking and let's get started.

18
00:00:37,640 --> 00:00:43,780
Oh, okay, so time to add a
shooting range to our enemy.

19
00:00:43,775 --> 00:00:45,295
But before we do that,

20
00:00:45,290 --> 00:00:46,910
there is one thing
that we need to

21
00:00:46,910 --> 00:00:48,560
take care of and that is that

22
00:00:48,560 --> 00:00:50,330
the sword wasn't getting

23
00:00:50,330 --> 00:00:53,570
destroyed when it hit
some kind of trigger.

24
00:00:53,570 --> 00:00:55,050
And you might be thinking,

25
00:00:55,054 --> 00:00:56,844
well, why is that happening?

26
00:00:56,840 --> 00:00:58,640
Even though we have
the enemy bullet

27
00:00:58,640 --> 00:01:00,370
on trigger, everything
is working.

28
00:01:00,365 --> 00:01:02,245
There is a small detail that

29
00:01:02,240 --> 00:01:04,840
I should have told you
I made the mistake.

30
00:01:04,835 --> 00:01:08,125
We do need a rigid
body 2D in here,

31
00:01:08,120 --> 00:01:10,540
but we need to
make it kinematic.

32
00:01:10,535 --> 00:01:13,815
So now when the flying salt

33
00:01:13,819 --> 00:01:16,459
has a rigid body to
the end Is Kinematic,

34
00:01:16,460 --> 00:01:18,140
it will be destroyed
when it hits

35
00:01:18,140 --> 00:01:19,880
a wall with that out of the way.

36
00:01:19,880 --> 00:01:21,890
Excuse me for not
saying that before I

37
00:01:21,890 --> 00:01:24,170
told you you shouldn't
have a rigid body.

38
00:01:24,170 --> 00:01:27,500
We should, but it
should be kinematic.

39
00:01:27,500 --> 00:01:30,310
So let's continue
on. What do we need?

40
00:01:30,305 --> 00:01:32,995
We need to have the
ability to show

41
00:01:32,990 --> 00:01:36,120
the player only at
a certain range.

42
00:01:36,124 --> 00:01:37,374
How do we do that?

43
00:01:37,370 --> 00:01:39,820
By giving you a challenge?

44
00:01:39,815 --> 00:01:41,795
And your challenge is to create

45
00:01:41,795 --> 00:01:44,005
a shooting range with color.

46
00:01:44,000 --> 00:01:47,630
So create a float variable
for the shooting range.

47
00:01:47,630 --> 00:01:48,950
Before we can shoot,

48
00:01:48,950 --> 00:01:51,710
make sure we are in the
shooting range by checking

49
00:01:51,710 --> 00:01:53,180
the distance between
the enemy and

50
00:01:53,180 --> 00:01:55,550
the player and draw a yellow.

51
00:01:55,550 --> 00:01:56,810
I'm not sure why I said the

52
00:01:56,810 --> 00:01:58,580
yellow because we
already have yellow.

53
00:01:58,580 --> 00:02:00,770
Choose whatever color
you want and make sure

54
00:02:00,770 --> 00:02:03,020
that that is the shooting range.

55
00:02:03,020 --> 00:02:04,910
Makes sure that the
enemy doesn't shoot

56
00:02:04,910 --> 00:02:08,350
unless the player is in
that shooting range.

57
00:02:08,345 --> 00:02:13,615
So pause the video right now
and go do the challenge.

58
00:02:13,610 --> 00:02:15,770
Okay, welcome back.

59
00:02:15,770 --> 00:02:19,300
So I'm going to go into
the enemy controller.

60
00:02:19,295 --> 00:02:22,345
And where do we
shoot? There we go.

61
00:02:22,340 --> 00:02:24,580
This is the time between shots.

62
00:02:24,575 --> 00:02:27,835
And let's go ahead and added.

63
00:02:27,830 --> 00:02:30,500
I think this is a
good place to add it.

64
00:02:30,500 --> 00:02:32,950
So I'm going to create
a serialized field,

65
00:02:32,945 --> 00:02:35,915
which will be a float,

66
00:02:35,915 --> 00:02:41,465
and it's going to be
the shooting range.

67
00:02:41,465 --> 00:02:42,515
Save that.

68
00:02:42,515 --> 00:02:45,455
And then I'm going to
scroll down in here.

69
00:02:45,455 --> 00:02:47,845
I'm going to check if we are not

70
00:02:47,840 --> 00:02:50,650
a male attacker and we
are ready to shoot,

71
00:02:50,645 --> 00:02:52,295
then I'm going to go ahead

72
00:02:52,295 --> 00:02:56,225
and up here right before we
check for the condition,

73
00:02:56,225 --> 00:02:59,885
create a variable which
is going to be the float.

74
00:02:59,885 --> 00:03:02,905
Or know what, let's not
create a, an extra variable.

75
00:03:02,900 --> 00:03:04,720
Let's just put it in here.

76
00:03:04,715 --> 00:03:06,295
We'll add another condition.

77
00:03:06,290 --> 00:03:07,970
And that condition
will be that a

78
00:03:07,970 --> 00:03:10,400
vector three and calculate

79
00:03:10,400 --> 00:03:15,860
the distance between
the layer to chase.

80
00:03:15,860 --> 00:03:18,100
There we go the player to chase

81
00:03:18,095 --> 00:03:21,745
dot transform dot position.

82
00:03:21,740 --> 00:03:23,480
And we are going to check for

83
00:03:23,480 --> 00:03:27,710
the transform dot
position of the enemy.

84
00:03:27,710 --> 00:03:31,030
And if it's less than
the shooting range,

85
00:03:31,025 --> 00:03:33,415
then we are going to
start the condition.

86
00:03:33,410 --> 00:03:36,100
Now you've noticed that
this line is a bit long.

87
00:03:36,095 --> 00:03:39,065
So what we can do
is simply go this,

88
00:03:39,065 --> 00:03:41,455
hit enter, go in
here, hit Enter.

89
00:03:41,450 --> 00:03:43,820
So now they are on
separate lines.

90
00:03:43,820 --> 00:03:45,920
We can see them more clearly.

91
00:03:45,920 --> 00:03:47,930
If you want, you can keep
them on the same line.

92
00:03:47,930 --> 00:03:50,960
And this is a better way of
looking at the conditions.

93
00:03:50,960 --> 00:03:54,830
We can also add this right
here so we know everything.

94
00:03:54,830 --> 00:03:57,170
With that done, we're
going to save oh,

95
00:03:57,170 --> 00:03:59,260
and we need to draw
the longest most.

96
00:03:59,255 --> 00:04:05,085
So I'm going to go ahead and
duplicate these two lines.

97
00:04:05,089 --> 00:04:07,969
And I'm going to make
the color not yellow.

98
00:04:07,970 --> 00:04:09,290
Let's make it blue.

99
00:04:09,290 --> 00:04:13,070
And this is going to
be the shooting range.

100
00:04:13,070 --> 00:04:15,940
Save that back in our game.

101
00:04:15,935 --> 00:04:18,415
Let's see the enemy.

102
00:04:18,410 --> 00:04:20,920
Now, what do you want the
shooting range to be?

103
00:04:20,915 --> 00:04:23,735
Of course, it will depend
on how the game will be.

104
00:04:23,735 --> 00:04:25,015
So for example, you can make

105
00:04:25,010 --> 00:04:26,800
the shooting range a bit bigger,

106
00:04:26,795 --> 00:04:29,305
but maybe just maybe
you would want

107
00:04:29,300 --> 00:04:34,300
the player chase to
be around like that.

108
00:04:34,295 --> 00:04:38,135
So the enemy might need to
chase the player a bit.

109
00:04:38,135 --> 00:04:41,755
And when he is in the
range he started shooting,

110
00:04:41,750 --> 00:04:43,730
or we can do whatever you want.

111
00:04:43,730 --> 00:04:44,960
It depends on your game.

112
00:04:44,960 --> 00:04:47,200
Will just add the slide here,

113
00:04:47,195 --> 00:04:50,695
circled labeled bigger,
save that, run the game.

114
00:04:50,690 --> 00:04:53,300
And now we should see
that the enemy doesn't

115
00:04:53,300 --> 00:04:56,270
shoot until we get an range.

116
00:04:56,270 --> 00:04:57,380
So there we go.

117
00:04:57,380 --> 00:04:59,150
He starts shooting
at the player.

118
00:04:59,150 --> 00:05:01,720
If we go out, does he
continue shooting?

119
00:05:01,715 --> 00:05:04,045
No pay, doesn't. He stops.

120
00:05:04,040 --> 00:05:05,930
We are in his fruits.

121
00:05:05,930 --> 00:05:09,440
We are out. He stops
shooting. Okay, great.

122
00:05:09,440 --> 00:05:11,240
So that working, I

123
00:05:11,240 --> 00:05:13,820
believe we still have
a bit of time and

124
00:05:13,820 --> 00:05:15,850
we will use that time to

125
00:05:15,845 --> 00:05:18,365
arrange everything
that we have in here.

126
00:05:18,365 --> 00:05:20,105
So what is the problem?

127
00:05:20,105 --> 00:05:22,055
You can see everything
is working.

128
00:05:22,055 --> 00:05:23,395
We don't have an errors.

129
00:05:23,390 --> 00:05:26,110
But the code is very API.

130
00:05:26,105 --> 00:05:28,045
You can see right
here that we have

131
00:05:28,040 --> 00:05:30,730
this update and this
update does a lot,

132
00:05:30,725 --> 00:05:33,055
and this is not something
that we want an even.

133
00:05:33,050 --> 00:05:35,520
And the player, you can
see player controller.

134
00:05:35,524 --> 00:05:39,294
We have a lot of lines of
code and they are messy.

135
00:05:39,290 --> 00:05:40,460
What happens in here?

136
00:05:40,460 --> 00:05:41,060
What do we do?

137
00:05:41,060 --> 00:05:42,200
Do we should do a move to

138
00:05:42,200 --> 00:05:44,630
replay what is
happening in here.

139
00:05:44,630 --> 00:05:46,150
So we are going to,

140
00:05:46,145 --> 00:05:49,115
of course fix this
by creating or

141
00:05:49,115 --> 00:05:52,745
using our new found knowledge
of creating methods.

142
00:05:52,745 --> 00:05:54,685
What we can do is,

143
00:05:54,680 --> 00:05:57,620
for example, let's say where
is the player walking?

144
00:05:57,620 --> 00:06:00,910
So this is the movement we
normalize, we give velocity.

145
00:06:00,905 --> 00:06:03,535
So from the first
line to this line,

146
00:06:03,530 --> 00:06:05,800
we are moving the player.

147
00:06:05,795 --> 00:06:08,785
Let's go ahead and
select all of these.

148
00:06:08,780 --> 00:06:10,700
I'm going to select these lines.

149
00:06:10,700 --> 00:06:12,460
I'm going to right-click.

150
00:06:12,455 --> 00:06:14,185
And you can see right
here that we have

151
00:06:14,180 --> 00:06:16,510
the quick actions
and refactoring.

152
00:06:16,505 --> 00:06:17,885
So when I click it,

153
00:06:17,885 --> 00:06:19,435
you can see that it gives us

154
00:06:19,430 --> 00:06:21,770
the option to extract a method.

155
00:06:21,770 --> 00:06:23,980
I'm going to click
on Extract Method

156
00:06:23,975 --> 00:06:26,515
and now you have a new method.

157
00:06:26,510 --> 00:06:27,760
What do you want to call it?

158
00:06:27,755 --> 00:06:31,975
Let's call it the moving player.

159
00:06:31,970 --> 00:06:33,650
Hit Enter. So now you

160
00:06:33,650 --> 00:06:35,600
can see that all
the lines are gone,

161
00:06:35,600 --> 00:06:38,990
but they are stored
in this little nifty,

162
00:06:38,990 --> 00:06:41,300
very handy method down here.

163
00:06:41,300 --> 00:06:43,820
So now we know that we
start off by moving

164
00:06:43,820 --> 00:06:46,310
the player and then
we have all of this.

165
00:06:46,310 --> 00:06:47,800
So what happens in here,

166
00:06:47,795 --> 00:06:50,515
the mouse position
and screen point,

167
00:06:50,510 --> 00:06:52,160
we calculate the offset,

168
00:06:52,160 --> 00:06:53,320
we get the angle,

169
00:06:53,315 --> 00:06:56,665
we rotate the arm and
then we check if we

170
00:06:56,660 --> 00:07:00,740
should go from one
direction to the next.

171
00:07:00,740 --> 00:07:02,770
What else do we have in here?

172
00:07:02,765 --> 00:07:06,895
Let's separate first
of all, the shooting.

173
00:07:06,890 --> 00:07:10,410
So we're going to
separate the shooting.

174
00:07:12,730 --> 00:07:15,950
And what did we call
this the moving player.

175
00:07:15,950 --> 00:07:23,380
And let's call this the
layer shooting enter.

176
00:07:23,375 --> 00:07:25,925
That's called this
instead of moving player.

177
00:07:25,925 --> 00:07:29,855
That's called this, the player

178
00:07:29,855 --> 00:07:34,075
Moving and control
RR, of course.

179
00:07:34,070 --> 00:07:35,930
And how do we want to separate?

180
00:07:35,930 --> 00:07:39,460
We can call this animating
the player if we want.

181
00:07:39,455 --> 00:07:41,305
And we can call

182
00:07:41,300 --> 00:07:46,040
this one setting the player
in the right direction.

183
00:07:46,040 --> 00:07:48,710
And this one will be.

184
00:07:48,710 --> 00:07:51,520
But for example, what
should we call this?

185
00:07:51,515 --> 00:07:54,715
So honestly, this is all
based on your preference.

186
00:07:54,710 --> 00:07:56,480
How do you want to make it?

187
00:07:56,480 --> 00:07:59,810
So I'm going to call
this the right-click.

188
00:07:59,810 --> 00:08:05,440
And it's called the pointing.

189
00:08:05,435 --> 00:08:09,465
Pointing in our case.

190
00:08:09,460 --> 00:08:12,890
So obviously you can see
right here what happened.

191
00:08:12,890 --> 00:08:16,610
We have this outmost position
and out scream point.

192
00:08:16,610 --> 00:08:19,780
That means that we are
returning these values.

193
00:08:19,775 --> 00:08:21,475
Why are we returning them?

194
00:08:21,470 --> 00:08:22,940
While? Because we use

195
00:08:22,940 --> 00:08:25,720
the Screen point somewhere
else in our code.

196
00:08:25,715 --> 00:08:27,085
That's just said apply for now,

197
00:08:27,080 --> 00:08:29,210
double-click on stream
point and there you go.

198
00:08:29,210 --> 00:08:31,630
You can see that we are
using the mouse position

199
00:08:31,625 --> 00:08:34,405
and the screen point
in this if condition.

200
00:08:34,400 --> 00:08:36,500
But we are calculating it

201
00:08:36,500 --> 00:08:39,650
inside of the point in pointing.

202
00:08:39,650 --> 00:08:41,120
I didn't change the name.

203
00:08:41,120 --> 00:08:42,530
So you can see this is where we

204
00:08:42,530 --> 00:08:44,930
calculated and this
is where we need it.

205
00:08:44,930 --> 00:08:46,850
So we create this out,

206
00:08:46,850 --> 00:08:48,280
but I don't want this.

207
00:08:48,275 --> 00:08:51,205
I feel It's a bit bad.

208
00:08:51,200 --> 00:08:53,500
Oops. Too much.

209
00:08:53,495 --> 00:08:56,855
Control Z, layer.

210
00:08:56,855 --> 00:08:59,155
Moving enter.

211
00:08:59,150 --> 00:09:00,250
We save that.

212
00:09:00,245 --> 00:09:04,525
Now let's go ahead and use
all of these we right-click.

213
00:09:04,520 --> 00:09:07,760
And what shall we call
this huge method?

214
00:09:07,760 --> 00:09:11,100
We will call it, what
do we do in here?

215
00:09:12,400 --> 00:09:21,160
Pointing a gun at
mouse, at Enter.

216
00:09:21,155 --> 00:09:23,125
So you need the
name of the method

217
00:09:23,120 --> 00:09:25,480
to describe what's happening.

218
00:09:25,475 --> 00:09:27,715
And finally, we can
say that this is

219
00:09:27,710 --> 00:09:31,760
animating the player
Extract Method.

220
00:09:31,760 --> 00:09:37,360
Animating V player
hit Enter and whoa,

221
00:09:37,355 --> 00:09:40,135
what a huge difference that

222
00:09:40,130 --> 00:09:42,920
just made seriously
look at the update,

223
00:09:42,920 --> 00:09:44,690
how clean it is now,

224
00:09:44,690 --> 00:09:47,390
we know that the
player is moving.

225
00:09:47,389 --> 00:09:49,969
He's pointing the
gun at the mouse.

226
00:09:49,970 --> 00:09:54,320
We are animating the player
and the player is shooting.

227
00:09:54,320 --> 00:09:56,810
And maybe you can even extract

228
00:09:56,810 --> 00:09:58,550
an extra method inside of

229
00:09:58,550 --> 00:10:00,800
the pointing guns to
make it even cleaner.

230
00:10:00,800 --> 00:10:04,840
Because for example, what
we're doing in here is we are,

231
00:10:04,835 --> 00:10:06,655
may be rotating or

232
00:10:06,650 --> 00:10:08,930
pointing the player in
the right direction.

233
00:10:08,930 --> 00:10:11,050
You can even extract
these two methods,

234
00:10:11,045 --> 00:10:13,025
even make this more clean.

235
00:10:13,025 --> 00:10:16,205
So her time as a challenge,

236
00:10:16,205 --> 00:10:18,955
I'm going to give
you the liberty of

237
00:10:18,950 --> 00:10:22,090
extracting the methods
in the NMI controller.

238
00:10:22,085 --> 00:10:23,755
And I'll see you in

239
00:10:23,750 --> 00:10:26,420
the next video where I will
have already done this.

240
00:10:26,420 --> 00:10:28,670
Maybe I'll show you
whatever you want.

241
00:10:28,670 --> 00:10:30,370
Do it yourself, have fun.

242
00:10:30,365 --> 00:10:31,805
There's no pressure in here.

243
00:10:31,805 --> 00:10:35,575
Just have this practice of
doing things on your own.

244
00:10:35,570 --> 00:10:38,110
Of course, I will show
you the results later on,

245
00:10:38,105 --> 00:10:40,235
but I hope you enjoy it

246
00:10:40,235 --> 00:10:42,595
and I'll see you
in the next video.

247
00:10:42,590 --> 00:10:46,140
Never forget to commit
your changes. My body.

