1
00:00:00,240 --> 00:00:04,380
Welcome back, my fellow game developers.

2
00:00:04,375 --> 00:00:07,335
In this video, we are going to add bullets.

3
00:00:07,330 --> 00:00:10,960
We call them bullets, but they are swords to our skeleton.

4
00:00:10,960 --> 00:00:13,510
So when we run our game, look what happens.

5
00:00:13,510 --> 00:00:17,770
You can see that the skeleton is actually now throwing sorts.

6
00:00:17,770 --> 00:00:20,050
And not only that, if we walk around,

7
00:00:20,050 --> 00:00:22,390
the swords start following us.

8
00:00:22,390 --> 00:00:23,490
And if you notice,

9
00:00:23,485 --> 00:00:26,325
if we change the source that are already flying,

10
00:00:26,320 --> 00:00:30,760
keep flying in the direction because there is something very recent that happened.

11
00:00:30,760 --> 00:00:35,160
They discovered that sorts don't change directions in mid air.

12
00:00:35,155 --> 00:00:38,505
Amazing discovery. Let's not waste any more time with our,

13
00:00:38,500 --> 00:00:40,140
let's not call them stupid jokes.

14
00:00:40,135 --> 00:00:42,885
And let's get started.

15
00:00:42,885 --> 00:00:49,675
Oh, okay, so time to create some proper enemy bullets.

16
00:00:49,670 --> 00:00:55,190
So the first thing that we're going to do is we are going to go ahead

17
00:00:55,190 --> 00:01:01,100
and get the sprites of our sword or the sort of the enemy.

18
00:01:01,100 --> 00:01:03,220
So if I go into the sprites,

19
00:01:03,215 --> 00:01:06,515
if I go into the enemy's,

20
00:01:06,515 --> 00:01:09,485
the Muslim enemies, the skeleton.

21
00:01:09,485 --> 00:01:14,245
And in here I have the sprite for where is it?

22
00:01:14,240 --> 00:01:16,940
Where is the flying thoughts?

23
00:01:16,940 --> 00:01:19,160
So there you go. This is the flying sorted.

24
00:01:19,160 --> 00:01:20,980
We create anything for it.

25
00:01:20,975 --> 00:01:22,375
I think we haven't.

26
00:01:22,370 --> 00:01:26,620
So I'm going to create an empty game object, reset its position.

27
00:01:26,615 --> 00:01:29,695
And I'm going to give it one of the,

28
00:01:29,690 --> 00:01:31,250
where is it, the sorts?

29
00:01:31,250 --> 00:01:34,230
So let's just give him this one.

30
00:01:34,450 --> 00:01:37,250
I will add four.

31
00:01:37,250 --> 00:01:40,120
I can simply add the sword right here.

32
00:01:40,115 --> 00:01:48,235
Just remove this game object and I'll call this the flying skeleton sword. There you go.

33
00:01:48,230 --> 00:01:50,510
And I'm going to create an animation.

34
00:01:50,510 --> 00:01:55,210
So I'm going to go to the Assets and animations,

35
00:01:55,205 --> 00:01:58,505
and I'm going to skeleton animation.

36
00:01:58,505 --> 00:02:01,525
And let's just call it the flying saucer.

37
00:02:01,520 --> 00:02:02,690
Click on Save.

38
00:02:02,690 --> 00:02:08,690
There we go. So now we have the animator and let me just choose all of the sort sprites.

39
00:02:08,690 --> 00:02:11,170
And let's go ahead and drag them in here.

40
00:02:11,165 --> 00:02:13,645
So now if we run the animation, there we go,

41
00:02:13,640 --> 00:02:18,230
you can see we have an extremely fast sword That's move it around.

42
00:02:18,230 --> 00:02:23,990
Okay, so that's almost okay. So there you go.

43
00:02:23,990 --> 00:02:26,950
You can see that this is the way our sort works.

44
00:02:26,945 --> 00:02:30,235
And it explodes before it will get to the enemy.

