How To Add Sound Effects the RIGHT Way | Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome today I'm going to show you how to properly set up your music and sound effects in unity so that you can play all your sounds from an audio mixer this will let you use different effects for different sound groups as well as control your volume with a slider from a menu let's get started so I have a really simple scene here with a player and a test enemy and if I hit the Escape key we've got a test menu this currently doesn't do anything we will go through that together you don't need assets to follow along with this tutorial however all of the assets you see in this project can be downloaded for free you can use them personally or commercially that's totally up to you I've left a link in the description I hope you enjoy so let's start off by just grabbing some sound effects first I will leave a link to this down in the description below but this is called chip tone and it's just a really nice sound generator tool that you can use to download free sound effects I'm going to click hurt I'm going to save that one and again and one more time now I'm just going to drag all of those into my sounds folder there and I've just renamed those to hurt one two and three so first let's just get a sound effect plane when we damage our enemy so what you might think that you want to do is you know add an audio source to your enemy and then let's just get a sound effect playing in our enemy Health script right here where we call damage so let's create a field for our audio clip right so we've set up a private audio clip called audio sound clip let's also get a reference to the audio source called audio source and let's get that component in our start function and then down here you know you could say audiosource.clip equals damage sound clip and then you could say audiosource.play and if we drag in a sound there let's uncheck play on awake and let's try that now doing it this way we can already see a problem right when we damage our enemy for the third time and we kill him because his health is at three we then destroy our enemy game object which is going to destroy the audio source as well so the damage clip is not going to play that third time and that's definitely not what we want so let's get rid of the audio source and we'll get rid of these and this and this so the solution that you might come across after looking into this is you can just directly call audiosource Dot play clip at point and you pass in a clip which is our damage sound clip we can also even give it a position in the world so we'll just say transform.position and you can also pass in a volume and we'll just put it at full volume so there's no audio Source on our enemy nothing like that and yet when we play you can see that that fixes our issue however this introduces a new problem if we do play focused and we do that again and there it took me a couple of tries but I managed to pause while it's actually got this object in the scene so if you do play clip at Point what it's actually going to do is temporarily create this game object in your scene which has an audio Source on it with your audio clip now remember our goal with this video is to be able to control our volume with these sliders here but in order to do that we need to be able to access this output and put our audio mixer in here and you cannot do that with play clip at Point there's just no way to access this audio source so that is not what we want either foreign so what we're going to do is set up our own script to handle this so inside my scripts folder and inside of managers I'm going to create a new c-sharp script called sound effects manager I'm going to create a new game object called sound effects manager let's reset the transform I'm going to move that up here and assign sound effects manager to it let's open that up let's get rid of all this now I want to be able to call this very easily from anywhere so we are going to do that by making this a Singleton so that is super easy to do all we have to do is say public static sound effects manager instance and then create an awake function and inside of there say if instance is equal to null then instance equals this what that allows you to do is let's just set up a quick public void example function if we call this from anywhere else we can say sound effects manager dot instance dot example function we can call it like that without having to grab the component or do anything like that we can just call it directly you only ever want to make something a Singleton if you know there's only going to be one of them in the scene ever so just be careful when you're setting these up let's get rid of this so let's create a function that will essentially do the same thing that play clip at Point did so we want to instantiate a game object into the world we want it to play the sound effect and then we want it to destroy itself after the clip is done playing so we're going to say public void play sound effects clip and we know we're going to need to pass in some sort of audio clip let's call that audio clip and just like play clip a point I also want to pass in some sort of transform let's call that spawn transform okay so how is this going to work first we want to spawn in the game object then we want to assign the clip then actually it would be nice to be able to assign some sort of volume so let's also add a float called volume up here then we want to actually play the sound then we want to get the length of our clip and then we want to destroy this game object after a certain amount of time now we don't have to get a reference to the audio Source because what we can actually do is say audio Source let's call it audio source is equal to instantiate so what are we spawning into the scene well we're going to need to set up some sort of Prefab game object so let's get a reference to that up here now normally you would say private game objects sound effects object but then we would have to include the extra step down here of having to grab a reference to the audio Source if we just assign it as an audio Source directly then we can actually grab that component while we spawn it in at the exact same time so now we're spawning in our sound effects object where are we spawning that in at our spawn transform dot position and we don't care about rotation so let's just say quaternion.identity so next we want to assign the clip so now we actually have a reference to our audio Source here so we're going to say audiosource dot clip is equal to audio clip which is being passed in here to assign the volume we'll say audiosource dot volume is equal to volume play the sound I'll say audio Source dot play to get the length of the sound effects clip let's set up a float called clip length and that's equal to our audio Source Dot clip.length and then finally we'll call destroy on what on our audio source.game object after how many seconds after clip length now let's not forget that we set this up up here so before we forget let's go ahead and sign that in the inspector so to create our object let's go up here and say create empty sound effects object let's reset the transform let's add an audio Source let's uncheck play on awake and I'm going to drag that into my prefabs folder let's delete that and now we can assign that to our sound effects manager so finally to test this out let's actually call this when we damage our enemy so we can say sound effects manager dot instance dot play sound effects clip what are we playing our damage sound clip what's the spawn transform just the transform and volume we could create a serialized field up here or we can just pass in a one here for full volume let's give that a test there you go and you can see just like play clip at point you can see that when we create it it very quickly instantiates into the scene and then destroys itself one thing that's always really nice is to have a couple of sound effects clips and then to play one at random so let's set that up very fast in our sound effects manager let's copy and paste this let's rename that to play random sound effects clip and instead of just taking in an audio clip let's have it take in an audio clip array so the only thing that needs to change here is we need to grab some sort of random index so we're going to set up a private integer called Rand and that's equal to random dot range so the minimum is going to be 0 and the maximum is going to be our audio clip dot length which is the number of Clips in our array and now when we assign the actual audio clip down here what we can say is audiosource.clip is equal to audio clip of a random index now if we go back to our enemy health and we change this to a private audio clip array called damage sound Clips let's get rid of this one and instead call soundfxmanager.instance Dot play random sound effects clip we're just going to pass in damage sound Clips at the transform at full volume and finally let's lock our inspectors so that we can actually grab all three of these and drag them onto our damage sound clips there you go so now let's get this set up to be working with an audio mixer let's go to window over here go to audio and select audio mixer I'm going to dock that up here and let's create a new mixer called main mixer all right so I'm not going to go through all of these because that's outside of the scope of this tutorial however there's all sorts of awesome things that you can do with this it will automatically set up a master node within attenuation effect attenuation essentially just allows you to control the volume that's really all it means now what we do care about is the groups over here we want to be able to play our sounds in different groups so let's create a new one let's call that sound effects and let's create one more select the master and click plus and let's say music so you'll see both of those were set up with attenuation as well which is what we want so what we want to essentially be able to do is inside of our slider functions here if you scroll down you'll see on value changed we want to be able to assign something in there that controls these sliders here and to do that we're going to need to expose some parameters in the audio mixer so if you select the master you'll see your inspector changes over here and if you right click the volume we can say expose volume of Master to script click that and now you can see exposed parameters 1. if you click that and double click this we can rename it to master volume and let's click sound effects and do the same thing right click click Expose and let's call that sound effects volume and music right click expose we'll call that music volume so back in my scripts folder I'm going to go into my managers and create a new c-sharp script called sound mixer manager in my scene here I'm going to create a new empty called sound mixer manager let's reset the transform I'm going to drag that up here and add the sound mixer manager script to it and open that up let's get rid of this we want to grab a reference to this audio mixer here and in order to do that we need to go up here and use the Unity engine dot audio namespace so let's say serialize field private audio mixer called audio mixer and before we forget let's grab a reference to our main mixer so we are going to create three functions one called set master volume with a parameter float called level another one called set sound effects volume with a parameter float called level and a public void set music volume with a parameter of type float called level now these are the functions that are going to be called inside of our slider on value changed so every time we change the value we're going to call one of these corresponding methods we're passing in a float called level because that is going to correspond to our value right here so what are we doing inside the master volume we're saying audio mixer dot set float we need to grab the string names exactly so it's just easier to go back here double click and Ctrl C let's throw that in there and the float value will be our level let's throw that in these methods as well okay and so now you can see that our normal volume range corresponds between 0 for the Max and minus 80 for the minimum so let's change our values down here our minimum value is going to be minus 80 and our max value is going to be zero let's do that for the other ones as well and now to actually call these functions when we change the slider value let's click plus here where is the function stored it's stored in sound mixer manager and if we click here and go to sound mixer manager we want to go up here to Dynamic float which means it's going to dynamically pass in this value right here and this is the master so we want to set master volume now let's do the same for sound effects and music I don't currently have a way to play music in my scene so I'm going to very quickly create an empty called Music Manager I'm going to reset the transform and then move that up here I'm going to add an audio Source I'm going to keep play on awake checked as well as check on Loop and this is just a random song track that I have here I'm going to drag that in there we go so I will have music that plays by default right away now how do we tell our audio mixer which one is sound effects and which one is music up here in our music manager in the audio Source we can click right here in output and select our music group under the main mixer and for our sound effects we can click on our sound effects object open up the prefab and do the same thing except this output is going to be the sound effects group from the main mixer foreign see that working there of course the master node will control both of them [Music] now the one problem that you may have noticed is the volume doesn't get quiet the way that you would expect we are assigning this value from the slider here which goes linearly from -80 to 0 to this fader over here the problem though is the way that decibels and Sound Works this does not affect your volume in a linear manner this changes in a logarithmic Manner and really you can just think of the decibels changing on like a curve instead of in a straight line linear manner and I'm not going to get into what that means but what I am going to show you is how to fix it so this is not the correct function that we want what we actually want is to say audiomixer dot set float master volume and the level is going to be math F DOT log 10 passing in our level float times 20. this is one of those things where I really I don't know what this math function means I'm just I'm not going to lie I have no idea what the heck this means all I know is that the math that goes on behind the scenes here is it will properly translate your logarithmic interpolation of the volume into a linear interpolation of the volume so we changed our code the very last thing that we need to change is inside of our sliders we don't want this going from minus 80 to 0 we actually want this going from 0.0001 to 1 and let's default the volume to full and let's do it with the rest as well there we go so now you can see as I slowly increase my master slider this jumps up really fast but then slowly creeps its way towards zero so this is the way we actually want it to work there you go guys I hope you found that extremely helpful thank you so much for staying all the way to the end and please let me know in the comments down below if you have any future ideas for tutorials you would like to see thanks and I hope you have an awesome day bye
Info
Channel: Sasquatch B Studios
Views: 9,234
Rating: undefined out of 5
Keywords: unity tutorial, unity 2d, unity audio manager, unity audio mixer, unity sound effects, unity sound manager, unity sound slider, sound effects, how to sound design, unity tutorial for beginners, sasquatch b, sound manager, unity audio, unity audio tutorial, unity audio play, unity how to play sound, unity sound, unity sound effects manager, unity audio mixer tutorial, unity how to turn down audio in menu, unity audio menu, unity separate sound effects and music
Id: DU7cgVsU2rM
Channel Id: undefined
Length: 15min 21sec (921 seconds)
Published: Thu Dec 08 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.