1
00:00:03,120 --> 00:00:04,380
It's like just a nice big one.

2
00:00:04,380 --> 00:00:09,330
We're going to go through the process of actually shooting our bullets and flying across the screen,

3
00:00:09,330 --> 00:00:11,190
making sure that we've figured out collision.

4
00:00:11,190 --> 00:00:13,320
And when they hit the enemy, they destroy the enemy.

5
00:00:13,320 --> 00:00:14,340
Let's get started.

6
00:00:14,340 --> 00:00:17,190
The bullet we have floating around in our scene, we can get rid of.

7
00:00:17,190 --> 00:00:17,940
We don't need it in there.

8
00:00:17,940 --> 00:00:19,740
We can work directly on the prefab.

9
00:00:19,740 --> 00:00:21,450
And I'm going to click on that prefab.

10
00:00:21,450 --> 00:00:22,350
Let's see on here.

11
00:00:22,350 --> 00:00:23,460
I'm going to add some components.

12
00:00:23,460 --> 00:00:28,140
First of all, we need to get ourselves a rigid body, 2D, so that we can have this fly across the

13
00:00:28,140 --> 00:00:29,730
screen or leave it as type dynamic.

14
00:00:29,730 --> 00:00:30,750
We'll see how that goes.

15
00:00:30,750 --> 00:00:31,620
It should be okay.

16
00:00:31,620 --> 00:00:35,880
I don't want it to have gravity, although you could if you wanted to have like really kind of droop

17
00:00:35,880 --> 00:00:36,120
off.

18
00:00:36,120 --> 00:00:36,810
That would be funny.

19
00:00:36,810 --> 00:00:37,680
I think so.

20
00:00:37,710 --> 00:00:38,400
Put it zero.

21
00:00:38,400 --> 00:00:40,650
You know, maybe we'll come back to Gravity and play around with it.

22
00:00:40,650 --> 00:00:46,110
This is the art of iterative game design, let's say to add a component which will be some sort of collider.

23
00:00:46,110 --> 00:00:52,500
So I will do a C, I think a capsule collider two D because that's roughly the shape of the player.

24
00:00:52,530 --> 00:00:56,340
I'm going to click into the prefabs, I double click on the prefab.

25
00:00:56,340 --> 00:00:57,690
Here, I can see the collider.

26
00:00:57,690 --> 00:00:59,100
It's a bit messy in its shape.

27
00:00:59,100 --> 00:01:03,390
Click on the collider, edit and then just edit in that.

28
00:01:03,390 --> 00:01:06,540
So we've got something that resembles the player.

29
00:01:07,800 --> 00:01:11,160
Jump back out of the prefab and we need to create ourselves a script.

30
00:01:11,160 --> 00:01:15,420
So what the player is doing with the player movement, with the gun, it's saying, Hey, I brought

31
00:01:15,420 --> 00:01:16,440
this bullet to life.

32
00:01:16,440 --> 00:01:18,570
Now what it does, well, that's up to it.

33
00:01:18,570 --> 00:01:19,560
That's its business.

34
00:01:19,560 --> 00:01:21,420
So I'm going to jump over in the scripts folder.

35
00:01:21,420 --> 00:01:26,100
I'm going to right click create a new C-sharp script, which I will call a bullet.

36
00:01:26,490 --> 00:01:30,090
Obviously, if you have more than one type of gun, more than one type of bullet, you might have to

37
00:01:30,090 --> 00:01:32,520
have different naming, different functionality.

38
00:01:32,520 --> 00:01:33,930
But for us, we're keeping it pretty simple.

39
00:01:33,930 --> 00:01:36,930
Yeah, we just the player can run a long jump and shoot bullets.

40
00:01:37,020 --> 00:01:38,820
I think that's going to be cool for our game.

41
00:01:38,820 --> 00:01:43,230
Let's double click on Bullet, clean up our script a little bit by getting rid of the comments, doing

42
00:01:43,230 --> 00:01:47,730
a little bit of formatting, and to have our bullet fly across the screen, we need to get a reference

43
00:01:47,730 --> 00:01:48,660
to the rigid body.

44
00:01:48,660 --> 00:01:50,400
So I think I want to cash that.

