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

2
00:00:06,060 --> 00:00:07,370
And as you can see,

3
00:00:07,365 --> 00:00:10,515
we have now a yellow circle.

4
00:00:10,515 --> 00:00:14,355
What does this yellow circle do when we run our game?

5
00:00:14,355 --> 00:00:17,435
You can see that if I go inside the yellow circle,

6
00:00:17,430 --> 00:00:21,300
nothing happens and just make the enemy speed a little bit less.

7
00:00:21,300 --> 00:00:24,600
If I go into the red circle as normal,

8
00:00:24,600 --> 00:00:26,030
he starts chasing us.

9
00:00:26,025 --> 00:00:27,305
I tried to get away.

10
00:00:27,300 --> 00:00:28,800
I'm inside the yellow circle,

11
00:00:28,800 --> 00:00:30,360
but he is still chasing us.

12
00:00:30,360 --> 00:00:31,710
What is happening?

13
00:00:31,705 --> 00:00:36,455
The only way to win is to go outside the yellow circle.

14
00:00:36,455 --> 00:00:38,935
Also, that's how you get away from

15
00:00:38,930 --> 00:00:42,010
the enemy because he started now it doesn't want to chase you anymore.

16
00:00:42,005 --> 00:00:44,075
So let's not waste any more time.

17
00:00:44,075 --> 00:00:45,275
Let's get started.

18
00:00:45,275 --> 00:00:48,845
And I forgot to tell you that you need to commit your changes at the end.

19
00:00:48,845 --> 00:00:50,435
Don't forget to do that.

20
00:00:50,435 --> 00:00:54,475
So let's start k.

21
00:00:54,470 --> 00:00:58,750
So now we have the enemy knowing

22
00:00:58,745 --> 00:01:03,335
where the player is based on a certain distance he is from him.

23
00:01:03,335 --> 00:01:06,215
When the player gets on a certain distance,

24
00:01:06,215 --> 00:01:08,125
a certain chase range,

25
00:01:08,120 --> 00:01:11,860
the enemy will know and he will start chasing the player.

26
00:01:11,855 --> 00:01:13,915
Well, he doesn't do that physically,

27
00:01:13,910 --> 00:01:17,930
but we can see from the logic and the code that he knows,

28
00:01:17,930 --> 00:01:20,470
how are we going to do this?

29
00:01:20,465 --> 00:01:26,665
First thing first, we are going to determine the direction this enemy will be moving in.

30
00:01:26,660 --> 00:01:30,950
So if the distance is less than the player chase range,

31
00:01:30,950 --> 00:01:36,580
I'm going to stop the debugging and I'm going to save that the direction to move in,

32
00:01:36,575 --> 00:01:43,805
it's going to be equal to the player chase range or nonalignment Eclair to chase layer

33
00:01:43,805 --> 00:01:51,235
to chase dot position minus the transform position.

34
00:01:51,230 --> 00:01:55,400
So this is just like the formula that we use.

35
00:01:55,400 --> 00:01:58,010
The 2 the gun and the right direction,

36
00:01:58,010 --> 00:02:00,040
we choose the position,

37
00:02:00,035 --> 00:02:03,125
we remove the position of the player to chase.

38
00:02:03,125 --> 00:02:07,235
We subtract the position of the enemy from

39
00:02:07,235 --> 00:02:11,305
the player to chase position and that way we get the direction.

40
00:02:11,300 --> 00:02:14,330
So it's similar to the way we are pointing our gone,

41
00:02:14,330 --> 00:02:17,170
you can go back and see how that works.

42
00:02:17,165 --> 00:02:21,715
Now what we're going to do after we know the direction that we're moving in,

43
00:02:21,710 --> 00:02:25,720
we are going to normalize that direction.

44
00:02:25,715 --> 00:02:31,235
Because we don't want the enemy to be faster when he moves diagonally.

45
00:02:31,235 --> 00:02:33,275
So we normalize.

46
00:02:33,275 --> 00:02:36,565
And then we are going to access the rigid body.

47
00:02:36,560 --> 00:02:41,150
So the enemy rigid body get the velocity and it's going

48
00:02:41,150 --> 00:02:46,040
to be equal to the direction to move n times the enemy.

49
00:02:46,040 --> 00:02:47,570
Speak.

50
00:02:47,570 --> 00:02:49,670
Save that.

51
00:02:49,670 --> 00:02:52,700
And now let's go back to our game.

52
00:02:52,700 --> 00:02:54,280
Let's see how that works.

53
00:02:54,275 --> 00:03:00,295
I'm going to set the game view next to the scene view so we can see how the range works.

54
00:03:00,290 --> 00:03:01,700
When we get into range,

55
00:03:01,700 --> 00:03:03,440
I'm going to run the game.