45
00:02:30,230 --> 00:02:34,910
So it has about 40 frames of flying time.

46
00:02:34,910 --> 00:02:41,000
And that's good. We do want to have the sword at least can destroy at some range.

47
00:02:41,000 --> 00:02:43,450
We don't want the salt to be flying all the time now,

48
00:02:43,445 --> 00:02:46,765
if you do want to have your salt flying all the time,

49
00:02:46,760 --> 00:02:49,150
you will need to add a couple of things.

50
00:02:49,145 --> 00:02:53,515
So for example, you can just put him on a loop and the salt will keep flying

51
00:02:53,510 --> 00:02:57,890
and flying without getting ever destroyed until he reaches the player.

52
00:02:57,890 --> 00:03:01,550
And finally, fair can get destroyed with some conditions.

53
00:03:01,550 --> 00:03:04,810
But let's not worry too much about that right now.

54
00:03:04,805 --> 00:03:08,315
What we will do is we are going to open up the scripts in here.

55
00:03:08,315 --> 00:03:10,495
We are going to go into the bullets and

56
00:03:10,490 --> 00:03:13,400
we'll right-click and create a new C-sharp script,

57
00:03:13,400 --> 00:03:18,980
which is going to be the enemy bullet Controller.

58
00:03:18,980 --> 00:03:21,620
And let's just call it bullet or projectile,

59
00:03:21,620 --> 00:03:23,500
but for now we will call it bullet.

60
00:03:23,495 --> 00:03:26,165
So I'm going to save that.

61
00:03:26,165 --> 00:03:29,425
And I'm going to double-click and open this up.

62
00:03:29,420 --> 00:03:34,040
So the first thing that we are going to need is a serialized field,

63
00:03:34,040 --> 00:03:38,530
which is a float for the bullet speed.

64
00:03:38,525 --> 00:03:42,925
And i'm, I'm going to use bullet and sort of projected as we go on.

65
00:03:42,920 --> 00:03:46,190
And we are going to need a reference to the players direction.

66
00:03:46,190 --> 00:03:53,930
So I'm going to create a private vector 3 and it's going to be the player direction.

67
00:03:53,930 --> 00:03:57,190
So how is the enemy bullet actually going to work?

68
00:03:57,185 --> 00:03:59,645
What we will do is then start,

69
00:03:59,645 --> 00:04:02,125
we are going to determine the players direction.

70
00:04:02,120 --> 00:04:06,170
So it's going to be equal to find objects of type.

71
00:04:06,170 --> 00:04:10,210
And it's going to be the layer controller.

72
00:04:10,205 --> 00:04:16,525
And from here we are going to go ahead and use the transform dot position of

73
00:04:16,520 --> 00:04:22,990
the player that we find minus the transform.py position dot o.

74
00:04:22,985 --> 00:04:25,405
That's it. No, not normalized,

75
00:04:25,400 --> 00:04:27,110
No, not right now.

76
00:04:27,110 --> 00:04:30,950
So why are we doing this installed and not an update?

77
00:04:30,950 --> 00:04:34,580
Because we don't want the bullet to keep following the player.

78
00:04:34,580 --> 00:04:39,620
We only want to have the player direction as soon as the bullet is fired.

79
00:04:39,620 --> 00:04:41,270
So when the bullet is fired,

80
00:04:41,270 --> 00:04:43,450
we are instantiating that bullet.

81
00:04:43,445 --> 00:04:47,215
And that bullet. As soon as it wakes up. Start.

82
00:04:47,210 --> 00:04:49,820
We'll start. It will find the player object,

83
00:04:49,820 --> 00:04:51,980
get the direction of the player.

84
00:04:51,980 --> 00:04:58,490
And then we are going to make sure that the player direction normalize.

85
00:04:58,490 --> 00:04:59,890
There we go.

86
00:04:59,885 --> 00:05:03,505
So that way it's not faster if it's moving in a diagonal.

