1
00:00:03,120 --> 00:00:07,110
In this lecture, we're going to add some audio into our game for when Barry crosses the finish line

2
00:00:07,110 --> 00:00:09,000
or when Barry bumps his head.

3
00:00:09,000 --> 00:00:12,270
So let's get started implementing some audio.

4
00:00:12,420 --> 00:00:16,680
Let's start with a little bit of audio terminology so that we're on the same page here.

5
00:00:16,680 --> 00:00:21,810
We'll need an audio listener in our scene and it acts kind of like a microphone in that it receives

6
00:00:21,810 --> 00:00:24,570
the sounds and plays them through your computer speakers.

7
00:00:24,570 --> 00:00:27,360
It's not literally a microphone, but metaphorically.

8
00:00:27,360 --> 00:00:29,910
It's listening for what sounds are being played.

9
00:00:30,000 --> 00:00:33,990
Usually the audio listener is placed on the camera in the scene.

10
00:00:33,990 --> 00:00:36,450
So by default, our camera.

11
00:00:36,450 --> 00:00:37,710
Let's just quickly have a look at this.

12
00:00:37,740 --> 00:00:42,330
We'll click on our main camera, already has an audio listener that's sitting there saying, I'm listening

13
00:00:42,330 --> 00:00:44,430
for sounds, which is very cool.

14
00:00:44,430 --> 00:00:50,970
We have an audio source which allows us to play the audio and also allows us to adjust settings, for

15
00:00:50,970 --> 00:00:57,090
example, to adjust the volume to be louder or less loud for particular things we want to play in our

16
00:00:57,090 --> 00:00:57,810
scene.

17
00:00:57,810 --> 00:00:59,640
And then of course we have audio clips.

18
00:00:59,640 --> 00:01:04,200
This is what contains the audio data to be played within our scene.

19
00:01:04,200 --> 00:01:08,340
For example, MP threes or waves or Ogg or Ogg files.

20
00:01:08,340 --> 00:01:12,140
They're some of the file formats that unity can play.

21
00:01:12,150 --> 00:01:15,060
The first thing to do is for us to grab a couple of sound effects.

22
00:01:15,060 --> 00:01:19,440
So we're going to need two sound effects one for the crash, one for the finish so you can jump onto

23
00:01:19,440 --> 00:01:24,000
the Internet, do some search for royalty free sound effects or just sound effects in general, and

24
00:01:24,000 --> 00:01:26,250
download some that you think would be pretty cool.

25
00:01:26,250 --> 00:01:30,630
Or you could go ahead and make a couple if you know how to do that using audacity, for example.

26
00:01:30,630 --> 00:01:35,550
So I want you to find some sound effects you can use for crash or finish, or if you're feeling a little

27
00:01:35,550 --> 00:01:39,330
bit lazy, you can just download the ones that I've provided you against this lecture.

28
00:01:39,330 --> 00:01:40,950
I've given you two sound effects.

29
00:01:40,950 --> 00:01:43,290
So if you don't want to go search for any, you can just use mine.

30
00:01:43,290 --> 00:01:44,190
So there's your challenge.

31
00:01:44,190 --> 00:01:46,020
Take that on C back here when you're done.

32
00:01:48,130 --> 00:01:53,330
I'm going to download and unzip the sound effects that I've got.

33
00:01:53,350 --> 00:01:56,620
I'm going to click on my assets folder, actually, so I can drag these.

34
00:01:56,680 --> 00:01:58,630
Click, click, drag them into unity.

35
00:01:58,630 --> 00:02:04,570
And from here I'll right click, create a folder and that folder we will call audio.

36
00:02:04,600 --> 00:02:05,910
I think it's a good name for that.

37
00:02:05,920 --> 00:02:08,910
We'll grab both of these and pop them into the folder.

38
00:02:08,919 --> 00:02:09,780
Just a quick one.

39
00:02:09,789 --> 00:02:11,740
Maybe if English is not your first language.

