1
00:00:03,930 --> 00:00:06,140
Well game is now coming along nicely.

2
00:00:06,150 --> 00:00:09,340
We can dodge the enemies, and if we hit them, we die.

3
00:00:09,360 --> 00:00:14,490
But now let's step it up a notch by adding some projectiles so we can both shoot at the enemies and

4
00:00:14,490 --> 00:00:16,050
they can shoot back us.

5
00:00:16,740 --> 00:00:21,690
Before we start coding up this system, though, there's a little bit of prep work we need to do beforehand.

6
00:00:21,690 --> 00:00:26,490
The first thing we want to do is to create some projectiles that we can instantiate into our world.

7
00:00:26,700 --> 00:00:33,090
So over in our hierarchy, let's right click and create a new empty game object and call this the projectile.

8
00:00:33,570 --> 00:00:34,830
Let's reset the transform.

9
00:00:34,830 --> 00:00:40,740
If it didn't come in at 000 and then underneath this object, we're going to add our sprite.

10
00:00:40,980 --> 00:00:43,920
We have a whole bunch of sprites in our assets pack folder.

11
00:00:43,920 --> 00:00:49,710
So if we dig through into the PNG folder and then into lasers, we have a few options here.

12
00:00:49,710 --> 00:00:53,310
We have a whole bunch of blue lasers, green lasers and red lasers.

13
00:00:53,310 --> 00:00:59,100
But because our player is predominantly blue, I'm going to go with one of the blue options and I quite

14
00:00:59,100 --> 00:01:04,290
like the look of this laser blue oh seven So I'm going to drag that underneath my projectile.

15
00:01:04,739 --> 00:01:10,680
This is pretty small in our game world, so let's scale it up a bit and I'm going to go with twice the

16
00:01:10,680 --> 00:01:11,040
size.

17
00:01:11,040 --> 00:01:14,010
So just scale that up to two on the X, Y and Z.

18
00:01:14,400 --> 00:01:18,960
And then if we head back to the root of this object, we need to think about some of the components

19
00:01:18,960 --> 00:01:20,220
we're going to want to add.

20
00:01:20,460 --> 00:01:24,570
Well, this projectile needs to be able to interact with our enemies in the world.

21
00:01:24,570 --> 00:01:27,840
So we're going to be setting this up as a trigger collider.

22
00:01:27,960 --> 00:01:34,530
This means we're going to need a rigid body, 2D, and just like our other objects, let's set that

23
00:01:34,530 --> 00:01:36,120
to be kinematic.

24
00:01:36,480 --> 00:01:40,770
And we're also going to need a type of collider given the shape of our projectile.

25
00:01:40,770 --> 00:01:46,920
I think a capsule collider two DX will do the job and if we zoom in on our projectile, then we can

26
00:01:46,920 --> 00:01:50,610
go ahead and edit the collider to fit our projectile.

27
00:01:51,300 --> 00:01:53,130
So let's just drag in our handles.

28
00:01:53,130 --> 00:01:57,570
And if you're wondering how I'm managing to do both sides at the same time, just need to hold out the

29
00:01:57,570 --> 00:01:58,800
alt key for that.

30
00:01:58,890 --> 00:02:05,070
With that looking about right, the last component we want to add is going to be our damage data because

31
00:02:05,070 --> 00:02:09,720
our player projectiles are going to need to be able to damage the enemies and the enemy projectiles,

32
00:02:09,720 --> 00:02:11,280
which will be very similar to this.

33
00:02:11,280 --> 00:02:12,870
We'll need to damage our player.

34
00:02:13,290 --> 00:02:16,230
Let's leave the default damage at ten for now.

35
00:02:16,680 --> 00:02:20,310
And with all of that set up, we can go ahead and prefab our projectile.

36
00:02:22,520 --> 00:02:25,970
But it's just drag that over into our Prefabs folder.

37
00:02:26,450 --> 00:02:32,870
And I'm actually going to rename this prefab projectile player because now we're going to create a second

38
00:02:32,870 --> 00:02:34,280
one for the enemy.

39
00:02:34,430 --> 00:02:38,360
And this really is as simple as duplicating our prefab with control.

40
00:02:38,360 --> 00:02:42,590
DX And then let's rename this to Projectile Enemy.

41
00:02:43,440 --> 00:02:49,320
If we open up this projector, we're going to want to change our laser blue prefab to one that suits