87
00:05:03,500 --> 00:05:07,130
Okay, great. Now the next thing we want to do is we want to make sure that

88
00:05:07,130 --> 00:05:11,270
that bullet gets destroyed whenever it hits an obstacle.

89
00:05:11,270 --> 00:05:12,670
But not only that,

90
00:05:12,665 --> 00:05:17,045
we want to also reduce the health of the player later on when it hits him.

91
00:05:17,045 --> 00:05:19,805
So one of the things that we will need to do.

92
00:05:19,805 --> 00:05:21,995
Is issue you a challenge?

93
00:05:21,995 --> 00:05:23,615
How about that transition?

94
00:05:23,615 --> 00:05:26,245
The straw, the bullet, and check the player.

95
00:05:26,240 --> 00:05:27,760
So you'll need to create,

96
00:05:27,755 --> 00:05:31,175
this should be M on Trigger Enter to D for the bullet.

97
00:05:31,175 --> 00:05:35,585
You need to check if what we hit was the player by his tag,

98
00:05:35,585 --> 00:05:38,545
will need to destroy the bullet when it hits something.

99
00:05:38,540 --> 00:05:40,220
And I'll give you a small hint,

100
00:05:40,220 --> 00:05:41,630
you'll need to use the straw.

101
00:05:41,630 --> 00:05:42,860
Well, this isn't actually,

102
00:05:42,860 --> 00:05:44,390
it was pretty easy,

103
00:05:44,390 --> 00:05:48,230
but I thought that maybe because it's the first time you use destroyed,

104
00:05:48,230 --> 00:05:49,810
maybe you'll need that.

105
00:05:49,805 --> 00:05:53,135
So remember that you'll also need to add a trigger to this.

106
00:05:53,135 --> 00:05:59,965
Pause the video right now and go do the Shalon k.

107
00:05:59,960 --> 00:06:03,260
So there is something that I left From the challenge.

108
00:06:03,260 --> 00:06:07,580
I did mention it a bit and I hope that shoe that this on your own,

109
00:06:07,580 --> 00:06:10,390
I'm going to have to add a collider.

110
00:06:10,385 --> 00:06:14,135
So a circular collider 2D, It's pretty big.

111
00:06:14,135 --> 00:06:17,545
I'm going to put the radius above, take big.

112
00:06:17,540 --> 00:06:20,150
I'm going to, okay,

113
00:06:20,150 --> 00:06:22,510
So I think this radius is good enough.

114
00:06:22,505 --> 00:06:24,835
Let's see how this will be flying.

115
00:06:24,830 --> 00:06:26,360
So this is the complete circle.

116
00:06:26,360 --> 00:06:30,370
So I'm going to move it a little bit down,

117
00:06:30,365 --> 00:06:32,575
a little bit like that or okay, great.

118
00:06:32,570 --> 00:06:34,310
So now we have the collider,

119
00:06:34,310 --> 00:06:36,590
we make sure to make it a trigger.

120
00:06:36,590 --> 00:06:42,170
I'm going to save this and I'm going to go ahead into the o and add

121
00:06:42,170 --> 00:06:47,870
also the scripts of the enemy bullet controller.

122
00:06:47,870 --> 00:06:48,770
There we go.

123
00:06:48,770 --> 00:06:51,500
The boat speed will be, let's make it 34.

124
00:06:51,500 --> 00:06:53,960
Now, double-click on this, open this up.

125
00:06:53,960 --> 00:06:57,950
And now what we're going to do is create an on sugar enters,

126
00:06:57,950 --> 00:07:01,880
so on Trigger Enter to D.

127
00:07:01,880 --> 00:07:10,120
And here we are going to check if the collision.com tag.

128
00:07:10,115 --> 00:07:13,585
And if it's the player,

129
00:07:13,580 --> 00:07:16,790
obviously we'll need to create the stack in a bit.