45
00:01:50,400 --> 00:01:58,620
So we will start by creating a rigid body to DX and I'll call this my rigid body, as is our convention

46
00:01:58,620 --> 00:02:05,490
in this section and semicolon my rigid body, I think I've felt that right and then start my rigid body

47
00:02:05,670 --> 00:02:07,560
equals get component.

48
00:02:07,560 --> 00:02:08,970
What sort of component is it?

49
00:02:08,970 --> 00:02:10,380
Well, it's a rigid body.

50
00:02:10,380 --> 00:02:16,800
Two dx parentheses semicolon and in update, what we want to say is, well, make this thing fly across

51
00:02:16,800 --> 00:02:17,190
the screen.

52
00:02:17,190 --> 00:02:19,920
We've done this before with how we run, but this is going to be a lot more simple.

53
00:02:19,920 --> 00:02:21,870
We're just going to jump into my rigid body.

54
00:02:22,170 --> 00:02:26,910
What what thing do we need to do to give something, a movement across the screen?

55
00:02:27,030 --> 00:02:28,500
We can use velocity.

56
00:02:29,560 --> 00:02:34,840
Equals new vector two because we need the two vectors and the x and the y.

57
00:02:34,840 --> 00:02:36,190
We need to tell it what's going on.

58
00:02:36,190 --> 00:02:44,110
In parentheses we will say just we'll start off with some baked in parameters here will say one f comma

59
00:02:44,110 --> 00:02:49,900
zero f to say we want you to fly to the right by one each frame and nothing up and down.

60
00:02:49,900 --> 00:02:54,580
We'll save that and we'll make sure that it's all hooked up properly before we get much more clever

61
00:02:54,580 --> 00:02:55,660
with what we're trying to do.

62
00:02:55,690 --> 00:02:58,330
We'll find our prefabs.

63
00:02:58,330 --> 00:02:59,260
Bullet.

64
00:03:00,030 --> 00:03:05,340
We will add a component which we will call bullet or we won't call it that will search for bullet at

65
00:03:05,340 --> 00:03:06,540
our bullet script on there.

66
00:03:06,570 --> 00:03:08,160
That should be pretty cool.

67
00:03:08,190 --> 00:03:13,650
Now we'll see when we instantiate or spawn or bring to life or birth our bullets.

68
00:03:13,650 --> 00:03:14,820
What's going to happen?

69
00:03:14,820 --> 00:03:15,410
Whoa.

70
00:03:15,420 --> 00:03:18,390
They kind of fly and across the screen.

71
00:03:18,390 --> 00:03:18,690
Good.

72
00:03:18,990 --> 00:03:20,340
So functionality done.

73
00:03:20,340 --> 00:03:21,060
Mission accomplished.

74
00:03:21,060 --> 00:03:22,200
That's all I wanted to do.

75
00:03:22,230 --> 00:03:23,820
There's our game right there.

76
00:03:24,060 --> 00:03:25,620
Now we've got more to do than that.

77
00:03:25,620 --> 00:03:26,950
That's a starting point.

78
00:03:26,970 --> 00:03:30,180
We can do some parameter rising of these things.

79
00:03:30,180 --> 00:03:35,250
So instead of just baking in there, one, we want to be able to change that up to have some sort of

80
00:03:35,250 --> 00:03:35,760
tuning.

81
00:03:35,760 --> 00:03:38,790
So I will call this bullet speed.

82
00:03:38,790 --> 00:03:40,740
I think it's a good name for that bullet speed.

83
00:03:40,740 --> 00:03:41,730
Jump up to the top.

84
00:03:41,730 --> 00:03:48,930
Very first thing at the top we will serialize field and I will create a float called bullet speed.

85
00:03:49,050 --> 00:03:54,390
Will initialize that at something faster than what we had just there will say 20 F now we'll save and

86
00:03:54,390 --> 00:03:55,890
we'll see if that has worked.

87
00:03:55,890 --> 00:04:00,720
If we can parameter rise and shoot across the screen with some velocity.

88
00:04:01,380 --> 00:04:04,710
Pupu are nice and I like that it's rotating.