56
00:03:03,440 --> 00:03:06,670
So now as I approach the enemy,

57
00:03:06,665 --> 00:03:09,145
yep, there we go, nothing happens.

58
00:03:09,140 --> 00:03:10,220
Why is that?

59
00:03:10,220 --> 00:03:13,900
Well, because we have the small error in here and that the object,

60
00:03:13,895 --> 00:03:16,345
we don't have a reference to width.

61
00:03:16,340 --> 00:03:17,570
Why is that?

62
00:03:17,570 --> 00:03:21,520
Because we are trying to access the enemy rigid body,

63
00:03:21,515 --> 00:03:22,855
but we haven't set it.

64
00:03:22,850 --> 00:03:25,730
So the NMI rigid body,

65
00:03:25,730 --> 00:03:32,660
the dot is equal to the debt component, rigid body 2D.

66
00:03:32,660 --> 00:03:34,690
There we go.

67
00:03:34,685 --> 00:03:38,735
Okay, now everything should be working fine.

68
00:03:38,735 --> 00:03:41,845
Let's stop the game, run it again.

69
00:03:41,840 --> 00:03:45,050
And now we're getting close. And there you go.

70
00:03:45,050 --> 00:03:47,690
You can see that the enemy is actually chasing us.

71
00:03:47,690 --> 00:03:50,680
And obviously we need to fix a couple of things,

72
00:03:50,675 --> 00:03:52,645
but it is working.

73
00:03:52,640 --> 00:03:55,340
What happens if we are out of range?

74
00:03:55,340 --> 00:03:57,400
I'm trying to get out of range.

75
00:03:57,395 --> 00:03:59,095
Okay. So it doesn't work.

76
00:03:59,090 --> 00:04:01,360
Let me just first of all,

77
00:04:01,355 --> 00:04:03,205
well at least it's working.

78
00:04:03,200 --> 00:04:04,940
It's not perfect, but it's working.

79
00:04:04,940 --> 00:04:06,770
So let me set this one.

80
00:04:06,770 --> 00:04:11,430
Let me make the range a little bit smaller because I want to demonstrate something.

81
00:04:11,434 --> 00:04:16,884
And let's make the speed around too so we can escape.

82
00:04:16,880 --> 00:04:19,150
I'm going to run the game.

83
00:04:19,145 --> 00:04:23,665
I'm going to get close to the enemy and then I'm going to stop, I'm runway.

84
00:04:23,660 --> 00:04:25,120
So as you can see,

85
00:04:25,115 --> 00:04:29,225
the enemy is still walking and finally he hits a wall.

86
00:04:29,225 --> 00:04:31,835
So what is the problem in here?

87
00:04:31,835 --> 00:04:35,425
The problem is that the enemy will still have the momentum with them.

88
00:04:35,420 --> 00:04:37,750
So I get close, I move away.

89
00:04:37,745 --> 00:04:42,655
And even if I'm not and the range is still walking mindlessly, now,

90
00:04:42,650 --> 00:04:46,040
this would be not very bad if he was a zombie,

91
00:04:46,040 --> 00:04:47,690
but apparently he's a skeleton,

92
00:04:47,690 --> 00:04:50,050
so he should stop.

93
00:04:50,045 --> 00:04:52,045
Okay, I should stop though jokes.

94
00:04:52,040 --> 00:04:54,290
And let's see what we can do.

95
00:04:54,290 --> 00:04:59,390
So in here, what we should do is when we are in range,

96
00:04:59,390 --> 00:05:01,540
we are chasing the enemy.

97
00:05:01,535 --> 00:05:03,265
If we're out of range,

98
00:05:03,260 --> 00:05:11,660
the direction to move in will be equal to vector 3, 0.

99
00:05:11,660 --> 00:05:14,510
There we go. That was the direction to move in and vector three,

100
00:05:14,510 --> 00:05:16,850
okay, Yes, it was. So save that.

101
00:05:16,850 --> 00:05:18,920
So now when we get out of range,

102
00:05:18,920 --> 00:05:21,110
we should stop chasing the player.

103
00:05:21,110 --> 00:05:23,450
So let me know what.

104
00:05:23,450 --> 00:05:26,170
Let's use a different view, a different layout.

105
00:05:26,165 --> 00:05:28,045
We can use the two-by-three.

106
00:05:28,040 --> 00:05:32,810
There you go. This is a nice way also to view the Unity engine.

107
00:05:32,810 --> 00:05:39,140
So you can see right now change this from three aspects to 19 by 1920 by 1080.

108
00:05:39,140 --> 00:05:42,650
Now we can see it scene view clearly and the game view clearly.

109
00:05:42,650 --> 00:05:46,570
We run the game and we get close and to the chase range.

110
00:05:46,565 --> 00:05:47,095
There we go.