130
00:07:16,790 --> 00:07:21,980
If it's the player will simply going to use the debug dot log.

131
00:07:21,980 --> 00:07:27,300
The layer was hit.

132
00:07:27,300 --> 00:07:31,390
And then we are simply going to straw this,

133
00:07:31,390 --> 00:07:33,570
whatever the target was,

134
00:07:33,565 --> 00:07:36,615
soul destroying game object.

135
00:07:36,610 --> 00:07:37,870
Save that.

136
00:07:37,870 --> 00:07:41,470
Now the final step is we want to move our bullet.

137
00:07:41,470 --> 00:07:43,450
And how are we going to move our bullet?

138
00:07:43,450 --> 00:07:46,240
Well, obviously we can use rigid body again,

139
00:07:46,240 --> 00:07:49,380
just like we did with the player bullet.

140
00:07:49,375 --> 00:07:51,565
But I think I'm this case,

141
00:07:51,565 --> 00:07:57,115
it's much better to use the old-fashioned way which is moved the transform of the book.

142
00:07:57,115 --> 00:07:58,995
First of all, there are many reasons.

143
00:07:58,990 --> 00:08:04,300
First of all, we can do this pretty easily and it is very effective.

144
00:08:04,300 --> 00:08:08,890
And the second reason is we don't want to have a lot of rigid bodies and our game,

145
00:08:08,890 --> 00:08:11,090
because whenever you have a rigid body,

146
00:08:11,090 --> 00:08:13,580
that means the physics engine is working.

147
00:08:13,580 --> 00:08:16,390
And when there is a physics engine is working,

148
00:08:16,385 --> 00:08:19,525
that means that there are more computational power.

149
00:08:19,520 --> 00:08:23,170
So the last we use the butter our game runs.

150
00:08:23,165 --> 00:08:26,245
So an update, I'm going to access the transform.

151
00:08:26,240 --> 00:08:29,570
I'm going to, I mean the position on the transform.

152
00:08:29,570 --> 00:08:32,690
And I'm going to keep increasing it.

153
00:08:32,690 --> 00:08:36,170
Going into the player's direction,

154
00:08:36,170 --> 00:08:38,540
time the bullet speed.

155
00:08:38,540 --> 00:08:45,040
So this speed, and I'm of course going to use time, delta time.

156
00:08:45,035 --> 00:08:49,075
So as soon as the bullet is instantiated,

157
00:08:49,070 --> 00:08:52,420
the direction of the player is found.

158
00:08:52,415 --> 00:08:58,015
And each and every single frame the transform is moved towards that player.

159
00:08:58,010 --> 00:09:00,190
And as soon as it hits the player,

160
00:09:00,185 --> 00:09:03,175
then the game object is destroyed.

161
00:09:03,170 --> 00:09:06,560
And if we had deep layer or any other object that gets destroyed,

162
00:09:06,560 --> 00:09:08,420
but if we had the player specifically,

163
00:09:08,420 --> 00:09:13,490
then the player was hit is going to be put or printed on the console.

164
00:09:13,490 --> 00:09:15,040
So I'm going to save this.

165
00:09:15,035 --> 00:09:17,725
I'm going to go back into my again.

166
00:09:17,720 --> 00:09:21,730
And let's see, we are going to make this a prefab.

167
00:09:21,725 --> 00:09:28,535
Prefab. I'm going to right-click and here and create a folder, 40 bullets.

168
00:09:28,535 --> 00:09:31,565
And the player bullet in here.

169
00:09:31,560 --> 00:09:37,660
Double-click in here, set the flying skeleton sort in here.

170
00:09:37,660 --> 00:09:39,760
I'm going to delete it.

171
00:09:39,760 --> 00:09:41,400
Let's save this.

172
00:09:41,395 --> 00:09:45,675
And let's go ahead and run the game and see what happens.

173
00:09:45,670 --> 00:09:49,350
So, okay, we didn't change the heap.