89
00:04:04,710 --> 00:04:06,300
That wasn't my objective.

90
00:04:06,300 --> 00:04:13,740
What you could do on your bullet is to find your rigid body and you could put some constraints on that

91
00:04:13,740 --> 00:04:16,529
where you can freeze the rotation and we'll give that a go.

92
00:04:16,529 --> 00:04:18,360
Actually, we'll see what happens when we freeze it.

93
00:04:18,360 --> 00:04:22,890
I'm also going to change the collision detection to be continuous because we want to make sure these

94
00:04:22,890 --> 00:04:27,120
things don't go flying through the things we want to be shooting.

95
00:04:27,120 --> 00:04:28,200
So shoot.

96
00:04:28,200 --> 00:04:29,280
Okay, it's going straight.

97
00:04:29,280 --> 00:04:30,630
That's got a different kind of look to it.

98
00:04:30,630 --> 00:04:33,210
It's kind of like a mirror of me flying across the screen.

99
00:04:33,210 --> 00:04:35,580
So rotation or not rotation, it's up to you.

100
00:04:35,610 --> 00:04:39,900
I'm going to have it not rotated for the moment and that might seem like mission accomplished.

101
00:04:39,900 --> 00:04:43,470
However, I know one thing that's going to go astray up here.

102
00:04:43,470 --> 00:04:44,760
If we're shooting right, we shoot right.

103
00:04:44,760 --> 00:04:46,500
If we're shooting left, we also shoot right.

104
00:04:46,500 --> 00:04:47,880
So we need to figure that out.

105
00:04:47,880 --> 00:04:52,680
And I'm going to do a solution based upon what we've already got in our code.

106
00:04:52,680 --> 00:04:54,210
So jump back into the script.

107
00:04:54,210 --> 00:04:59,520
What I'm going to look for is a way of saying what is the current local scale of the player?

108
00:04:59,520 --> 00:05:04,890
So if the player's local scale is minus one, then we want the bullets to shoot left.

109
00:05:04,890 --> 00:05:07,380
If it's plus one, we want the bullets to shoot right.

110
00:05:07,380 --> 00:05:12,540
The same sort of philosophy we've been using throughout in terms of flipping the player and flipping

111
00:05:12,540 --> 00:05:13,530
the enemy.

112
00:05:13,530 --> 00:05:18,180
So what I need to do is to get a reference to the player, and I'm going to do it in a fairly simple

113
00:05:18,180 --> 00:05:18,450
way.

114
00:05:18,450 --> 00:05:20,280
Well, I think it's fairly conceptually simple.

115
00:05:20,280 --> 00:05:23,060
So what I need to do, first of all, is get a reference to the player.

116
00:05:23,100 --> 00:05:25,320
There's a couple of ways that we've done this throughout the course.

117
00:05:25,320 --> 00:05:31,230
We can tag the player as player and then look for the tag, or we can look for a component that is the

118
00:05:31,230 --> 00:05:33,270
player that is only on the player.

119
00:05:33,270 --> 00:05:36,210
So player inputs only on the player, player movements only on the player.

120
00:05:36,210 --> 00:05:37,290
So I'm going to do it that way.

121
00:05:37,290 --> 00:05:40,710
Just to show you a little bit of flavor, there's a few ways to do these things.

122
00:05:40,710 --> 00:05:46,950
Let's look for a player movement object in our world, and there should only be one because there can

123
00:05:46,950 --> 00:05:48,180
only be one player.

124
00:05:48,180 --> 00:05:49,620
So play a movement player.

125
00:05:49,620 --> 00:05:51,510
So it's a type player movement.

126
00:05:51,510 --> 00:05:55,410
Therefore it's going to be the only thing we're going to be getting is the player.

127
00:05:55,410 --> 00:06:04,740
And in start, I'm going to assign that by saying player equals find object of type player movement

128
00:06:05,340 --> 00:06:07,380
and parentheses semicolon.

129
00:06:07,740 --> 00:06:11,040
And doing a find objective type I think is okay every now and again.

130
00:06:11,040 --> 00:06:12,750
If we're doing it in start, that's okay.

131
00:06:12,750 --> 00:06:16,500
You don't really want to have a fine objective type in update that you're doing.