111
00:05:47,090 --> 00:05:50,980
You can see he keeps chasing us when I'm outside of that range.

112
00:05:50,975 --> 00:05:54,025
You can see that he immediately stops.

113
00:05:54,020 --> 00:05:58,070
There you go. You can see as soon as I'm in, he stops.

114
00:05:58,070 --> 00:06:00,580
When I'm out, he stops chasing.

115
00:06:00,575 --> 00:06:01,885
Okay, great.

116
00:06:01,880 --> 00:06:03,430
So what that done?

117
00:06:03,425 --> 00:06:04,745
I hope you enjoy it.

118
00:06:04,745 --> 00:06:07,675
Now we can make the chase range a little bit bigger

119
00:06:07,670 --> 00:06:11,150
because we know if we get far away enough from the enemy.

120
00:06:11,150 --> 00:06:12,110
There you go.

121
00:06:12,110 --> 00:06:15,190
You can see he stops chasing girls.

122
00:06:15,185 --> 00:06:21,025
Now, you can add an extra layer of intelligence to the enemy if you want,

123
00:06:21,020 --> 00:06:23,500
you can create a stop range.

124
00:06:23,495 --> 00:06:26,755
For example, let's say we have this range,

125
00:06:26,750 --> 00:06:28,630
we have another range.

126
00:06:28,625 --> 00:06:30,025
And if the player,

127
00:06:30,020 --> 00:06:32,420
if we can get out of that range,

128
00:06:32,420 --> 00:06:35,890
the second one, then the enemy will stop chasing us.

129
00:06:35,885 --> 00:06:40,585
And that's going to be actually a challenge for you.

130
00:06:40,580 --> 00:06:44,750
So how do you like the transitions to the challenge now, there are better, right?

131
00:06:44,750 --> 00:06:47,000
I got you off guard there, didn't I?

132
00:06:47,000 --> 00:06:52,620
So I hope create a keep chasing range variable.

133
00:06:52,624 --> 00:06:56,104
You'll need to draw that range using the gizmos.

134
00:06:56,105 --> 00:06:59,315
You need to create a boolean is chasing.

135
00:06:59,314 --> 00:07:05,544
That is true when chasing and false when we're outside the certain range.

136
00:07:05,540 --> 00:07:09,920
And finally, you will need to create an else if statement that checks if

137
00:07:09,920 --> 00:07:14,630
we're chasing and we are inside the keep chasing range.

138
00:07:14,630 --> 00:07:16,670
So I hope you understood the challenge.

139
00:07:16,670 --> 00:07:17,960
It's a bit hard.

140
00:07:17,960 --> 00:07:20,510
It will need a bit of configuring,

141
00:07:20,510 --> 00:07:24,160
but I'm sure that you are up to the challenge.

142
00:07:24,155 --> 00:07:31,105
So pause the video right now and go to the Shalon case.

143
00:07:31,100 --> 00:07:37,100
So first thing we're going to do is we're going to create a new variable in here,

144
00:07:37,100 --> 00:07:40,720
which is going to be the keep chasing.

145
00:07:40,715 --> 00:07:46,855
And we'll call it the player keep Chase range.

146
00:07:46,850 --> 00:07:49,760
And then we're create a Boolean.

147
00:07:49,760 --> 00:07:52,310
And here which we are going to call,

148
00:07:52,310 --> 00:07:54,530
I'll make it private or know what,

149
00:07:54,530 --> 00:07:58,120
let's make it public so we can see it and then we'll make it private.

150
00:07:58,115 --> 00:08:01,715
And public boolean is chasing.

151
00:08:01,715 --> 00:08:04,085
So the next step,

152
00:08:04,085 --> 00:08:07,225
what was the next step and the challenge that we see, draw,

153
00:08:07,220 --> 00:08:10,910
okay, so we're going to draw the circle right here.

154
00:08:10,910 --> 00:08:14,150
I'm just going to take this and duplicated.

155
00:08:14,150 --> 00:08:16,270
So Control D as always,

156
00:08:16,265 --> 00:08:17,875
let's make it yellow.

157
00:08:17,870 --> 00:08:21,680
And this will be the keep chases range.

158
00:08:21,680 --> 00:08:22,730
Save that.

159
00:08:22,730 --> 00:08:25,490
And now what do we have to do next?

160
00:08:25,490 --> 00:08:29,120
We need to create a Boolean as tracing with that.

161
00:08:29,120 --> 00:08:31,800
And okay, so when is it true?

162
00:08:31,795 --> 00:08:37,465
It's true in here when we are inside the range and we start going after the player.

163
00:08:37,465 --> 00:08:41,035
So is chasing is equal to true.

164
00:08:41,035 --> 00:08:45,075
Then what happens if we go outside this range?

