In this lecture, we're going to start adding some background music for our game.
And the first step for this is to find some music for our game.
So I've gone over to open game dot org, which is a fantastic resource for indie developers.
And if we head up to browse and then down to music, we'll have a whole bunch of music that we can choose
from.
Now we can narrow down our search with the search parameters on the left here.
So first of all, I'm going to narrow down my license to look for CC zero.
So copyright free, we want some music as our art type.
And if we have a rough idea of what sort of thing we're looking for, we may want to narrow down our
search even further with themes and things like that.
I actually got pretty lucky though, when I was listening through some of these tracks and in the very
first one in Chip Tunes Action, I found some pretty cool music for my game.
And the great thing about this site is that we can also preview our music before we download it.
So we just hit the play button and we can hear our music and this sounds pretty epic for a space battle,
so I'm going to go with that track and if we scroll down, we can select our files down here and we
can download our zip file.
Once we've downloaded that, we need to import it into our project.
So I've dropped mine in the assets pack.
So we've got our five action chip tunes in here and we can also preview them in Unity.
So if we click on one of our tracks and go down to the preview window, if you don't see the preview
window, it might be closed up down the bottom like it is here.
All we need to do is just drag it up.
Now we can go ahead and hit the play button.
And here's one of those other tracks from that hack.
Now, before we start adding this music to our game, I've noticed that our asset pack is starting to
get a little bit messy.
We've got some materials in there in various places, so let's do a quick bit of housekeeping.
I'm going to right click and create a new folder for our backgrounds and we'll drag the star filled
Star Zero and stars one into that.
And then in the assets folder, I'm going to create a new folder specifically for our materials that
we've been creating.
So in here we had the background so we can drag that in.
And in our space shooter folder we also had our M explosions.
So let's drag that into the materials folder as well.
That just makes them nice and easy to find and separates them away from the asset packs that we've been
importing.
So that's the housekeeping done.
So now let's go over to our audio player and see what we did.
Last Lecture.
We created this audio player script, which held a audio clip to be played and specified the volume
that we wanted to play it.
And if we open up our audio player, then we were using this audio source play clip at point.
But for our music, we don't really want this one shot audio.
We want it to be continuously playing in the background.
So let's get a better idea of how unity actually handles music in our game and understand what this
audio source actually is.
Well, the audio in Unity really comes in three parts.
First of all, we need an audio listener, which is the thing that will allow us to hear the audio.
And you may be wondering where that was sitting when we were doing this one shot audio and I'll show
you where that is when we go back into Unity in a moment.
Next, we need an audio source to play our audio.
So that was that audio source play clip at point.
And again, I'll show you where that was sitting with our one shot audio.
And finally, we need an audio file.
So the actual sound that gets played, so if we follow the arrows here, we have our audio file feeds
into our audio source and our audio source gets listened to by our audio listener.
So let's jump back into unity and I'm going to hit play and pause very quickly.
And if we play to the point where we have our laser being instantiated, we'll see that we've got this
one shot audio clip.
And if we click on that one shot audio, we'll find our audio source without audio clip attached.
So we were passing that audio clip from our audio player over to this audio source.
And the listener for this audio source actually sits on our main camera.
So if we look on our main camera, right near the bottom, we've got this audio listener.
And that's why when we were playing our one shot audio, we were telling it to play at the camera position
for the clearest sound.
So if we come out of play mode and go back over to our audio player, let's have a think about how we're
going to play our music well rather than mess with the audio player.
For now, let's go ahead and add a new component to this game object.
And the component we want is an audio source.
Now, this audio source has a lot of bells and whistles and levers that we can pull.
And it may be a little bit overwhelming unless you've done any audio engineering in the past, but for
us we can actually ignore the vast majority of what's going on here.
We're going to be playing two D sound, so everything in this three D sound setting can be completely
ignored.
So we'll just roll that up.
And the only things we're interested in is our audio clip, which is the thing we want to play.
So let's go over to our audio and drag the clip.
We want into the audio clip.
We don't need to worry about the output and we can see here that it's expecting an audio mixer.
It's a bit overkill for our game though, so we really don't need to worry about that.
We have a mute option so we can mute and unmute our audio.
We don't need to worry about any of the bypass effects for our music.
We will want it to play on Awake.
So the moment the game starts, the music will start with it.
But if we wanted to manually trigger our audio to play, then we could turn that off and then control
that in code.
And then finally for our checkboxes, we have a looping option and our music wants to be able to loop
continuously through the game.
So let's enable that and hopefully for your music that you found.
It is a looping tracks.
There won't be any hard cuts between the beginning and the end, then down onto our sliders.
We don't need to worry about the priority or the pitch or the stereo plan or any of that stuff.
The only thing we really care about is the volume.
So that's our normal volume slider like we've added to our audio player and we could arguably pay attention
to the spatial blend as well.
As we can see, it goes from two D to three D.
So what is that doing?
Well, when we set it to two D, the audio listener isn't really going to care where the sound is originating
from, but in three D it will affect how loudly we can hear that sound based on where we're viewing
it from, from the camera.
So if you think about three D games, if something's off in the distance, it's not going to be as loud
as something that's right next to you.
So not massive.
Be relevant for our game.
The only thing we really need to be aware of is that it should be set to two D.
Now if we go ahead and hit play, we should find that audio source with Play on Awake.
We'll start to play our music straight away.
So let's hit play.
And we do in fact get our music playing right out of the gate.
And this has really drastically changed the feel of our game.
For me, I think it makes the game feel a lot more exciting and as though we're going into battle against
these invaders.
So music selection is also incredibly important.
So if we went with something more subdued, it would feel significantly different.
So Music Choice is also a key part of your game design.
So congratulations on adding music to your game.
And with all of our sounds added, it's now time to start thinking about the user interface because
our player can take damage, but we really have no idea of how much health we've got left or how many
points we've been scoring.
We'll start our journey into that in the next lecture, so I'll see you there.