1
00:00:00,000 --> 00:00:00,000
Hey.

2
00:00:00,000 --> 00:00:01,000
Hello there, guys.

3
00:00:01,000 --> 00:00:03,000
It's time to work on the audio in this game.

4
00:00:03,000 --> 00:00:06,000
First, I want to organize our project just a bit.

5
00:00:06,000 --> 00:00:09,000
I want to take these two scripts, drag it to the script folder.

6
00:00:10,000 --> 00:00:14,000
And then just put these two in the UI folder.

7
00:00:14,000 --> 00:00:18,000
Now let's create an empty object that I'm going to call audio.

8
00:00:19,000 --> 00:00:20,000
Oh, dear.

9
00:00:20,000 --> 00:00:21,000
Manager.

10
00:00:23,000 --> 00:00:24,000
And we're going to create a new script.

11
00:00:24,000 --> 00:00:26,000
C Sharp.

12
00:00:26,000 --> 00:00:27,000
Oh, dear.

13
00:00:27,000 --> 00:00:28,000
Manager.

14
00:00:30,000 --> 00:00:32,000
And drag it here.

15
00:00:33,000 --> 00:00:35,000
All right, now, let's go inside of the script.

16
00:00:38,000 --> 00:00:46,000
We can close all but this and what and first, what we need to do is to make it accessible from any

17
00:00:46,000 --> 00:00:48,000
place, just like we have with the game manager.

18
00:00:48,000 --> 00:00:50,000
So I'm going to make it public.

19
00:00:50,000 --> 00:00:53,000
Static Outdoor Manager.

20
00:00:56,000 --> 00:00:57,000
Instance.

21
00:00:57,000 --> 00:00:57,000
Yeah.

22
00:00:57,000 --> 00:00:59,000
Then we need a wake function.

23
00:01:00,000 --> 00:01:06,000
A wake in the wake, I'm going to say instance equals to this, and I think I can do.

24
00:01:07,000 --> 00:01:08,000
Is what I can do.

25
00:01:08,000 --> 00:01:11,000
I don't plan to write anything else in their wake.

26
00:01:11,000 --> 00:01:13,000
When you start function.

27
00:01:14,000 --> 00:01:14,000
All right.

28
00:01:14,000 --> 00:01:21,000
So how this is going to work, you can create a type of variable that is called audio source.

29
00:01:21,000 --> 00:01:25,000
Let's try this serialized field, private audio source.

30
00:01:25,000 --> 00:01:27,000
Now, I'm going to call it a fix.

31
00:01:28,000 --> 00:01:30,000
Very simple as a fix for sound effect.

32
00:01:30,000 --> 00:01:32,000
I want to make it an array.

33
00:01:32,000 --> 00:01:35,000
Let's make it an array and let's go to unit now.

34
00:01:36,000 --> 00:01:42,000
There is a folder of audio in the project that I added just in the beginning, and there is an it's

35
00:01:42,000 --> 00:01:44,000
a fixed folder and this is our sound effects.

36
00:01:44,000 --> 00:01:47,000
So what we want to do is to take any of these sounds.

37
00:01:47,000 --> 00:01:50,000
For example, let's take coin pickup, the one that we use the most.

38
00:01:50,000 --> 00:01:57,000
First of all, when you drag any of the audio source uncheck play on a wake, because if it is on,

39
00:01:57,000 --> 00:02:00,000
then whenever you start the game, the sound going to be played.

40
00:02:01,000 --> 00:02:04,000
Let me unmute because now audio is muted.

41
00:02:07,000 --> 00:02:10,000
So with the play on Awake, you can see that sound was played.

42
00:02:10,000 --> 00:02:14,000
If you put more sounds into the game and you will keep playing awake.

43
00:02:14,000 --> 00:02:15,000
And let me show you this.

44
00:02:15,000 --> 00:02:20,000
I'm going to take jump jump and UI click and wasted drag it here.

45
00:02:20,000 --> 00:02:25,000
They all have play on a wake function and if you go to play mode, it's going to sound horrible because

