1
00:00:00,650 --> 00:00:05,970
Welcome back my fellow game developers to a brand new video.

2
00:00:05,969 --> 00:00:11,629
And in this one we've actually made our enemies lightly stupid.

3
00:00:11,625 --> 00:00:14,145
So even though he can shoot,

4
00:00:14,145 --> 00:00:18,705
but only the slight problem is that he shoots himself,

5
00:00:18,705 --> 00:00:20,445
which is sometimes good,

6
00:00:20,445 --> 00:00:22,425
especially if a very hard enemy.

7
00:00:22,425 --> 00:00:25,235
But in our case, we will be fixing it later on.

8
00:00:25,230 --> 00:00:27,930
But for now, all you need to know is that we can

9
00:00:27,930 --> 00:00:31,330
change the time between shots to, for example, three.

10
00:00:31,330 --> 00:00:33,410
And you can see it takes three seconds to shoot,

11
00:00:33,410 --> 00:00:35,660
but we don't do that using a counter.

12
00:00:35,660 --> 00:00:38,990
We do that using something called a co-routine,

13
00:00:38,990 --> 00:00:46,120
which is a very hard scaring but in credibly powerful tool to learn in Unity.

14
00:00:46,115 --> 00:00:50,245
So I need your concentration and your full attention.

15
00:00:50,240 --> 00:00:55,890
Let's not waste any more time and let's get started.

16
00:00:55,990 --> 00:01:02,930
Okay, so time to give our enemy the ability to shoot the player,

17
00:01:02,930 --> 00:01:05,420
or at least attack the player.

18
00:01:05,420 --> 00:01:10,460
We do have two different types of attacks that we are going to implement in our game.

19
00:01:10,460 --> 00:01:11,990
One of them will be the Malay,

20
00:01:11,990 --> 00:01:15,190
one of them will be simply shooting from afar or

21
00:01:15,185 --> 00:01:19,235
throwing the sword and the skeletons example.

22
00:01:19,235 --> 00:01:22,615
But we are going to focus more for now on the shooting from

23
00:01:22,610 --> 00:01:25,960
a distance and do them melee attacks later on.

24
00:01:25,955 --> 00:01:27,595
Maybe in a different section,

25
00:01:27,590 --> 00:01:31,570
may be in the boss section where we have the giant skeleton.

26
00:01:31,565 --> 00:01:33,545
Okay, with that out of the way,

27
00:01:33,545 --> 00:01:35,065
let's go ahead and get started.

28
00:01:35,060 --> 00:01:38,350
The first thing we're going to do is we're going to

29
00:01:38,345 --> 00:01:42,195
create a couple of variables and the enemy controller 0 case.

30
00:01:42,190 --> 00:01:45,970
So we are going to go into the enemy controller.

31
00:01:45,965 --> 00:01:49,865
And in here we are going to create a couple of variables.

32
00:01:49,865 --> 00:01:55,145
So before the death splatter right here,

33
00:01:55,145 --> 00:01:58,225
we're going to add attack,

34
00:01:58,220 --> 00:02:03,710
just a common so we know what everything is and it's going to be a serialized field

35
00:02:03,710 --> 00:02:09,400
of type Boolean and it's going to be a melee attack.

36
00:02:09,395 --> 00:02:11,585
So if it is a melee attacker,

37
00:02:11,585 --> 00:02:15,635
we are going to check it in the inspector and it will attack using melee.

38
00:02:15,635 --> 00:02:18,505
Then we are going to create a serialized fields,

39
00:02:18,500 --> 00:02:25,040
which is going to be a game object and it's going to be the enemy projectile.

40
00:02:25,040 --> 00:02:30,460
So this is what the enemy will be throwing out of his hand if he does shoot.

41
00:02:30,455 --> 00:02:34,945
Finally, we are going to create a transform dot.

42
00:02:34,940 --> 00:02:38,180
Oh, sorry, fire position.

43
00:02:38,180 --> 00:02:41,110
Okay, so now we have the projectile.

44
00:02:41,105 --> 00:02:43,435
We will create a file position.

45
00:02:43,430 --> 00:02:49,180
And we also want to have a time in-between the shots that the enemy does.

46
00:02:49,175 --> 00:02:51,965
So it's similar to the way we handled our gun.

47
00:02:51,965 --> 00:02:56,755
So float time between shots.

48
00:02:56,750 --> 00:03:04,520
And finally we are going to create a private bool ready to shoot.