132
00:06:16,500 --> 00:06:21,390
Many, many objects are doing many, many things on every single frame, but in start it's only when

133
00:06:21,390 --> 00:06:22,110
you click the button.

134
00:06:22,110 --> 00:06:25,440
I think this is totally fine from a performance perspective.

135
00:06:25,440 --> 00:06:26,220
This is cool.

136
00:06:26,730 --> 00:06:27,750
What are we going to do with this?

137
00:06:27,750 --> 00:06:34,140
Well, I need to create some sort of x direction or x speed will say so x speed.

138
00:06:34,140 --> 00:06:36,780
And when I say x speed, I made the speed on the x axis.

139
00:06:36,780 --> 00:06:40,680
We're not worried about the y axis is going to equal player.

140
00:06:41,250 --> 00:06:42,810
Remember, that's our player in the scene.

141
00:06:42,810 --> 00:06:44,550
Player transform.

142
00:06:44,580 --> 00:06:45,810
It's the transform component.

143
00:06:45,810 --> 00:06:47,730
Dot what what are we looking for here?

144
00:06:47,730 --> 00:06:48,960
What do we need to know?

145
00:06:48,990 --> 00:06:51,480
We need to know the local scale.

146
00:06:51,570 --> 00:06:54,960
We need to know whether it's positive or negative, to say whether you're facing to the right or the

147
00:06:54,960 --> 00:06:55,500
left.

148
00:06:55,650 --> 00:06:58,200
DOT We're just interested in X.

149
00:06:58,200 --> 00:06:59,970
So local scale is a vector three.

150
00:06:59,970 --> 00:07:05,250
We just need to know the X value because we just want this to, to be one value or one float value.

151
00:07:05,250 --> 00:07:07,350
And I'll multiply that in here.

152
00:07:07,350 --> 00:07:10,830
So I'll say the x speed will multiply that by the bullet speed.

153
00:07:10,830 --> 00:07:16,590
So in one hit right in here, when the bullet comes to life, remember, we only need to know the direction

154
00:07:16,590 --> 00:07:20,940
when it comes to life, when it's instantiated, we're going to say, What is your X speed?

155
00:07:20,970 --> 00:07:27,210
It's going to be the direction of the player based upon the local scale times by the bullet speed.

156
00:07:27,210 --> 00:07:32,130
Now I need to create this x speed as a variable that we can use within update.

157
00:07:32,130 --> 00:07:36,240
So I will create a float x speed semicolon.

158
00:07:36,240 --> 00:07:41,730
Now in our update, instead of flying along at bullet speed, we're flying along an x speed click save.

159
00:07:41,730 --> 00:07:42,810
We'll see if this works.

160
00:07:42,810 --> 00:07:43,980
I have a feeling it will.

161
00:07:44,610 --> 00:07:45,690
Strong feeling.

162
00:07:45,810 --> 00:07:48,900
Pop the player up there so I don't need to jump click on play.

163
00:07:49,760 --> 00:07:50,840
We shoot the right.

164
00:07:50,840 --> 00:07:52,110
Shoot, shoot, shoot to the left.

165
00:07:52,130 --> 00:07:52,430
Shoot.

166
00:07:52,430 --> 00:07:52,740
Shoot.

167
00:07:52,760 --> 00:07:53,240
Excellent.

168
00:07:53,240 --> 00:07:56,280
So that's exactly what I wanted to do there.

169
00:07:56,310 --> 00:07:56,650
Okay.

170
00:07:56,660 --> 00:07:59,630
And the last step of the process, we don't really want our bullets sticking around.

171
00:07:59,630 --> 00:08:01,510
I think when it hits the wall, it should disappear.

172
00:08:01,520 --> 00:08:03,830
Likewise, when it hits the enemy, it should disappear.

173
00:08:03,830 --> 00:08:05,930
Probably do some damage, maybe even kill the enemy.

174
00:08:05,930 --> 00:08:07,100
Let's just do killing enemy.

175
00:08:07,100 --> 00:08:08,690
We won't worry about damage for now.

176
00:08:08,690 --> 00:08:10,910
You know how to do damage from other sections of the course.