46
00:02:25,000 --> 00:02:27,000
all of the sound is going to be played at once.

47
00:02:27,000 --> 00:02:31,000
So what we need to do to fix that is to click play on a wake over here.

48
00:02:31,000 --> 00:02:33,000
Then I want to give them parent.

49
00:02:34,000 --> 00:02:37,000
And I'll name it as a suffix.

50
00:02:37,000 --> 00:02:42,000
And then I'll drag this parent to the manager to make it a child of audio manager.

51
00:02:43,000 --> 00:02:44,000
Good.

52
00:02:44,000 --> 00:02:45,000
Now we have five sounds in the game.

53
00:02:45,000 --> 00:02:50,000
There are some other sounds, but we're not going to touch them yet because now let's focus just on

54
00:02:50,000 --> 00:02:51,000
creating an audio manager.

55
00:02:51,000 --> 00:02:56,000
We have going to pick up a couple of jumps dest sound effect and UI click.

56
00:02:56,000 --> 00:03:02,000
Now we're going to go to audio manager, click this, lock it, open this array and choose all of the

57
00:03:02,000 --> 00:03:04,000
sounds and drag them here.

58
00:03:05,000 --> 00:03:06,000
That's good.

59
00:03:06,000 --> 00:03:10,000
Now that we have bunch of elements in the array, we can use them, we can call them and tell them,

60
00:03:10,000 --> 00:03:13,000
Just play this what we're going to do, Let's go back.

61
00:03:14,000 --> 00:03:18,000
I'll delete these two functions for now, and I'll create one public void.

62
00:03:19,000 --> 00:03:21,000
Play as a fix.

63
00:03:21,000 --> 00:03:23,000
I need an index for the sound.

64
00:03:23,000 --> 00:03:29,000
So and I and then just inside, I'm going to do this.

65
00:03:29,000 --> 00:03:35,000
If I is less than as a fixed length and we're using this one just to check if there is such a number

66
00:03:35,000 --> 00:03:38,000
in the array, then we're going to play the sound.

67
00:03:38,000 --> 00:03:39,000
It's a fix.

68
00:03:39,000 --> 00:03:42,000
I don't play very simple stuff.

69
00:03:42,000 --> 00:03:44,000
Let's go and try this.

70
00:03:44,000 --> 00:03:48,000
Let's go to the coin and trigger enter of the coin.

71
00:03:48,000 --> 00:03:51,000
We're going to call audio manager dot instance.

72
00:03:51,000 --> 00:03:55,000
Dot play is a fix and we need to check what was the number because I don't remember.

73
00:03:56,000 --> 00:03:57,000
Coin pickup is on the zero.

74
00:03:57,000 --> 00:03:59,000
So let's do this zero.

75
00:04:00,000 --> 00:04:01,000
Let's go and see.

76
00:04:01,000 --> 00:04:02,000
How does this work?

77
00:04:09,000 --> 00:04:10,000
Okay.

78
00:04:10,000 --> 00:04:11,000
That is very good.

79
00:04:11,000 --> 00:04:16,000
Now, let's go back and create another function that we might use just in some cases.

80
00:04:16,000 --> 00:04:23,000
But still, it's better to have this function public void stop as a fix int i.

81
00:04:24,000 --> 00:04:25,000
And inside we're going to do.

82
00:04:27,000 --> 00:04:30,000
Suffix when I stop.

83
00:04:32,000 --> 00:04:35,000
Now regarding background music, basically the same.

84
00:04:35,000 --> 00:04:41,000
The only difference is when we play background music, we need to make sure we switch off all other

85
00:04:41,000 --> 00:04:45,000
background music there is to avoid the chaos from different audio sources.

86
00:04:45,000 --> 00:04:47,000
So let's do sterilize field here.

87
00:04:47,000 --> 00:04:51,000
Private audio source for BGM.

88
00:04:52,000 --> 00:04:56,000
Then we're going to create a function public void play BGM.

89
00:04:56,000 --> 00:05:00,000
We're going to pass into a because we need a number.

90
00:05:00,000 --> 00:05:01,000
Then we're going to do for loop.

