Welcome back.
In this video, we are going to have a look at sounds and how we can use them within our games.
So you can simply Google for royalty free game sounds.
And there you can find some websites which have multiple sounds, 1000 free sound effects, music tracks,
loops for games and so forth.
As you can see, you can find very many websites which have this so free sound, sound Bible, audio
and music unity as a store.
So there is one called Free Game Music, which is great and which we are going to use so well.
That's one option.
Of course you can go to pages like Free Sound, for example, they are by Carsten Frosch, who has created
quite some music.
So space sound, a sermon, tonal, earthen, which means Sun Tunnel, open, mysterious sounds and
so forth.
So if you want to have a beam within your game, you can simply download this beam here.
But on this page you will have to log in to download the sound.
But that's how the beam would sound, for example.
Well, you can google and find great sounds online quite easily, but be careful that there license
is Creative Commons zero, so it means that you may use them for free without having to pay any royalty
fees and so on.
All right.
So let's go to Unity.
Let's search for absolutely free music.
All right.
So let's go ahead.
Absolutely.
Free music and you'll find this asset here by Vertex Studio.
And you can download that so you can download the whole package and only import just the music that
you want to use.
As I have already downloaded it, I can import it directly and I'm just going to use any of the sounds
that's decompressing it.
And I don't want to use all of them, but let's just use number one, let's import that.
And in this case, I'm actually going to get rid of my canvas and my game manager.
So let's delete those.
Now I only have my cube.
Let's go back to our scene.
As you see, there is a new folder here called Game Music and it has one sound here, which is an audio
file which you can now use in your game.
So let's go to our cube.
That's where it is.
And what I want to do is I want to add a sound onto my cube itself.
So let's add a component.
And an audio component, to be precise.
And then here, audio source, let's add an audio source.
As you can see, the audience audio source now expects an audio clip.
And that here that little piece of music is an audio clip.
So what you can do now is drag that audio clip into your audio source.
And now let's reduce the volume because that would be a bit too loud.
All right.
Let's get started.
Let's play the music or play the game.
And you might hear that there are some sounds coming and you can change the pitch here.
You can change the volume, the priority any time.
And the music only starts playing because now here it says Play on Awake.
So as soon as the object that contains this audio source is awoken, we will hear the music.
So I'm just going to change the volume to zero.
Then another important piece is this loop.
Loop will allow us to run the sound over and over again.
So as soon as the sound or the music is done, it will start off from the beginning.
Now, of course, there are multiple other properties here, as you can see, and you can access those
later on via code.
Now let's have a quick look how we can start the sound via code.
So let's add a component, a new script, and I'm going to call that sound behavior, and I add that
to our cube.
I'm going to keep it simple now.
So let's open up that Sound Behavior script.
And then here, what I want to do is I want to start the sound as soon as I press a button.
So actually I need the update method and the update method is going to work like that.
I'm going to say if there is input.
So if the player presses a button, get key down and the key code will be, let's say, space.
So if the player presses space, go ahead and start sound.
So to do start sound and how do I get the sound?
Well, I need to get an audio source here.
So public audio source.
And I'm just going to call that music now.
And now I can go ahead in my update method and I can start the music
with play.
So I simply call the play method.
Now, of course, if I do that, I need to be sure that music exists so that there is some music I can
check here if music exists.
So if it's not empty, only then play the music.
What that does is it checks.
Is there an audio source in music or is there not?
And if there's none, then it will not run.
So let's check that out.
Sound behavior is going to get a new variable here.
As you can see, music.
And now I can drag the audio source and where they get it from, well, actually from here.
So I drag the audio source from here to there.
And now let's deactivate play on a wake.
So I'm going to deactivate that.
And if I run the game now, it shouldn't play any sound.
I hope you hear that.
They don't hear anything.
And now let's press the space key and the music starts.
So it's not great music.
At least the beginning is a bit weird.
But then the music starts.
All right.
And likewise, of course, you can go ahead here and check if the music is currently running.
So if music thought is playing.
So maybe we should use the square brackets here.
If music is playing, then don't play any music and else play music.
So if music is playing, don't play music.
And so it means music.
Stop.
So stop the music and otherwise play the music.
All right.
Now let's check that out again.
And we press play.
No music is running.
I press the play button.
And there is some music coming up.
You might hear it.
Right now, I press the space key again and the music stops.
I press it again.
As you can see, the music starts from the start.
I press it again and it's gone.
So that's how easy it is.
You can, of course, check out what other methods the music or the audio source class has.
So here, audio source, for example, loop mute.
You could mute the music and not stop it.
So it would still run, but there wasn't wouldn't be any sound coming out.
Then you can of course pause it.
You can change the pitch.
As you can see, there are methods and properties here.
So you could change the pitch, you can change the property mute, you can run the method pause, which
will pause the music and so forth.
So there are plenty of methods you can check them out.
And yeah, that's pretty much it.
So here, pause and on pause.
Maybe that would be the better options.
So if the music is playing for the first time, we could run play.
Otherwise we could run pause to stop it and then unpause to run it again.
All right.
So let's add here.
Stop.
And we're good to go.