177
00:08:10,910 --> 00:08:12,710
So we're just going to straight up hit the enemy.

178
00:08:12,710 --> 00:08:13,280
You kill it.

179
00:08:13,280 --> 00:08:19,310
We can resolve that in either the enemy saying if a bullet hits me, I should die, or the bullet saying

180
00:08:19,310 --> 00:08:22,310
If I hit an enemy, then I want the enemy to die.

181
00:08:22,310 --> 00:08:26,270
And because we're in the bullet script, I'm going to do it that the bullet owns that conversation.

182
00:08:26,270 --> 00:08:27,860
If you hit an enemy, do this.

183
00:08:27,860 --> 00:08:29,150
If you hit a wall, do this.

184
00:08:29,150 --> 00:08:30,540
If you hit something else, do this.

185
00:08:30,560 --> 00:08:37,309
So down underneath update we will void on trigger enter to DX.

186
00:08:37,340 --> 00:08:42,380
So when you collide with something, the first thing I want to know is if what if the other thing is

187
00:08:42,380 --> 00:08:42,770
an enemy.

188
00:08:42,770 --> 00:08:45,020
Let's go have a look at the enemy and see what we can grab.

189
00:08:45,020 --> 00:08:46,130
I'm going to click on the enemy.

190
00:08:46,130 --> 00:08:47,690
I'm going to go to Tag.

191
00:08:47,690 --> 00:08:53,120
So I'm going to add a tag and it tags I'll hit plus and we'll call this enemy enemies.

192
00:08:53,120 --> 00:08:54,230
What are we doing in the past?

193
00:08:54,380 --> 00:08:55,730
Call it enemies.

194
00:08:56,420 --> 00:08:58,250
Call it enemy because this is a tag.

195
00:08:58,250 --> 00:08:59,420
This is one enemy.

196
00:08:59,420 --> 00:09:00,500
Okay, that makes sense there.

197
00:09:00,560 --> 00:09:02,720
So it's just one single enemy.

198
00:09:02,720 --> 00:09:04,010
And so we're mixing a few things here.

199
00:09:04,010 --> 00:09:08,120
We're using tags, we're using layers, we're using a bunch of stuff, which is cool to see how these

200
00:09:08,120 --> 00:09:08,810
things work.

201
00:09:08,810 --> 00:09:14,300
So within here, if the what well, we know when we bump into something, we trigger something, we

202
00:09:14,300 --> 00:09:19,430
get information about the other thing, the collider type collider, the other thing we bumped into.

203
00:09:19,430 --> 00:09:26,240
So if the other dot tag equals equals means the same as or equals.

204
00:09:26,720 --> 00:09:29,390
And in our parentheses we called it just now enemy.

205
00:09:29,420 --> 00:09:30,890
Make sure you check the spelling on that.

206
00:09:30,890 --> 00:09:32,480
If it doesn't work, check the spelling.

207
00:09:32,480 --> 00:09:34,160
Yes, it is enemy on our tag.

208
00:09:34,160 --> 00:09:35,300
Then what do we want to do?

209
00:09:35,300 --> 00:09:39,050
Well, I'm just going to straight up say destroy.

210
00:09:39,260 --> 00:09:42,860
What are we destroying while we're destroying the other thing?

211
00:09:42,860 --> 00:09:44,720
Dot game object.

212
00:09:44,720 --> 00:09:46,250
So what is the game object?

213
00:09:46,250 --> 00:09:47,810
Just destroy it, get rid of it.

214
00:09:47,810 --> 00:09:50,120
And then the last thing I want to do is destroy the bullet.

215
00:09:50,120 --> 00:09:51,410
So the bullet doesn't hang around.

216
00:09:51,410 --> 00:09:53,360
Destroy the bullet if it bumps into an enemy.

217
00:09:53,360 --> 00:09:56,390
So how would we go about doing that little micro challenge for you?

218
00:09:57,770 --> 00:10:06,170
We'd simply say destroy an in parentheses game object, so destroy me basically back over into unity.

219
00:10:06,170 --> 00:10:08,630
And I forgot to do this before I went back to my code.