49
00:03:04,520 --> 00:03:06,380
So what happens here?

50
00:03:06,380 --> 00:03:09,010
When we are ready to shoot, the enemy shoots,

51
00:03:09,005 --> 00:03:12,715
and every time he shoots and between time shots is going to calculate.

52
00:03:12,710 --> 00:03:14,270
So we did this using,

53
00:03:14,270 --> 00:03:16,730
I believe the counter.

54
00:03:16,730 --> 00:03:20,180
We created a counter where we count down the time until

55
00:03:20,180 --> 00:03:24,290
we finish a certain amount of time between shots when it's finished,

56
00:03:24,290 --> 00:03:25,900
then we can show it again.

57
00:03:25,895 --> 00:03:27,545
That was an old way.

58
00:03:27,545 --> 00:03:30,545
Now we are going to use something called the co-routine.

59
00:03:30,545 --> 00:03:34,105
So before I do that and introduce you to coroutines,

60
00:03:34,100 --> 00:03:38,390
let me first of all set the ready to shoot in here to true.

61
00:03:38,390 --> 00:03:40,280
So whenever the enemy starts,

62
00:03:40,280 --> 00:03:42,950
he is ready to shoot and an update.

63
00:03:42,950 --> 00:03:47,510
We are going to go ahead and create something in here which is a condition.

64
00:03:47,509 --> 00:03:50,829
And if The Malay attacker,

65
00:03:50,824 --> 00:03:54,484
or if we're not a merely attacker, we are not.

66
00:03:54,485 --> 00:03:59,305
When we put the exclamation mark again and we are ready to shoot.

67
00:03:59,300 --> 00:04:03,050
Then what we're going to do is we're going to go ahead

68
00:04:03,050 --> 00:04:07,040
and say that ready to shoot as false.

69
00:04:07,039 --> 00:04:11,859
And then in here we are going to start a coating.

70
00:04:11,854 --> 00:04:14,364
So what is a co-routine?

71
00:04:14,359 --> 00:04:16,179
Why do we need it in here?

72
00:04:16,175 --> 00:04:19,055
Let's have a bit of an explanation.

73
00:04:19,055 --> 00:04:21,715
So what is a coltan?

74
00:04:21,710 --> 00:04:25,970
A co-routine is a method that can suspend the execution of

75
00:04:25,970 --> 00:04:32,260
instructions until a certain condition or certain conditions are met.

76
00:04:32,255 --> 00:04:36,025
These suspensions are know as yield.

77
00:04:36,020 --> 00:04:37,730
And in our example,

78
00:04:37,730 --> 00:04:42,950
will be suspending the time until our enemy is ready to shoot again.

79
00:04:42,950 --> 00:04:46,880
So let's go back to our code and see what all of that means.

80
00:04:46,880 --> 00:04:48,460
So currently what we have,

81
00:04:48,455 --> 00:04:51,605
we check in the update which is called every single frame.

82
00:04:51,605 --> 00:04:57,485
We check if we are not aiming attacker and we are ready to shoot,

83
00:04:57,485 --> 00:04:59,665
then ready to shoot, set to false.

84
00:04:59,660 --> 00:05:04,430
And for example, here we are going to shoot the bullet.

85
00:05:04,430 --> 00:05:07,600
But what we want, just like we had in the counter,

86
00:05:07,595 --> 00:05:12,235
we want after a certain amount of time to be able to shoot.

87
00:05:12,230 --> 00:05:15,760
Again, we have this set to false because we don't want to shoot

88
00:05:15,755 --> 00:05:19,555
every single frame a bullet and have gazillions of bullets.

89
00:05:19,550 --> 00:05:23,530
So the way we fix this is by using a co-routine.

90
00:05:23,525 --> 00:05:26,395
So let me just go ahead and create a coroutine right

91
00:05:26,390 --> 00:05:30,520
here is going to be of type eye enumerator.

92
00:05:30,515 --> 00:05:34,675
You don't need to worry a lot about what our enumerator is,

93
00:05:34,670 --> 00:05:38,420
just know that it's the type of coatings.

94
00:05:38,420 --> 00:05:45,970
We're going to create the coroutine name as fire enemy, projectile.

95
00:05:45,965 --> 00:05:53,045
I'm going to open up the brackets and we are going to suspend the time.

96
00:05:53,045 --> 00:05:55,865
So I'm going to say that yield.