174
00:09:49,345 --> 00:09:52,785
We haven't changed the actual player bullets.

175
00:09:52,780 --> 00:09:53,440
So there we go.

176
00:09:53,440 --> 00:09:55,400
Let's add this flying skeleton,

177
00:09:55,404 --> 00:09:58,094
sort astron the game.

178
00:09:58,090 --> 00:10:01,590
And as you can see,

179
00:10:01,585 --> 00:10:07,815
nothing is happening because the bullet is on the wrong sorting layers.

180
00:10:07,810 --> 00:10:10,210
So at the enemy sorting layer,

181
00:10:10,210 --> 00:10:11,710
now we should save this.

182
00:10:11,710 --> 00:10:14,510
Now when we run, we should.

183
00:10:14,505 --> 00:10:20,305
Yep, there we go. You can see that we have these sorts flying in towards the player.

184
00:10:20,300 --> 00:10:21,830
And when we had the player,

185
00:10:21,830 --> 00:10:24,610
we don't have anything printed out to the console.

186
00:10:24,605 --> 00:10:28,855
Why is that? Well, let's check, will save this.

187
00:10:28,850 --> 00:10:34,130
So I forgot the most important step and what you should have done in the challenge,

188
00:10:34,130 --> 00:10:37,130
I assume that you would know what to do and the challenge,

189
00:10:37,130 --> 00:10:39,130
because we are checking for the player tag,

190
00:10:39,125 --> 00:10:42,065
but apparently by memory, isn't that great?

191
00:10:42,065 --> 00:10:48,095
So the problem here is that we did not tag the player as well, the player.

192
00:10:48,095 --> 00:10:51,205
As you can see, we already have the tag right here.

193
00:10:51,200 --> 00:10:54,490
So big mistake, no worries about that.

194
00:10:54,485 --> 00:10:55,625
We live, we learned,

195
00:10:55,625 --> 00:10:57,265
we apply our changes.

196
00:10:57,260 --> 00:10:59,090
We save all of this, we run the game.

197
00:10:59,090 --> 00:11:03,050
And now what we should see is that there you go.

198
00:11:03,050 --> 00:11:05,720
As soon as the sorted the player, we have,

199
00:11:05,720 --> 00:11:10,570
the player was hit and you can see it multiple times and here 4, 5, etc.

200
00:11:10,565 --> 00:11:13,615
Now, what I wanted to show you is if I move the player,

201
00:11:13,610 --> 00:11:18,410
the swords that are already going in the direction of the player won't change.

202
00:11:18,410 --> 00:11:21,230
So there you go. You can see that now sort

203
00:11:21,230 --> 00:11:24,320
follow the player as he moves and can we destroy them?

204
00:11:24,320 --> 00:11:25,300
Yep, we can.

205
00:11:25,295 --> 00:11:28,285
If you want, you can add another layer,

206
00:11:28,280 --> 00:11:30,430
which is the enemy bullet layer,

207
00:11:30,425 --> 00:11:35,065
and make sure that the bullets of the player don't interact with the bullet enemy.

208
00:11:35,060 --> 00:11:36,160
That's your choice.

209
00:11:36,155 --> 00:11:38,905
What game mechanics do you have in your game?

210
00:11:38,900 --> 00:11:40,180
Do that, apply it?

211
00:11:40,175 --> 00:11:42,295
I would love to see it if you show me.

212
00:11:42,290 --> 00:11:44,350
And was that none, I hope you enjoy it.

213
00:11:44,345 --> 00:11:48,805
I hope you are committing your changes every single video.

214
00:11:48,800 --> 00:11:53,480
And in the next one we are going to add a range to the enemy

215
00:11:53,480 --> 00:11:58,760
shooting so we don't want to let our ME sures from wherever on the map we want to have a,

216
00:11:58,760 --> 00:12:02,300
some kind of range in which the enemy can shoot us.

217
00:12:02,300 --> 00:12:05,370
So I'll see you then.