40
00:02:11,770 --> 00:02:13,830
SFX The letters there.

41
00:02:13,840 --> 00:02:17,650
SFX is just another way of writing sound effects.

42
00:02:17,650 --> 00:02:21,400
The letters F and X kind of sound like when you say them quickly.

43
00:02:21,400 --> 00:02:23,110
So that's how we write sound effects.

44
00:02:23,140 --> 00:02:24,760
Now I'm going to start with the finish line.

45
00:02:24,760 --> 00:02:26,980
If I double click on that, takes me all the way down here.

46
00:02:26,980 --> 00:02:27,700
Actually, you know what?

47
00:02:27,700 --> 00:02:30,760
Let me just very quickly bring this all the way back next to barrier.

48
00:02:30,760 --> 00:02:35,500
So if we want to trigger this sound effect, we're not having to scoot halfway across the world to get

49
00:02:35,500 --> 00:02:35,810
there.

50
00:02:35,830 --> 00:02:41,590
Now, the finish line, what we need to do, first of all, is add a audio source so that we have a

51
00:02:41,590 --> 00:02:42,790
way of playing a sound effect.

52
00:02:42,790 --> 00:02:45,730
So click on Add component search for audio source.

53
00:02:45,730 --> 00:02:48,250
You can see that there put that drops one on in here.

54
00:02:48,250 --> 00:02:50,080
You can see there's some fields and some settings.

55
00:02:50,080 --> 00:02:55,150
We have the audio clip and this is the place that the audio source is going to say, I'm going to play

56
00:02:55,150 --> 00:02:56,770
this particular thing.

57
00:02:56,770 --> 00:03:01,750
So for me, when it comes to the finish line, I'm going to drag my finished sound effects onto audio

58
00:03:01,750 --> 00:03:02,050
clip.

59
00:03:02,050 --> 00:03:05,020
Or I could have clicked the selector and found it within my project.

60
00:03:05,020 --> 00:03:10,420
Now, this approach of dragging or having something set within the audio clip field is for if we're

61
00:03:10,420 --> 00:03:16,360
only going to be triggering one specific sound effect for this game object or for this audio source.

62
00:03:16,360 --> 00:03:20,260
So when we cross the finish line, it's always going to be this particular sound effect.

63
00:03:20,260 --> 00:03:21,370
So I'm comfortable with that.

64
00:03:21,370 --> 00:03:25,570
While I'm here, I'm going to drop the volume down to say 0.5 because I'm pretty sure it's going to

65
00:03:25,570 --> 00:03:26,590
be a bit too loud.

66
00:03:26,590 --> 00:03:29,380
I'm not going to mess around with any of the other things, but it's kind of cool to play around with

67
00:03:29,380 --> 00:03:29,620
pitch.

68
00:03:29,650 --> 00:03:34,390
If you wanted to make your sound effect a little bit higher, a little bit lower, you can change pitch.

69
00:03:34,390 --> 00:03:38,860
And while I'm here, I'm going to click play on a wake because we don't want the sound effect to go.

70
00:03:38,860 --> 00:03:45,070
When I click on the play button, we want it when the snowboarder flies through this collision area.

71
00:03:45,070 --> 00:03:47,590
Now, what script is going to deal with that?

72
00:03:47,590 --> 00:03:49,570
It's going to be the finish line script.

73
00:03:49,570 --> 00:03:55,000
So let's open up finish line now because we're only playing this one sound effect.

74
00:03:55,000 --> 00:03:55,570
Where are we?

75
00:03:55,570 --> 00:03:57,430
Back to finish line audio source.

76
00:03:57,430 --> 00:03:58,630
It's just this one sound effect.

77
00:03:58,630 --> 00:04:02,860
All we need to do is say, Let's get this component and play.

78
00:04:02,860 --> 00:04:03,670
That's all we need to do.

