1
00:00:04,150 --> 00:00:07,450
In this lecture, we're going to start adding some audio to our game.

2
00:00:07,720 --> 00:00:12,640
Before we get into music, though, we're going to start with our explosions and our firing effect.

3
00:00:13,540 --> 00:00:17,890
Now, the first step to adding any audio into our game is to find something to use.

4
00:00:17,890 --> 00:00:22,510
So this is going to be your first challenge to find some audio that you like the sound of.

5
00:00:22,540 --> 00:00:29,170
Here I am on Kenny Nel again, where we got our sprites from and if we add up to assets, Kenny also

6
00:00:29,170 --> 00:00:30,550
has some audio.

7
00:00:30,550 --> 00:00:35,470
So if we filter by audio, I'm going to go with the sci fi sounds pack.

8
00:00:36,950 --> 00:00:42,470
Once you've downloaded your chosen audio, we need to import it into our project and I've placed mine

9
00:00:42,470 --> 00:00:45,560
under my assets pack and sci fi sound.

10
00:00:45,560 --> 00:00:48,200
So I've got all of my Kenny sounds in here.

11
00:00:48,530 --> 00:00:51,560
And now let's think about how we're going to play this audio.

12
00:00:51,590 --> 00:00:56,510
There are many options for playing audio in unity depending on whether you're using sound effects or

13
00:00:56,510 --> 00:00:57,880
music and things like that.

14
00:00:57,890 --> 00:01:03,440
So let's start by looking at playing one shot audio clips, and these are going to be audio clips that

15
00:01:03,440 --> 00:01:09,890
are instantiated into the world at runtime, exist for the lifetime of the audio clip and then get destroyed

16
00:01:10,190 --> 00:01:11,360
up in our hierarchy.

17
00:01:11,360 --> 00:01:17,030
Let's right click and create a new empty game object and let's call this the audio player.

18
00:01:17,870 --> 00:01:21,620
It doesn't really matter, but let's reset the transforms, keep it tidy.

19
00:01:21,620 --> 00:01:24,320
And next we're going to add a new script to this.

20
00:01:24,320 --> 00:01:29,120
So in our scripts folder, let's right click create a new C-sharp script.

21
00:01:29,120 --> 00:01:35,300
Let's call this the audio player and attach that to our audio player game object.

22
00:01:35,870 --> 00:01:39,830
With that set up, let's open up that script and see what we need to write.

23
00:01:40,750 --> 00:01:45,320
Let's start by getting rid of start and update because we're not going to need them for this.

24
00:01:45,340 --> 00:01:50,170
And I said we were going to play audio clips for when we're firing projectiles and when the enemies

25
00:01:50,170 --> 00:01:54,280
are firing projectiles at us, and also whenever something takes damage.

26
00:01:54,280 --> 00:01:58,690
So when that explosion happens, we're going to have a nice explosion effect to go with it to start

27
00:01:58,690 --> 00:01:58,930
with.

28
00:01:58,930 --> 00:02:03,790
Let's just go through the shooting effects, though, and then I'll leave the damage effect to you as

29
00:02:03,790 --> 00:02:09,340
a challenge now, because our audio player is going to be managing multiple different pieces of audio,

30
00:02:09,340 --> 00:02:12,730
it would make sense to separate up all variables using some headers.

31
00:02:12,730 --> 00:02:16,930
So first of all, let's add a new header for our shooting.

32
00:02:17,170 --> 00:02:19,900
And under this header we're going to need two variables.

33
00:02:19,900 --> 00:02:23,350
We're going to need a serialized filled of type audio clip.

34
00:02:23,350 --> 00:02:29,890
So the thing we want to play and let's call this the shooting clip and the second variable we want is

35
00:02:29,890 --> 00:02:34,630
again going to be a serialized filled, but this is going to control the volume of our clip.

36
00:02:34,630 --> 00:02:41,320
So this is going to be of type float and we'll call it the shooting volume to give it a default value.

37
00:02:41,320 --> 00:02:44,890
Let's set this to one to start with, which is full volume.