97
00:05:55,865 --> 00:05:58,835
Okay, Focus, yield.

98
00:05:58,835 --> 00:06:00,785
That means we are suspending.

99
00:06:00,785 --> 00:06:04,205
Return. New.

100
00:06:04,205 --> 00:06:07,735
Wait four seconds.

101
00:06:07,730 --> 00:06:09,890
I'm going to open up the brackets.

102
00:06:09,890 --> 00:06:15,340
And how many seconds do we want to weight as the time between shots?

103
00:06:15,335 --> 00:06:23,095
And after that, we can go ahead and instantiate the bullet or instantiate the projectile,

104
00:06:23,090 --> 00:06:25,520
which is the projectile,

105
00:06:25,520 --> 00:06:29,690
enemy projectile where do instantiated at the five position,

106
00:06:29,690 --> 00:06:35,810
that position and the position but rho station.

107
00:06:35,810 --> 00:06:40,630
And then what we can do is set the ready to shoot back to true.

108
00:06:40,625 --> 00:06:42,985
And now we can show it again.

109
00:06:42,980 --> 00:06:44,530
Complicated, isn't it?

110
00:06:44,525 --> 00:06:46,085
I know less explain.

111
00:06:46,085 --> 00:06:47,785
Oh, and before I forget,

112
00:06:47,780 --> 00:06:50,660
and here after we have the shoot,

113
00:06:50,660 --> 00:06:52,900
we start the co-routine,

114
00:06:52,895 --> 00:06:56,635
which is the far enemy projectile.

115
00:06:56,630 --> 00:06:59,000
Okay, now you're even more confused.

116
00:06:59,000 --> 00:07:02,050
Let's try this again and update.

117
00:07:02,045 --> 00:07:05,755
What do we have every single frame update as being cold,

118
00:07:05,750 --> 00:07:08,780
we have all these statements right here which we

119
00:07:08,780 --> 00:07:12,080
will be converting into methods in just a bit.

120
00:07:12,080 --> 00:07:14,210
But what we focus on is here,

121
00:07:14,210 --> 00:07:18,440
if we are not a melee attacker and we are ready to shoot,

122
00:07:18,440 --> 00:07:19,660
then ready to shoot,

123
00:07:19,655 --> 00:07:22,625
set to false, and we start a Colton.

124
00:07:22,625 --> 00:07:24,325
So what happens in here,

125
00:07:24,320 --> 00:07:26,260
ready to shoot a set to false?

126
00:07:26,255 --> 00:07:31,015
That means that this method or this condition right here is false.

127
00:07:31,010 --> 00:07:33,830
So we don't shoot again every single frame.

128
00:07:33,830 --> 00:07:35,630
That's why we set it to false.

129
00:07:35,630 --> 00:07:38,360
But how are we going to set it back to true?

130
00:07:38,360 --> 00:07:41,030
We used to do that using the counter.

131
00:07:41,030 --> 00:07:43,670
Now we are using something a coating.

132
00:07:43,669 --> 00:07:45,199
And just like I said,

133
00:07:45,200 --> 00:07:47,860
co-routine is called Using start coroutine.

134
00:07:47,855 --> 00:07:51,745
You put the name in here and you call it I enumerator.

135
00:07:51,740 --> 00:07:54,560
No worries. Don't worry about that. For now.

136
00:07:54,560 --> 00:07:57,190
You want to delve deeper into that? Be my guest.

137
00:07:57,185 --> 00:08:00,055
You don't have to know exactly what that means.

138
00:08:00,050 --> 00:08:01,930
We have the far enemy projectile,

139
00:08:01,925 --> 00:08:04,495
then we have this expansion, which is yield.

140
00:08:04,490 --> 00:08:08,180
Then we have the return new, wait four seconds.

141
00:08:08,180 --> 00:08:10,700
So because we have an ion numerator,

142
00:08:10,700 --> 00:08:15,230
we have to return a new weight four seconds. What does this do?

143
00:08:15,230 --> 00:08:19,160
Just like it says, it waits for a number of seconds,

144
00:08:19,160 --> 00:08:21,110
which is the time between shots.

145
00:08:21,110 --> 00:08:23,530
And after it waits for this,

146
00:08:23,525 --> 00:08:27,125
then we instantiate the enemy projectile.

147
00:08:27,125 --> 00:08:32,005
Enemy shoots, and he is ready to shoot again, again and again.