42
00:02:49,320 --> 00:02:50,400
our enemies.

43
00:02:51,410 --> 00:02:55,190
So let's head back into our asset packs into the lasers folder.

44
00:02:56,030 --> 00:03:01,340
And for this one, I'm going to use the same type of laser, but I'm going to go with the laser red

45
00:03:01,490 --> 00:03:02,090
seven.

46
00:03:03,070 --> 00:03:08,710
So let's drag that in, delete our blue prefab and then rescale our red one.

47
00:03:09,160 --> 00:03:13,300
And since we duplicated our original projectile, we don't have to worry about setting up all of the

48
00:03:13,300 --> 00:03:15,550
colliders and the damage data.

49
00:03:15,820 --> 00:03:19,630
One thing I am noticing, though, is that the colliders should be triggers.

50
00:03:19,630 --> 00:03:23,200
So let's actually go ahead and just fix that on both of our objects.

51
00:03:23,440 --> 00:03:26,380
So is trigger on our enemy projectile?

52
00:03:28,560 --> 00:03:33,030
And if we jump into our projectile, we can set this to a trigger as well.

53
00:03:33,480 --> 00:03:36,120
So that's our projectile mostly set up.

54
00:03:36,120 --> 00:03:40,800
But if we do leave it like this, we're going to run into a bit of a problem when we start instantiating

55
00:03:40,800 --> 00:03:41,970
them via the code.

56
00:03:42,420 --> 00:03:47,520
And to show you what I mean, let's go ahead and hit play and then pause very quickly.

57
00:03:47,970 --> 00:03:52,440
And if we move our projectile around, so let's zoom out so we can see the full level.

58
00:03:54,300 --> 00:03:56,850
And move all projectile out of the way of the enemies.

59
00:03:57,300 --> 00:04:03,390
If we unpause our game, our projectile can actually interact and hit our player.

60
00:04:03,480 --> 00:04:07,320
This deletes our projectile from the scene and damages our player.

61
00:04:07,590 --> 00:04:09,540
And this definitely isn't what we want.

62
00:04:09,570 --> 00:04:15,150
We only want the player projectiles to interact with the enemies and the enemy, projectiles to interact

63
00:04:15,150 --> 00:04:16,140
with the player.

64
00:04:16,170 --> 00:04:18,329
So how do we go about doing this?

65
00:04:18,540 --> 00:04:22,760
Well, the first step is to set these objects to be on specific layers.

66
00:04:22,770 --> 00:04:28,050
So if we click on any object in our scene, we can see up in the top of the inspector that they're all

67
00:04:28,050 --> 00:04:29,430
on layer default.

68
00:04:29,460 --> 00:04:33,780
If we click on this dropdown and then head to the bottom add layer option.

69
00:04:33,930 --> 00:04:37,280
What we're going to do is we're going to set up two new layers.

70
00:04:37,290 --> 00:04:40,620
We're going to sell one for the player and one for the enemy.

71
00:04:40,920 --> 00:04:45,060
With those two sets up, let's click out of our inspector so that that disappears.

72
00:04:45,300 --> 00:04:51,870
And now in our prefabs folder, let's select our enemy and our enemy projectile and set both of their

73
00:04:51,870 --> 00:04:53,880
layers to be the enemy layer.

74
00:04:54,450 --> 00:04:59,160
This will give us a warning message asking whether we want to change the children or just the parent.

75
00:04:59,190 --> 00:05:01,320
Let's go ahead and change the children as well.

76
00:05:01,770 --> 00:05:05,550
And then we want to do the exact same thing for our player and our player projectile.

77
00:05:05,910 --> 00:05:09,660
But this time we're going to set our layer to be the player layer.

78
00:05:10,020 --> 00:05:12,330
Again, we can go ahead and change the children.

79
00:05:12,570 --> 00:05:18,810
And the reason we want to do this is because we can tell unity what objects should interact with each

80
00:05:18,810 --> 00:05:20,340
other based on their layer.

81
00:05:20,340 --> 00:05:25,860
So we can tell the player projectile not to interact with our player ship and we can tell the enemies

82
00:05:25,860 --> 00:05:27,630
not to interact with each other as well.

83
00:05:27,960 --> 00:05:32,990
To do this, we need to head up to edit and then down to product settings.

84
00:05:33,000 --> 00:05:35,790
And on the left side, we have physics to.