38
00:02:44,890 --> 00:02:51,520
And because our volume is going to range between zero and one, let's also add a range attribute to

39
00:02:51,520 --> 00:02:53,800
this so we can say range.

40
00:02:53,950 --> 00:03:00,430
And then in parentheses we can give it a lower bound of zero F and an upper bound of one F, and this

41
00:03:00,430 --> 00:03:04,180
attribute will then constrain our value between those bounds.

42
00:03:04,180 --> 00:03:10,120
This also has another nice benefit of adding a slider to our inspector to make it really easy to use.

43
00:03:10,210 --> 00:03:15,640
So with our variables set up now, let's create the method that we're going to call from another script

44
00:03:15,640 --> 00:03:19,600
to play this audio clip because it's going to be called from elsewhere.

45
00:03:19,600 --> 00:03:24,010
Let's go ahead and make it public, but it's not going to need to return anything.

46
00:03:24,130 --> 00:03:27,220
And we'll call this method play shooting clip.

47
00:03:29,160 --> 00:03:29,670
In here.

48
00:03:29,670 --> 00:03:34,710
The first thing we want to do is check whether our shooting clip has something assigned to it so we

49
00:03:34,710 --> 00:03:42,750
can say if our shooting clip does not equal null, then we want to go and play this clip.

50
00:03:43,440 --> 00:03:50,810
And the way we play this audio clip is by saying audio source don't play clip at point.

51
00:03:50,820 --> 00:03:55,920
So what this method does is takes our audio clip and instantiate it into the world.

52
00:03:56,190 --> 00:03:59,370
This means we're going to have to give this method some parameters.

53
00:03:59,370 --> 00:04:01,970
And the first one is the clip we want to play.

54
00:04:01,980 --> 00:04:04,470
So this is going to be our shooting clip.

55
00:04:04,500 --> 00:04:10,350
From our tooltip, we can then see the next parameter is going to be a position in world and to make

56
00:04:10,350 --> 00:04:15,630
the audio as audible as possible, we're going to place it directly in position of the camera.

57
00:04:15,780 --> 00:04:23,130
We can get the camera's position by saying camera dot main dot transform dot position.

58
00:04:23,250 --> 00:04:26,070
And finally we want to specify the volume.

59
00:04:26,070 --> 00:04:28,530
So that is going to be our shooting volume.

60
00:04:28,710 --> 00:04:32,640
Now this line is very long, so let's break it up and make it easier to read.

61
00:04:33,120 --> 00:04:35,420
And that is all there is to this method.

62
00:04:35,430 --> 00:04:40,320
Now we need to work out where we want to call this method from, and we're currently dealing with all

63
00:04:40,320 --> 00:04:43,080
of our shooting behavior in our shooter script.

64
00:04:43,080 --> 00:04:45,030
So let's open up our shooter script.

65
00:04:45,180 --> 00:04:49,860
And in here the first thing we want to do is grab a reference to our audio player.

66
00:04:49,890 --> 00:04:54,420
So let's say audio player and we'll call it the audio player.

67
00:04:55,140 --> 00:04:57,120
Let's add an awake method.

68
00:04:58,480 --> 00:05:05,380
And in this awake method we're going to say audio player equals find object of type audio player.

69
00:05:06,130 --> 00:05:11,890
Then if we carry on scrolling down our script, we're dealing with all of our firing behavior in this

70
00:05:12,190 --> 00:05:12,910
routine.

71
00:05:12,910 --> 00:05:15,250
And this code routine is getting pretty messy.

72
00:05:15,250 --> 00:05:20,410
So we may want to break this out into multiple smaller methods to make it easier to manage.

73
00:05:20,410 --> 00:05:25,180
But for now I'm going to leave it as is, but feel free to update yours if you prefer.

74
00:05:25,330 --> 00:05:28,750
And instead, I'm just going to add another new line in here.

75
00:05:29,110 --> 00:05:34,420
This line is going to be our audio player dot play shooting clip.