79
00:04:03,670 --> 00:04:09,010
So we'll jump in here and do this after we have finished our thing and done our thing.

80
00:04:09,010 --> 00:04:09,220
Yep.

81
00:04:09,220 --> 00:04:09,730
Down in there.

82
00:04:09,730 --> 00:04:15,580
So right after we're invoking reload scene, let's simply get component.

83
00:04:15,590 --> 00:04:22,390
We can get component because the finish line script is on the same game object as our audio source because

84
00:04:22,390 --> 00:04:27,490
the component we're going to get is audio source, parentheses and then dot.

85
00:04:27,490 --> 00:04:30,070
Now that we've got the audio source, what do we want to do?

86
00:04:30,070 --> 00:04:32,470
Well, audio source comes along with some methods.

87
00:04:32,470 --> 00:04:36,700
And one of those methods is to play very similar to the particle effect.

88
00:04:36,700 --> 00:04:42,310
So play and then parentheses semicolon just a group this I know it's a minor, minor thing, but I'm

89
00:04:42,310 --> 00:04:46,900
just going to cut that and pop it right next to the particle effect so I can see what I'm scrolling

90
00:04:46,900 --> 00:04:47,560
through this.

91
00:04:47,680 --> 00:04:52,240
Okay, play particle, play audio and then reload the scene.

92
00:04:52,240 --> 00:04:52,990
Click Save.

93
00:04:52,990 --> 00:04:54,310
We'll see if this works.

94
00:04:54,790 --> 00:04:56,110
Okay, Cruz, Cruz, Cruz.

95
00:04:56,110 --> 00:04:57,100
When we hit the thingy.

96
00:04:57,400 --> 00:04:58,030
There we go.

97
00:04:58,030 --> 00:04:58,930
You should have heard that.

98
00:04:58,930 --> 00:05:00,880
Now, that's all there is to triggering audio.

99
00:05:00,910 --> 00:05:01,450
Pretty.

100
00:05:01,450 --> 00:05:02,770
Pretty simple, huh?

101
00:05:02,800 --> 00:05:05,080
But we're going to add a little bit of complexity.

102
00:05:05,080 --> 00:05:06,550
Not a lot, but a little bit of complexity.

103
00:05:06,550 --> 00:05:11,320
We're going to have a look at battery and we're going to say with battery, we might want to play many

104
00:05:11,320 --> 00:05:13,090
different sound effects with Barry.

105
00:05:13,090 --> 00:05:14,950
So let's go ahead and add a component.

106
00:05:14,950 --> 00:05:19,810
Add the audio source component, which is a way for us to play our different clips.

107
00:05:19,810 --> 00:05:20,770
We might say, You know what?

108
00:05:20,770 --> 00:05:25,180
There's going to be a sound effect if you crash, a sound effect if you land, a sound effect if you

109
00:05:25,180 --> 00:05:26,320
do a spin.

110
00:05:26,320 --> 00:05:32,890
So we don't want to go and drag in one specific clip into the audio clip area in here because it's going

111
00:05:32,890 --> 00:05:33,520
to change.

112
00:05:33,520 --> 00:05:36,460
But we can play different things through code.

113
00:05:36,460 --> 00:05:37,660
How do we do that?

114
00:05:37,660 --> 00:05:41,620
Well, let's open up our crash detector, open that up.

115
00:05:41,620 --> 00:05:43,150
And within here, we need to save.

116
00:05:43,150 --> 00:05:47,110
First of all, what is the sound effect that we want to play if we crash?

117
00:05:47,110 --> 00:05:51,100
So very similar to the way we did with the particle system, we'll serialize a field.

118
00:05:51,100 --> 00:05:52,390
This will be audio clip.

119
00:05:52,390 --> 00:05:59,890
So I've type audio clip and then we'll call this crash sound effects or SFX for short now as we did

120
00:05:59,890 --> 00:06:02,590
before, we'll look for where it is that we're crashing here.