91
00:05:01,000 --> 00:05:04,000
They're going to cycle through all BGM sounds.

92
00:05:08,000 --> 00:05:10,000
The GM dot links.

93
00:05:10,000 --> 00:05:14,000
GM I dot stop and then we're going to play some we want.

94
00:05:14,000 --> 00:05:21,000
Basically we did same functionality when we did switch UI if you remember I dot play and then just in

95
00:05:21,000 --> 00:05:27,000
case you need to stop GM or we have some issue here because for a loop using AI and we as an AI, there

96
00:05:27,000 --> 00:05:29,000
is a conflict because of the same name.

97
00:05:29,000 --> 00:05:35,000
So we better change this to something like index, for example, index.

98
00:05:36,000 --> 00:05:38,000
Or we could use just a tee.

99
00:05:38,000 --> 00:05:41,000
Well, let's be consistent and type indexes here as well.

100
00:05:45,000 --> 00:05:46,000
Okay.

101
00:05:46,000 --> 00:05:48,000
Now, let's do public void.

102
00:05:48,000 --> 00:05:49,000
Stop.

103
00:05:50,000 --> 00:05:52,000
We don't need to pass index here.

104
00:05:52,000 --> 00:05:55,000
We're just going to switch off all of the BGM music.

105
00:06:01,000 --> 00:06:03,000
Okay, good enough.

106
00:06:03,000 --> 00:06:05,000
Now let's go and prepare background music.

107
00:06:12,000 --> 00:06:16,000
Going to go to audio folder, find BGM.

108
00:06:18,000 --> 00:06:23,000
Drag them to the hierarchy, then let's create a parent for them that's going to be named as PG.

109
00:06:23,000 --> 00:06:25,000
Let's unlock the inspector.

110
00:06:26,000 --> 00:06:27,000
Big jam.

111
00:06:27,000 --> 00:06:29,000
Then we're going to make a child of audio manager.

112
00:06:30,000 --> 00:06:33,000
And then we need to draw songs to the mirror.

113
00:06:34,000 --> 00:06:35,000
Just like this.

114
00:06:36,000 --> 00:06:39,000
And check make sure we uncheck play on awake here.

115
00:06:39,000 --> 00:06:40,000
And don't worry about the volume.

116
00:06:40,000 --> 00:06:42,000
We're going to fix volume later.

117
00:06:42,000 --> 00:06:44,000
For now, let's just do this.

118
00:06:46,000 --> 00:06:50,000
Let's make sure it's working so we can go to audio manager.

119
00:06:50,000 --> 00:06:52,000
Again and sorry.

120
00:06:53,000 --> 00:06:54,000
And in this third function.

121
00:06:55,000 --> 00:06:57,000
We can do a start.

122
00:06:57,000 --> 00:07:01,000
Maybe GM with the random index.

123
00:07:02,000 --> 00:07:04,000
So random range.

124
00:07:05,000 --> 00:07:11,000
And if yours, just like mine, shows this weird error, what you can do is go above and just delete

125
00:07:11,000 --> 00:07:14,000
this using system and delete using system reflection.

126
00:07:14,000 --> 00:07:15,000
We don't need them.

127
00:07:15,000 --> 00:07:17,000
We only need collection and collection generic.

128
00:07:17,000 --> 00:07:18,000
Now it should be fine.

129
00:07:18,000 --> 00:07:24,000
So not range from zero to BGM dot lengths.

130
00:07:24,000 --> 00:07:26,000
So it's going to play a random background music.

131
00:07:26,000 --> 00:07:27,000
Cool.

132
00:07:29,000 --> 00:07:32,000
Now let's go back to Unity and check this out.

133
00:07:51,000 --> 00:07:52,000
All right, great.

134
00:07:52,000 --> 00:07:55,000
Now let's go and do other sounds like jump and death.

135
00:07:55,000 --> 00:07:57,000
First, we need to find player.

136
00:07:58,000 --> 00:08:00,000
Find his jump function.

137
00:08:02,000 --> 00:08:02,000
Let's see.