76
00:05:34,690 --> 00:05:39,610
So with that done, let's save our script and jump into unity to test this out.

77
00:05:40,030 --> 00:05:45,280
If we check out our audio player, we can see how this header is going to come in useful for separating

78
00:05:45,280 --> 00:05:47,020
the different clips in our game.

79
00:05:47,050 --> 00:05:50,590
And we've also got this nice range slider between zero and one.

80
00:05:50,710 --> 00:05:54,010
Before we can play though, we do need to set up our shooting clip.

81
00:05:54,010 --> 00:06:00,730
So let's find our audio clips in our folder and I'm going to choose the laser small one for my audio

82
00:06:00,730 --> 00:06:01,270
clip.

83
00:06:01,540 --> 00:06:06,880
Now in our game view, if we just extend this over so we can see some of the options we need to make

84
00:06:06,880 --> 00:06:09,760
sure that mute audio is unselected.

85
00:06:09,760 --> 00:06:15,100
This can trip you up quite a lot of times, so if you've placed audio in your game and you can't hear

86
00:06:15,100 --> 00:06:19,300
it, then just make sure that this option is unchecked in the game view.

87
00:06:19,570 --> 00:06:27,130
With that deselect selected, let's go ahead and hit play and we've got some nice shooting effects.

88
00:06:27,400 --> 00:06:32,950
Now this isn't too bad as it is, but with all rapid firing projectiles, this could get a little bit

89
00:06:32,950 --> 00:06:33,700
overwhelming.

90
00:06:33,820 --> 00:06:38,620
So let's turn it down a tad because otherwise it can get very annoying.

91
00:06:38,800 --> 00:06:41,710
And I'm going to turn it right way down to around 2.4.

92
00:06:42,040 --> 00:06:46,420
This seems like a much more reasonable volume and hopefully you can still hear it on your end.

93
00:06:46,810 --> 00:06:50,140
So let's come out of play mode and we made that change in play mode.

94
00:06:50,140 --> 00:06:52,720
So let's just reset that to 0.4.

95
00:06:54,020 --> 00:06:56,450
And now I think it's time for your challenge.

96
00:06:56,900 --> 00:07:02,870
I want you to go ahead and extend our audio player to play an audio clip in damages taken.

97
00:07:02,870 --> 00:07:09,140
So when those explosions happen in our game, as a hint, it's a very similar process to playing our

98
00:07:09,140 --> 00:07:10,430
firing audio clip.

99
00:07:10,430 --> 00:07:15,920
So you can use all of what we've just done as a reference and have a think about which script is handling

100
00:07:15,920 --> 00:07:18,230
the damage processing for our game.

101
00:07:18,230 --> 00:07:21,140
So pause the video now and give that a go.

102
00:07:26,510 --> 00:07:26,990
Okay.

103
00:07:26,990 --> 00:07:27,950
Welcome back.

104
00:07:27,980 --> 00:07:31,580
So let's add our damage audio to our audio player.

105
00:07:31,580 --> 00:07:32,570
And it's so similar.

106
00:07:32,570 --> 00:07:38,120
In fact, I'm going to start off by copying and pasting my variables that I've already written and I'm

107
00:07:38,120 --> 00:07:41,080
going to change my header to be damaged.

108
00:07:41,120 --> 00:07:47,030
I'm going to change my audio clip to be the damaged clip, and I'm going to change the volume to be

109
00:07:47,030 --> 00:07:48,260
the damage volume.

110
00:07:48,620 --> 00:07:52,010
Then we're going to need a method just like our play shooting clip.

111
00:07:52,010 --> 00:07:55,790
So again, let's just copy and paste this and reuse what we've got.

112
00:07:56,420 --> 00:07:59,030
We'll call this play damage clip.

113
00:07:59,650 --> 00:08:06,330
And we now want to say that if our damage clip does not equal null, then our audio source play clip

114
00:08:06,370 --> 00:08:14,140
point should be playing our damaged clip at the camera's position and we want the damage volume.

115
00:08:14,710 --> 00:08:17,500
So that's all there is to this side of the script.