121
00:06:02,590 --> 00:06:03,490
It is that we crash.

122
00:06:03,490 --> 00:06:05,050
We're doing our particle effect.

123
00:06:05,050 --> 00:06:08,350
And then next we will once again get the component.

124
00:06:08,350 --> 00:06:10,900
So get component audio source.

125
00:06:10,900 --> 00:06:14,680
However, instead of just using play in here, we're going to do something slightly different.

126
00:06:14,680 --> 00:06:17,890
We're going to use a method called play one shot.

127
00:06:17,890 --> 00:06:23,710
And that allows us we put in our parentheses, you can see in here, it says plays an audio clip and

128
00:06:23,710 --> 00:06:26,140
scales the audio source volume by volume scale.

129
00:06:26,140 --> 00:06:31,510
So the main thing in here is that we can actually say this is the specific audio clip that we want to

130
00:06:31,510 --> 00:06:34,030
play and we've set that up already that we've identified.

131
00:06:34,030 --> 00:06:35,650
The audio clip is going to be our crash.

132
00:06:35,650 --> 00:06:37,900
SFX So we'll pop in crash.

133
00:06:37,900 --> 00:06:42,720
SFX In here and then semicolon getting the audio source we're telling it to play.

134
00:06:42,730 --> 00:06:45,250
This time we're saying play one shot as our approach.

135
00:06:45,250 --> 00:06:46,420
And what are we playing?

136
00:06:46,420 --> 00:06:46,780
We're playing.

137
00:06:47,310 --> 00:06:47,970
Sound effects.

138
00:06:47,970 --> 00:06:50,510
Save that back to unity.

139
00:06:50,520 --> 00:06:52,100
Now we need to go and tell it.

140
00:06:52,110 --> 00:06:54,630
Which thing is the crash sound effect?

141
00:06:54,630 --> 00:06:56,880
Click on Barry and will find in here.

142
00:06:56,880 --> 00:06:58,570
Where is our crash detector script?

143
00:06:58,590 --> 00:06:59,260
There it is.

144
00:06:59,280 --> 00:07:01,110
Crash SFX, which one do we want?

145
00:07:01,140 --> 00:07:04,320
Well, this time I'll use the selector just to show you a different flavor.

146
00:07:04,320 --> 00:07:06,630
And I'm going to use the crash sound effect.

147
00:07:06,630 --> 00:07:12,060
And we could, of course, have in here other fields for maybe there's a land sound effect or a spin

148
00:07:12,060 --> 00:07:12,720
sound effect.

149
00:07:12,720 --> 00:07:17,520
And then within our code, wherever it might be, it doesn't have to be just within this one particular

150
00:07:17,520 --> 00:07:18,030
script.

151
00:07:18,030 --> 00:07:19,500
It doesn't have to be in crash detector.

152
00:07:19,500 --> 00:07:21,420
It could be within player controller.

153
00:07:21,420 --> 00:07:26,250
We might want to play a different sound effect, but we'll still be accessing the same audio source

154
00:07:26,250 --> 00:07:28,530
and telling it to do the play one shot.

155
00:07:28,530 --> 00:07:29,760
But let's see if this works.

156
00:07:29,760 --> 00:07:32,840
Click on play here.

157
00:07:32,930 --> 00:07:33,860
Okay, cool.

158
00:07:33,870 --> 00:07:35,310
Does a nice little sound effect.

159
00:07:35,310 --> 00:07:35,820
Excellent.

160
00:07:35,820 --> 00:07:36,480
So there we go.

161
00:07:36,480 --> 00:07:40,160
We've got a couple of pieces of audio in our level, finish line and crash.

162
00:07:40,170 --> 00:07:44,400
It's pretty straightforward once you get the knack of it, but it really adds a lot of extra flavor

163
00:07:44,400 --> 00:07:45,310
to your game.

164
00:07:45,330 --> 00:07:46,860
Okay, I'll see you in the next lecture.