85
00:05:36,270 --> 00:05:40,770
Now we also have a physics option as well, which has some very similar stuff in there.

86
00:05:40,800 --> 00:05:44,730
But because our project is 2D, we want the physics 2D option.

87
00:05:45,180 --> 00:05:50,250
Then if we scroll to the bottom of this list, we'll find the layer collision matrix.

88
00:05:50,250 --> 00:05:54,900
If you can't see that, then just expand this option and then scroll down a little bit.

89
00:05:55,230 --> 00:06:00,090
And this collision matrix here tells Unity which layers are allowed to interact with each other.

90
00:06:00,270 --> 00:06:04,350
And there's various different reasons why you might want to change things in here depending on the project

91
00:06:04,350 --> 00:06:05,250
you're working on.

92
00:06:05,250 --> 00:06:10,560
But we said that we didn't want the player projectiles to interact with our player ship and the enemies

93
00:06:10,560 --> 00:06:12,120
to interact with each other as well.

94
00:06:12,540 --> 00:06:18,150
So in this matrix, we want to say that the enemy can interact with the player, but it can't interact

95
00:06:18,150 --> 00:06:23,190
with other enemies, and the player can interact with things on the enemy layer, but it can't interact

96
00:06:23,190 --> 00:06:25,140
with other things on the player layer.

97
00:06:25,470 --> 00:06:30,780
With those options set, we've also fixed another minor bug that was in our game before.

98
00:06:30,810 --> 00:06:35,730
You may or may not have noticed this depending on how you set up your waves, but after we added our

99
00:06:35,730 --> 00:06:40,620
damage data and our health system, it turns out that the enemy could collide with another enemy and

100
00:06:40,620 --> 00:06:41,460
destroy each other.

101
00:06:41,460 --> 00:06:44,400
So this will also fix little problems like that.

102
00:06:44,430 --> 00:06:49,710
With that set, let's close our project settings and now let's test this out to show that it actually

103
00:06:49,710 --> 00:06:50,370
works.

104
00:06:50,400 --> 00:06:52,470
Let's move our projectile and hit play.

105
00:06:52,860 --> 00:06:57,000
And now if we drag our projectile over our player, nothing happens.

106
00:06:57,000 --> 00:07:02,370
But if we interact with an enemy, then the projectile gets destroyed and the enemy will take some damage.

107
00:07:02,400 --> 00:07:08,310
Next, you may or may not have seen something odd, so let's go ahead and hit play again and notice

108
00:07:08,310 --> 00:07:13,320
that when we move the projectile over the player, the projectile is on top of the player.

109
00:07:13,350 --> 00:07:18,630
This looks a bit strange and we really want the projectile to be instantiated underneath our ships rather

110
00:07:18,630 --> 00:07:19,800
than on top of them.

111
00:07:19,800 --> 00:07:23,790
So have a very quick think about how you might go about fixing this.

112
00:07:26,290 --> 00:07:26,760
Okay.

113
00:07:26,770 --> 00:07:28,130
I'm not going to give you too long.

114
00:07:28,150 --> 00:07:34,210
What we're going to do is in our projectiles, if we head down to our sprite, we want to make sure

115
00:07:34,210 --> 00:07:36,940
that the projectiles are on our order.

116
00:07:36,940 --> 00:07:37,720
Zero.

117
00:07:37,750 --> 00:07:41,350
This should be the case by default, but let's double check both of them.

118
00:07:41,620 --> 00:07:46,450
And to make sure that these appear underneath our ships, all we need to do is bump up the order in

119
00:07:46,450 --> 00:07:47,740
layer for our ships.

120
00:07:48,100 --> 00:07:54,070
So let's head into our enemy ship and change the layer in order to one and then head into our player

121
00:07:54,070 --> 00:07:58,480
ship and again, change the sprite order in layer to one.

122
00:07:59,770 --> 00:08:01,870
Now, if we hit play one more time.

123
00:08:03,420 --> 00:08:10,320
If we zoom in on our player and move our projectile, we'll now see that the projectile appears beneath

124
00:08:10,320 --> 00:08:11,160
the player.

125
00:08:11,550 --> 00:08:16,380
So when we eventually get around to instantiating this in code in the next lecture, the projectile

126
00:08:16,380 --> 00:08:21,480
will appear underneath the player and then shoot out and eventually hit an enemy.

127
00:08:22,330 --> 00:08:25,500
So that's our projectile pretty much sets up.