165
00:08:45,070 --> 00:08:49,390
The is chasing becomes false.

166
00:08:49,390 --> 00:08:57,450
But in between, we want to create an if else or else, if.

167
00:08:57,445 --> 00:09:05,955
Else, if the vector or the distance of the player

168
00:09:05,950 --> 00:09:15,020
and the enemy is still less than v0, keep Chase range.

169
00:09:15,020 --> 00:09:20,150
And so this is for, is chasing.

170
00:09:20,150 --> 00:09:26,360
What we're going to do is we are going to keep the direction as it is.

171
00:09:26,360 --> 00:09:27,500
There you go.

172
00:09:27,500 --> 00:09:32,830
Let's save that. Let's look one more time and this code and see if everything works.

173
00:09:32,825 --> 00:09:34,915
So we've created the range,

174
00:09:34,910 --> 00:09:36,820
we've made it yellow.

175
00:09:36,815 --> 00:09:42,015
What happens in here is when the player enters the range of chasing,

176
00:09:42,019 --> 00:09:45,009
the direction will become the player to chase.

177
00:09:45,005 --> 00:09:49,415
And then we are going to set the is chasing to true.

178
00:09:49,415 --> 00:09:52,355
Then we are going to check if the player can get

179
00:09:52,355 --> 00:09:57,095
outside or if the player is still inside the keep Chase range.

180
00:09:57,095 --> 00:09:58,925
And we are chasing,

181
00:09:58,925 --> 00:10:04,175
then we will have the direction to move and still in the player.

182
00:10:04,175 --> 00:10:04,805
Else.

183
00:10:04,805 --> 00:10:06,085
If we are outside,

184
00:10:06,080 --> 00:10:09,430
both the players chase range and they keep Chase range,

185
00:10:09,425 --> 00:10:11,365
then is chasing is false.

186
00:10:11,360 --> 00:10:17,450
And what's great about this is if we go inside of the player keep Chase range,

187
00:10:17,449 --> 00:10:21,339
the enemy will not follow us because it's chasing as false.

188
00:10:21,335 --> 00:10:24,245
So let's see if that actually works.

189
00:10:24,245 --> 00:10:28,315
And I'm going to increase the keep Chase range.

190
00:10:28,310 --> 00:10:29,930
Let's make it.

191
00:10:29,930 --> 00:10:32,620
Yep, there we go. I think that's good enough.

192
00:10:32,615 --> 00:10:34,075
I'm going to run the game.

193
00:10:34,070 --> 00:10:37,760
And now let's see if I walk inside the keep Chase range.

194
00:10:37,760 --> 00:10:38,830
Nothing happens.

195
00:10:38,825 --> 00:10:41,285
If I walk inside the chase range,

196
00:10:41,285 --> 00:10:43,345
the enemy starts chasing.

197
00:10:43,340 --> 00:10:46,250
If I am, I can actually push him.

198
00:10:46,250 --> 00:10:48,200
If I am outside the chase range,

199
00:10:48,200 --> 00:10:49,940
he still follows me.

200
00:10:49,940 --> 00:10:53,240
What if I go outside both of the ranges?

201
00:10:53,240 --> 00:10:54,520
He stops following.

202
00:10:54,515 --> 00:10:57,125
And you can see right here that is chasing is false.

203
00:10:57,125 --> 00:10:58,795
So he doesn't chase me.

204
00:10:58,790 --> 00:11:00,650
When is chasing is true?

205
00:11:00,650 --> 00:11:05,260
He keeps chasing until I go outside the yellow circle.

206
00:11:05,255 --> 00:11:07,325
So I hope you do the challenge.

207
00:11:07,325 --> 00:11:08,975
I hope it wasn't too hard.

208
00:11:08,975 --> 00:11:11,165
I know when I did this challenge,

209
00:11:11,165 --> 00:11:13,805
I felt that it might be a bit too hard,

210
00:11:13,805 --> 00:11:16,015
a bit too confusing.

211
00:11:16,010 --> 00:11:19,660
But I hope you understood what the point of the Boolean was.

212
00:11:19,655 --> 00:11:24,775
I hope you understood how the else if statements here worked.

213
00:11:24,770 --> 00:11:26,420
If you didn't re-watch the video,

214
00:11:26,420 --> 00:11:29,350
I think I explained it well enough,

215
00:11:29,345 --> 00:11:31,615
so I hope you enjoy it and I'll see you in

216
00:11:31,610 --> 00:11:35,590
the next video where we will be actually animating our enemy.

217
00:11:35,585 --> 00:11:36,925
Because as you can see,

218
00:11:36,920 --> 00:11:39,100
he's very boring currently.

219
00:11:39,095 --> 00:11:42,995
Until then, have a nice day and I'll see you then.