116
00:08:17,500 --> 00:08:22,840
But I'm noticing that these two methods are virtually identical, so it would be quite nice if we could

117
00:08:22,840 --> 00:08:26,860
simplify this in case we need to add any more audio to our game later on.

118
00:08:27,100 --> 00:08:34,270
So let's actually write another private method of type void and we're just going to call this play clip.

119
00:08:35,140 --> 00:08:39,970
And if we look at the similarities that we can draw from both of our currently written methods, the

120
00:08:39,970 --> 00:08:43,630
things that really change are the clip and the volume.

121
00:08:43,630 --> 00:08:46,330
So our method can accept these as parameters.

122
00:08:46,330 --> 00:08:52,930
We need an audio clip and we'll just call this the clip for now and we need a float for the volume.

123
00:08:53,470 --> 00:08:58,060
Then we can say that if our clip does not equal null.

124
00:08:59,230 --> 00:09:03,040
Then the first thing I want to do is try and shorten the length of this line of code.

125
00:09:03,040 --> 00:09:07,180
So I'm actually going to take the camera position as a variable.

126
00:09:07,180 --> 00:09:15,700
So this is a vector three called camera pause and this is equal to our camera dot main dot transform

127
00:09:15,820 --> 00:09:17,080
dot position.

128
00:09:17,080 --> 00:09:22,330
And then we can play our audio by saying audio source play clip at point.

129
00:09:22,630 --> 00:09:27,910
And we want to play our clip at the camera position at the specified volume.

130
00:09:28,600 --> 00:09:30,910
With our more generic methods set up.

131
00:09:30,910 --> 00:09:35,260
Now, we can use this in both of these other helper methods.

132
00:09:35,440 --> 00:09:39,610
So let's delete everything that's in there and just say Play clip.

133
00:09:40,490 --> 00:09:44,420
And we want to play our shooting clip at our shooting volume.

134
00:09:44,750 --> 00:09:52,010
And for the second one, we want to play our clip and we want to play our damage clip at our damage

135
00:09:52,010 --> 00:09:52,610
volume.

136
00:09:52,700 --> 00:09:57,470
So a little bit of annoying refactoring out of the way there, but we've now got a much more robust

137
00:09:57,470 --> 00:09:58,220
solution.

138
00:09:58,220 --> 00:10:04,550
With that all set, we now have to actually call this play damage clip from somewhere and damage is

139
00:10:04,550 --> 00:10:07,430
currently being handled entirely within our health script.

140
00:10:07,790 --> 00:10:11,150
So let's open up our health script at the top of our script.

141
00:10:11,150 --> 00:10:15,920
Let's grab a reference to our audio player and call this audio player as well.

142
00:10:16,460 --> 00:10:22,370
In Awake, we can say that our audio player equals find object of type audio player.

143
00:10:23,840 --> 00:10:29,240
And finally in our on trigger enter when we're taking damage playing our hit effect, let's say that

144
00:10:29,240 --> 00:10:33,020
our audio player should play our damage clip.

145
00:10:33,590 --> 00:10:37,370
So let's save this up now and jump into unity to test that all out.

146
00:10:38,280 --> 00:10:39,390
In our audio player.

147
00:10:39,390 --> 00:10:44,460
Let's set up a damaged clip and I'm going to go with explosion crunch for.

148
00:10:45,380 --> 00:10:47,840
And if we once again hit play to test this out.

149
00:10:48,970 --> 00:10:54,430
We have our shooting sounds and we have a nice explosion sound as well.

150
00:10:55,390 --> 00:11:01,000
Just like our shooting audio though this may be a tad too loud, but until we get our background audio

151
00:11:01,000 --> 00:11:05,550
in, I think it's okay to leave it as it is for now and then balance it later on.

152
00:11:05,560 --> 00:11:10,930
So congratulations on adding the first pieces of audio to your game, and in the next lecture we're

153
00:11:10,930 --> 00:11:13,060
going to start adding some background music.

154
00:11:13,060 --> 00:11:14,410
So I'll see you there.