138
00:08:02,000 --> 00:08:03,000
Where is it?

139
00:08:05,000 --> 00:08:08,000
So in the jump here, we're going to do our job manager.

140
00:08:09,000 --> 00:08:12,000
That instance does not play as a fix.

141
00:08:14,000 --> 00:08:15,000
What was the number?

142
00:08:17,000 --> 00:08:21,000
First and second so we can do random range between two sounds, random range.

143
00:08:23,000 --> 00:08:24,000
Again.

144
00:08:24,000 --> 00:08:25,000
My range is not here.

145
00:08:25,000 --> 00:08:25,000
Let's fix it.

146
00:08:25,000 --> 00:08:28,000
Let's go above and delete this system.

147
00:08:29,000 --> 00:08:32,000
This component model also to know why it's there.

148
00:08:37,000 --> 00:08:41,000
Just sometimes Intel sends trolls different random stuff to your cold.

149
00:08:42,000 --> 00:08:46,000
Happens sometimes from 0 to 1, a weight from 1 to 2.

150
00:08:47,000 --> 00:08:47,000
Right.

151
00:08:52,000 --> 00:08:56,000
And I want to copy this line of code and put this to double jump as well.

152
00:08:58,000 --> 00:09:01,000
Now whenever we di let's go find di function.

153
00:09:01,000 --> 00:09:03,000
We're going to play this sound.

154
00:09:04,000 --> 00:09:05,000
Here.

155
00:09:06,000 --> 00:09:10,000
And your manager always have trouble to type in this.

156
00:09:11,000 --> 00:09:12,000
Oh dear.

157
00:09:12,000 --> 00:09:13,000
Manager.

158
00:09:14,000 --> 00:09:17,000
Instance Dot place a fix.

159
00:09:18,000 --> 00:09:19,000
And the number is.

160
00:09:20,000 --> 00:09:21,000
Three.

161
00:09:22,000 --> 00:09:26,000
Number three, I also think we need some sound for knockback, but let's take care of that later.

162
00:09:26,000 --> 00:09:30,000
We also need UI click whenever you click any UI buttons.

163
00:09:30,000 --> 00:09:33,000
So let's go to your main and add UI sound here.

164
00:09:33,000 --> 00:09:35,000
Add your manager dot instance.

165
00:09:37,000 --> 00:09:39,000
Place a fix, and I guess that was the last one.

166
00:09:39,000 --> 00:09:40,000
Number four.

167
00:09:41,000 --> 00:09:45,000
Yes, We also can use this sound whenever you try to purchase something.

168
00:09:45,000 --> 00:09:47,000
So let's go and find your shop.

169
00:09:50,000 --> 00:09:53,000
And assign the sound to purchase color over here.

170
00:09:53,000 --> 00:09:55,000
Now, let's go and see if all of that works.

171
00:09:55,000 --> 00:09:59,000
I'm going to bring up volume just a bit and test it.

172
00:10:03,000 --> 00:10:05,000
Music is there.

173
00:10:06,000 --> 00:10:07,000
Shop is there.

174
00:10:07,000 --> 00:10:09,000
I can hear the clicks.

175
00:10:09,000 --> 00:10:14,000
We also can find some sounds for successful purchase or unsuccessful purchase.

176
00:10:14,000 --> 00:10:17,000
I'll see if I have something I'll attach to the lecture.

177
00:10:17,000 --> 00:10:23,000
But in any case, I will show you how to find your sound effects and music later in the course.

178
00:10:23,000 --> 00:10:26,000
Just there will be a special video about that.

179
00:10:26,000 --> 00:10:27,000
How to search for sounds.

180
00:10:28,000 --> 00:10:30,000
This works this.

181
00:10:30,000 --> 00:10:31,000
We need to make it work later.

182
00:10:33,000 --> 00:10:34,000
It's going to be in the next section.

183
00:10:35,000 --> 00:10:36,000
Have to start.

184
00:10:38,000 --> 00:10:39,000
Jump is working.

185
00:10:40,000 --> 00:10:41,000
Big cap is working.