220
00:10:08,630 --> 00:10:13,070
But click on the enemy change from untagged to enemy so that we know it is the enemy.

221
00:10:13,070 --> 00:10:15,290
Now click on play when we get into the world.

222
00:10:15,290 --> 00:10:19,070
I suspect the bullets are going to stick around if we hit the wall because they are not triggers.

223
00:10:19,070 --> 00:10:19,670
Yes, they are.

224
00:10:19,680 --> 00:10:20,090
That's fine.

225
00:10:20,090 --> 00:10:21,410
For the moment, we shoot the enemy.

226
00:10:21,410 --> 00:10:22,910
It shoots and kills the enemy.

227
00:10:22,910 --> 00:10:25,040
The bullet that hit the enemy died, which is great.

228
00:10:25,040 --> 00:10:26,720
And our bullets stick around.

229
00:10:26,720 --> 00:10:32,540
And I think we can solve that very simply by saying, if you collide with anything, then destroy yourself.

230
00:10:32,540 --> 00:10:33,710
So let's do that.

231
00:10:33,710 --> 00:10:41,600
Just simply on collision into 2D, we'll just say once again destroy and we'll destroy the game object.

232
00:10:41,600 --> 00:10:43,430
So a couple of destroys in here.

233
00:10:43,430 --> 00:10:45,080
You trigger something important.

234
00:10:45,080 --> 00:10:47,330
If it's an enemy, call and destroy yourself.

235
00:10:47,330 --> 00:10:50,030
If you just bump into a wall, then destroy yourself as well.

236
00:10:50,030 --> 00:10:54,260
Let's make sure that we're not accidentally bumping into the player when we shoot or anything else.

237
00:10:54,260 --> 00:10:55,160
We just want to make that.

238
00:10:55,160 --> 00:10:59,090
When we shoot into walls, the bullet disappears and we don't have it sticking around.

239
00:10:59,090 --> 00:11:00,200
Click on play.

240
00:11:01,650 --> 00:11:04,480
And it looks like the bullets are colliding with the ground.

241
00:11:04,500 --> 00:11:05,370
I think.

242
00:11:06,410 --> 00:11:07,820
Now let's go into play a pre-fab.

243
00:11:07,840 --> 00:11:10,810
Before we do that, we'll just make sure we've applied any overrides.

244
00:11:10,810 --> 00:11:15,730
I tend to just work on the instance a bunch and then every now and again apply those back.

245
00:11:15,730 --> 00:11:17,890
We'll go into the prefab for this one.

246
00:11:17,890 --> 00:11:24,040
We will click on the gun and move it down a little bit, back a little bit just so it's not bumping

247
00:11:24,040 --> 00:11:26,140
onto the ground back out of here.

248
00:11:26,140 --> 00:11:30,610
I will also click on the bullet prefab from our folders just make it a little bit smaller.

249
00:11:30,610 --> 00:11:36,160
So 0.75 on the X and 0.75 on the Y.

250
00:11:36,190 --> 00:11:39,820
Now we shouldn't be touching the ground when we shoot it out from us.

251
00:11:39,850 --> 00:11:41,440
We shouldn't be touching ourselves.

252
00:11:41,440 --> 00:11:41,950
There we go.

253
00:11:41,950 --> 00:11:42,610
We can shoot, right?

254
00:11:42,610 --> 00:11:43,540
We can shoot left.

255
00:11:43,570 --> 00:11:46,630
It's a little bit fast, maybe, but can we shoot the enemy?

256
00:11:46,630 --> 00:11:48,280
Destroy the enemy and destroy the bullet?

257
00:11:48,280 --> 00:11:50,950
So we've got the behavior we wanted from our bullets.

258
00:11:50,980 --> 00:11:54,340
It kind of looks a little bit funny, a little bit quicky shooting out little impressions of yourself.

259
00:11:54,340 --> 00:11:55,300
I like it as a bullet.

260
00:11:55,300 --> 00:11:56,080
I think it's cool.

261
00:11:56,080 --> 00:12:00,130
I look forward to see what bullets you're shooting out of your player, so please do share in the discussions

262
00:12:00,130 --> 00:12:01,330
and I'll see you in the next lecture.