128
00:08:25,510 --> 00:08:30,490
We can go ahead and delete our prefab from our hierarchy because we're going to instantiate those in

129
00:08:30,490 --> 00:08:31,780
code in the next lecture.

130
00:08:31,930 --> 00:08:36,909
But before we finish up, there is one more thing that I want to do, and that is to just tweak our

131
00:08:36,909 --> 00:08:39,030
input system ever so slightly.

132
00:08:39,039 --> 00:08:44,800
So if we head over into the input system folder and open up our input actions, what we're going to

133
00:08:44,800 --> 00:08:47,080
do is have a look at the firing action.

134
00:08:47,260 --> 00:08:52,330
If you remember, all of this was kind of set up by default when we first added our input system and

135
00:08:52,330 --> 00:08:54,430
we didn't really touch much in here.

136
00:08:54,430 --> 00:09:00,700
But if we have a look at our firing action and then expand this dropdown to see which buttons and inputs

137
00:09:00,700 --> 00:09:06,160
are attached to it, we'll say that we have a left mouse button option so we can fire when we click

138
00:09:06,160 --> 00:09:07,060
the left mouse button.

139
00:09:07,060 --> 00:09:09,670
But there's currently no keyboard binding in here.

140
00:09:09,670 --> 00:09:14,440
And for our game, I think it would be nice if we had something like the space bar set up for firing

141
00:09:14,950 --> 00:09:15,610
to do this.

142
00:09:15,610 --> 00:09:21,910
All we need to do is click on the plus icon for our fire action, click on Add Binding and this will

143
00:09:21,910 --> 00:09:24,400
add a blank binding to the bottom of our list.

144
00:09:24,790 --> 00:09:29,830
Then over in the properties window we need to set up the path, which is the key that we're going to

145
00:09:29,830 --> 00:09:30,940
be listening for.

146
00:09:31,240 --> 00:09:33,370
And we could find it in this list in here.

147
00:09:33,370 --> 00:09:38,650
We could dig through our keyboard and find the spacebar, or we can just hit and listen, press the

148
00:09:38,650 --> 00:09:41,140
spacebar, and then it comes up in our list.

149
00:09:41,140 --> 00:09:47,260
So let's select our spacebar for our path, and we can also select keyboard and mouse to tell our input

150
00:09:47,260 --> 00:09:51,070
actions that this is to be used with the keyboard and mouse control scheme.

151
00:09:51,430 --> 00:09:58,030
With that set up, the final change that we want to make is if we click on the fire action again and

152
00:09:58,030 --> 00:10:02,530
look over in the properties, we can see that it's got an action type of being a button.

153
00:10:02,530 --> 00:10:07,630
And the way this action type is going to work is that when we hit our spacebar to fire, it's going

154
00:10:07,630 --> 00:10:09,640
to register the key down event.

155
00:10:09,640 --> 00:10:13,060
However, it isn't going to register the key up event.

156
00:10:13,060 --> 00:10:17,470
And for our firing action, we're really going to need that key up event so that we can tell the game

157
00:10:17,470 --> 00:10:18,790
when to stop firing.

158
00:10:19,210 --> 00:10:22,930
So instead, let's change the action type to be of type value.

159
00:10:22,960 --> 00:10:25,960
This is the same action type as our MOVE action.

160
00:10:25,960 --> 00:10:31,180
And remember that this triggers both when we press the key down and when we lift the key up.

161
00:10:31,180 --> 00:10:37,150
So we can now use that information to tell if the key is down and should be firing or not without input

162
00:10:37,150 --> 00:10:38,110
action sorted.

163
00:10:38,110 --> 00:10:43,540
Let's go ahead and save our asset and do remember to save unless you have the autosave option checked

164
00:10:43,540 --> 00:10:46,780
and then close our asset window with that set up.

165
00:10:46,780 --> 00:10:50,200
We're now in a very good place to start coding up our shooting behavior.

166
00:10:50,590 --> 00:10:55,600
And whilst it may seem like we didn't really get too much done in this lecture, we actually set up

167
00:10:55,600 --> 00:11:00,880
a lot of groundwork and saved ourselves a lot of potential headaches and bugs going forward.

168
00:11:00,880 --> 00:11:04,450
And in the next lecture, we're going to start generating these projectiles at runtime.

169
00:11:04,750 --> 00:11:06,100
So I'll see you there.