148
00:08:32,000 --> 00:08:34,360
So what happens in here?

149
00:08:34,355 --> 00:08:35,785
First of all, we start,

150
00:08:35,780 --> 00:08:38,210
we have the ready to shoot to true.

151
00:08:38,210 --> 00:08:39,850
We should.

152
00:08:39,845 --> 00:08:42,265
It's false, we no longer shoot.

153
00:08:42,260 --> 00:08:45,350
And when the time between shots ends,

154
00:08:45,350 --> 00:08:50,120
we throw the projectile and then we are ready to shoot again.

155
00:08:50,120 --> 00:08:52,960
Let's test this out and see how it works.

156
00:08:52,955 --> 00:08:55,255
I'm going to go back into Unity.

157
00:08:55,250 --> 00:09:00,500
I'm going to see where do we actually have our knife 0.

158
00:09:00,500 --> 00:09:01,760
So we don't have a knife.

159
00:09:01,760 --> 00:09:05,780
So what we'll do is we'll simply use the bullet that we already have.

160
00:09:05,780 --> 00:09:08,090
So in the prefabs,

161
00:09:08,090 --> 00:09:09,800
we have the player booklets.

162
00:09:09,800 --> 00:09:12,650
Just use that for now, the fire position,

163
00:09:12,650 --> 00:09:14,860
let me add right-click,

164
00:09:14,855 --> 00:09:16,775
create an empty game object.

165
00:09:16,775 --> 00:09:19,265
And I'm going to put it right here,

166
00:09:19,265 --> 00:09:20,815
are run almost right here.

167
00:09:20,810 --> 00:09:28,370
So this will be the fire position on the enemy.

168
00:09:28,370 --> 00:09:30,710
We provide him with the FAR position

169
00:09:30,710 --> 00:09:33,730
defined between shots would be 1 second. We will save that.

170
00:09:33,725 --> 00:09:42,025
And now we are going to make sure that he is not a, made an attacker.

171
00:09:42,020 --> 00:09:44,770
Let's run the game and see what happens.

172
00:09:44,765 --> 00:09:47,185
So now, there you go.

173
00:09:47,180 --> 00:09:49,820
You can see that he is instantiating the bullet,

174
00:09:49,820 --> 00:09:53,930
but He's shooting himself and the face.

175
00:09:53,930 --> 00:09:55,910
So if I pause this for a second,

176
00:09:55,910 --> 00:09:57,530
you can see that the five position,

177
00:09:57,530 --> 00:09:59,450
that's how it's being directed.

178
00:09:59,450 --> 00:10:05,660
And we know that the players bullet only traveled in the right direction.

179
00:10:05,660 --> 00:10:07,100
I know it's kind of funny.

180
00:10:07,100 --> 00:10:09,440
Let's try this a different way.

181
00:10:09,440 --> 00:10:11,060
But in any case,

182
00:10:11,060 --> 00:10:15,350
let's not worry about that too much for now because in the next video we'll be

183
00:10:15,350 --> 00:10:21,110
creating proper enemy bullets that will actually far towards the player or not.

184
00:10:21,110 --> 00:10:23,330
Even bullets will create the projectile,

185
00:10:23,330 --> 00:10:25,480
which is the ninth of the enemy.

186
00:10:25,475 --> 00:10:29,315
He'll be throwing it as the player and not shooting himself.

187
00:10:29,315 --> 00:10:33,655
But you can see right now that this is working using the Colton.

188
00:10:33,650 --> 00:10:36,410
If I click back on the enemy right here,

189
00:10:36,410 --> 00:10:40,000
you can see that I can increase the time between shots to 2.

190
00:10:39,995 --> 00:10:43,795
So now every two seconds we have this attack.

191
00:10:43,790 --> 00:10:46,370
So I hope you enjoyed the video.

192
00:10:46,370 --> 00:10:49,100
I know the shooting mechanic isn't perfect,

193
00:10:49,100 --> 00:10:51,740
but as I said in the next video,

194
00:10:51,740 --> 00:10:53,410
we'll be creating better bullets.

195
00:10:53,405 --> 00:10:56,985
I hope you are committing all of your changes.

196
00:10:56,989 --> 00:11:01,659
And in the next video we'll be making a proper enemy bullets,

197
00:11:01,655 --> 00:11:03,685
Gentiles, or whatever you wanna call it.

198
00:11:03,680 --> 00:11:06,180
So I'll see you then.