186
00:10:41,000 --> 00:10:43,000
And let's try this sound.

187
00:10:48,000 --> 00:10:49,000
Cool.

188
00:10:51,000 --> 00:10:53,000
There is something else to do at the moment.

189
00:10:53,000 --> 00:10:57,000
If you play background, music is going to play once and then it's going to stop and we need to make

190
00:10:57,000 --> 00:11:00,000
the functionality to play new song again.

191
00:11:00,000 --> 00:11:04,000
After the background music step, you need to play next song, write this one.

192
00:11:04,000 --> 00:11:05,000
We need to do.

193
00:11:05,000 --> 00:11:05,000
So.

194
00:11:05,000 --> 00:11:08,000
What we can do is just to change the functionality by a bit.

195
00:11:08,000 --> 00:11:10,000
Let's create a function that going to say.

196
00:11:11,000 --> 00:11:16,000
Public void play random BGM.

197
00:11:18,000 --> 00:11:21,000
We need a private integer for BGM index.

198
00:11:21,000 --> 00:11:25,000
So private int BGM index.

199
00:11:25,000 --> 00:11:28,000
Then we can assign random value to this index in the play.

200
00:11:28,000 --> 00:11:31,000
Random BGM BGM index equals to.

201
00:11:31,000 --> 00:11:33,000
Let's just cut it out from here.

202
00:11:36,000 --> 00:11:40,000
Paste it in here and then take this line of code.

203
00:11:40,000 --> 00:11:41,000
Go to play random BGM.

204
00:11:41,000 --> 00:11:44,000
Paste it in with the BGM index.

205
00:11:44,000 --> 00:11:47,000
So now whenever you want to change the song, you can call this function.

206
00:11:47,000 --> 00:11:52,000
It's going to stop all of the music that plays now and play new random song.

207
00:11:52,000 --> 00:11:55,000
No matter how many songs you have, is going to choose random one from the list.

208
00:11:56,000 --> 00:11:57,000
This is good.

209
00:11:57,000 --> 00:12:02,000
Now the next one is to make an update function instead of start in the update function.

210
00:12:02,000 --> 00:12:05,000
We're going to check if music is playing or not, so we're going to do it.

211
00:12:05,000 --> 00:12:10,000
If no BGM with the BGM index lane is playing.

212
00:12:10,000 --> 00:12:14,000
So if not playing, then we can play new random song.

213
00:12:14,000 --> 00:12:16,000
Play random BGM.

214
00:12:17,000 --> 00:12:19,000
In this way, we're going to do check.

215
00:12:19,000 --> 00:12:21,000
And by default, our BGM index is zero.

216
00:12:22,000 --> 00:12:28,000
So when our game starts and first update runs, it's going to see if BG zero is not playing, then we're

217
00:12:28,000 --> 00:12:31,000
going to say, Play random BGM and this play Random BGM.

218
00:12:31,000 --> 00:12:35,000
Going to take a new index from zero to BGM lengths.

219
00:12:35,000 --> 00:12:36,000
It's going to play the sound.

220
00:12:36,000 --> 00:12:38,000
It's going to play the music with new BGM.

221
00:12:38,000 --> 00:12:43,000
And as you know, inside of that, we're going to switch off all of the music that we had, if any,

222
00:12:43,000 --> 00:12:45,000
played play new sound.

223
00:12:45,000 --> 00:12:49,000
And by that moment, BGM Index is going to be new, for example, is going to be number one.

224
00:12:49,000 --> 00:12:51,000
BGM index equals to one.

225
00:12:51,000 --> 00:12:55,000
Then update going to check if BGM plane is going to do nothing.

226
00:12:55,000 --> 00:12:57,000
If it's not playing, it's going to play new song.

227
00:12:57,000 --> 00:12:58,000
Very simple.

228
00:12:58,000 --> 00:12:58,000
Very cool.

229
00:12:58,000 --> 00:12:59,000
All right.

230
00:12:59,000 --> 00:13:00,000
That's it for this video.

231
00:13:00,000 --> 00:13:01,000
And I'll see you in the next one.

