Ultimate Unity Audio Tutorial | Step by Step Guide 2024

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys it's matt welcome to speed shooter and today we're going to be looking at adding audio and all the different ways that we can add audio effects to our game whether that be adding a 2d or a 3d sound whether that be having a 3d sound and affecting that over time being able to control how we should and when we should play our audio source be able to choose an audio clip and when to play it to be able to affect a sound on a trigger on an event or to be able to affect ui buttons we'll also look at stopping sounds or checking to see if sounds are playing so you can use that in your code to make decisions when you will need to play or stop sounds and then we'll look at audio mixes to be able to control bunches of sounds together and we will go through all that today and i will put all the little annotations in the timeline for you to check them out so be sure to let me know what you think and if you have any other tips for users in the future so i'm going to add elements of sound to this game because it's silent at the moment and that doesn't sound realistic i always say to everybody that when you're making a game or any environment go out or look for reference to see what sounds you can hear in any given moment so if you're sat in your house at any one time can you hear the sound of your computer your fridge a fan the outside can you hear some wind trees birds anything like that and the more you add the more creative and believable your scene can be so if we grab any sound that we've got and we can drag it and drop it into a hierarchy and it will create an audio source for us remember that an audio should be a wav file or similar and you can check the unity documentation because that means it will loop correctly whereas other compressed file formats like mp3 don't loop as well now we'll go through the audio source basically here and you can see the audio clip is my general breathing i've got an output which is the audio mixer which we will go through we've got some bypass effects that we don't need to be concerned with but you can look at the unity documentation if that's what you want you can look at the play on awake means that the sound will start as soon as the game plays i'll also make sure that i take a loop so it always keeps repeating no matter what happens you just need to make sure that your audio track does loop you look at the priority which is usually to be kept at one 1 2a you can look at the volume and this can be adjusted between norton one the pitch usually left at one unless you specifically want to adjust a pitch to make it lower or higher you can look at the stereo pant to which side of left or right of your ears the sound effect would come out of more whereas 0 is just by default you've got the spatial blend which is either 2d or 3d and 2d is the sound will be heard from no matter where it exists in your scene it will be heard the same no matter where you are or wherever that position of the audio may be whereas if you put it all the way to 3d it will attenuate over a distance and say we put our audio effect over in the mountains and we don't we'd attenuate the distance to not far away from there only will be close do we hear it but for this breathing sound effect i want it to be 2d because it's going to be really near our character we'd be able to hear that because when we breathe out we can hear that and then the 3d settings are appropriate if you've got a 3d sound so in this case i've got loop plane awake and spatial blend i'm going to just turn the volume down slightly just for this current audio effect and you can drag as many as you want into your hierarchy and when we press play you'll be able to hear it and you could hear that sound effect in that little test that as i said about making more and more overlaps with the sound that you use i've got a wind loop and we can add that in exactly the same way so we can just add another audio effect which can just be dragged and dropped into our hierarchy and again we can play an awake we can press loop we can have it as a 2d sound because it will just be in our entire environment and we might want to turn that down to 0.52 and as you can hear in the background you can hear the sound of the wind and the breathing together as one now we've got another example of how do we add a 3d sound effect so you can see over in this side here i had some crows that spawn and move around i'll just activate my gizmo so they spawn in this dark blue box here and now we want the scrollers to make a sound effect that when we walk near them we'll be able to hear them but when we walk away we won't like when we're over in the direction we don't want to be able to hear them so we don't want to be a 2d effect i can just drag the chrome mix into my hierarchy so i just moved my audio source and positioned it roughly where i expect the cross to be and i want to be able to hear it when i'm on the bridge so you can set this to a special blend 2d sound we can keep it plain awake and loop we can now have access to the 3d sounds and i don't really like the doppler effect it changes the sound of the pitch based on the velocity of the movement of the character so i usually like to set that as zero so then when we move it doesn't get a weird distortion but you can test that out for yourself you can have different types of roll off a logarithmic linear or custom logarithmic you can see that it will attenuate over time and fade into distance but you will always be able to hear it unless you get very very far away but in my case i like to use a linear roll-off so if you get out of the boundary of the actual audio source that we're going to have then you won't be able to hear it so what i usually like to do is set the max distance all the way down until we can see the actual bounding sphere which is just on the edge if you can see it here just there so i'm going to set the max distance to about 100 and then i'll set the min distance to about half of that value so we attenuate between those two distances and then you can adjust between the min and max volumes and i do actually have a tutorial i'm looking at 3d sounds and explaining the differences between the different variations that you have but this is something that you can test out for yourself thing is we've got some audio effects that we've added ambient sounds and things like that but now how would we play an audio source or play an effect that we might have that we want to play at any given time so say i add another one which is tumbling rocks here and i don't want that to play on a wake or loop i want to play that only when i tell it to do that now how would we go about doing it it's going to be a 2d sound so our could go create c-sharp and i'm just going to call this playing audio source so we're going to create a bunch of different scripts to do different things we'll open up a visual studio now what we can do is get rid of the two starting methods and in this case we can write square brackets serialize field and then we'll write private audio source and then we'll call this as my audio source and from there underneath here we can write a maybe an update method so we can do an input so when we press e it's going to do it but it'll be the same whatever you choose to do so we'll just say avoid update create if input dot gets key down open back it's in quartz is keycode.e for instance then under here this is our line of code which control playing the audio source will say my audio source dot play with two brackets in a semicolon and it'll just play the method which is in the audio source functionality so when we press e we will play the audio source that we specify so what we can do is go back into unity now we'll create an empty game object and we'll just call this game object our audio controller then we can add our playing audio so script it'll look for it will need an audio source so we're going to have tumbling rocks so i'm just going to mute the audios that we have i'll get rid of them for now while we test this and now when we press the e button you'll be able to hear the tumbling rocks that activated exactly when we wanted it to now you know how to play an actual audio source with just one audio clip on it how do we specifically play with this audio source it might control all our effects we want to play how do we play a different sound effect on that audio source so then we can maybe minimalize how many audio sources we might have a million so in this case we'll say create another c sharp script and name this play specific audio we'll open up in visual studio again and then in this case we can copy exactly what we had for our previous script which for playing an audio source and we can just paste that into what we have here because we're going to base it on that we still need our audio source because we need to reference what that will be we can under here have another serialized field call that private audio clip this time as the audio clip that we're going to choose to change it to and this can be my audio clip one as an example and then what you could write under here as well is the you need to give another parameter to this is the volume so we can say private float volume set that equal to 1.0 f with a semicolon now in this case we could get rid of the line that we had when we pressed e and we can say that my audio source dot play one shot because that will play the audio that we want to play just once and brackets is my audio clip one so the variable that we created and then we can have a comma of our volume and then add a semicolon so now we will play on the audio source the clip that we wanted just once with play one shot because player one shot allows us to specify an audio clip and then specify the volume so we can go back to our audio controller you can add the play specific audio script i'm just going to disable my old one and you can see we need an audio source so in this case i can add the tumbling rocks again we can keep the volume at 1 and i might want a different audio clip so in this case i'll just add an alarm sound effect into there so this is that's going to what that's going to be what's playing but i'm going to turn the volume down because it'll probably be a loud sound effect so i'll put it at 0.2 then we're going to press play so when we press e at this instance we had that the tumbling rock audio source which had that effect but we expect to hear the alarm which we did in this case so it's working perfectly and as we wanted and don't forget that you can get all these scripts and this project without the 3d assets on my patreon and i will upload it all so you can get access to all these ways to use your sound effects in one go we might want to play an audio source on a trigger event so when we walk into a particular area like say we walk into the very beginning of the bridge here we might want the little tumbling rock audio sound we could again create a new script and call this play audio on trigger now when you're playing something on trigger you could play it in the same way with an audio clip or you could play it by just playing the audio source but maybe in this case we just want to play the audio source so i'll just grab from the other script that we had with just playing an audio source by itself which is fairly simple i'll copy it to use it in a little bit so now we can say in this method here we're going to need we're going to need to create a trigger event method so we can go void on trigger enter and then when i press double tab it will complete it for me so we have collider others it'll look for a collider and we'll just give it a name of other if you wanted 2d you can just name that ontrigger enter 2d collider 2d just like so and that would work in a 2d game but if we've got a 3d game we can have it like this now we can say that if other dot compare tag and the player is going to walk into our trigger event we can just have a tag of player as long as your player is tagged that then we can do our variable which was up here which was going to be our audio source so remember that we needed this private variable to reference it so that can be there and then we need the line of code to be able to play so we can just copy that straight in here so whenever we walk into the trigger event you'll be able to play the sound effect so we'll go and test that out now i'm going to go game object 3d object and create a cube just going to drag my cube up onto the top here i'm going to put it at the very start of my bridge just here and i'm just going to scale this out so we'll definitely walk through it we need to make sure that it's a but it has a box glider we set it to his trigger on our player we need to make sure there is tag player in our case and you may need to add the tag if it doesn't already we can rename this our audio trigger and we can add the play audio and trigger so we can add that to the box we'll look for the audio source that we want to play which is the tumbling rocks and we should be ready to go so we can press play and as you can see when i walk through the actual object here you can see that the sound effect played when we walked through it and of course you can maybe disable this object if you want to want the sound effect to play once or play it every time that you do it and of course like i said you could change this trigger event for it to be one which you can play a specific audio or you could play like we've done here just the audio source itself we can do something similar to this and we can actually create an event so we can play as many different sound effects as we want without specifying in script so what i'm going to do is i'm going to just comment out what we have here and at the top we can keep this script which is our trigger but we're going to use unit using unity engine dot events and now in our trigger event now we can create a new variable and call this square bracket serialization field private unity event and call this my audio event something like that with the semicolon now on in our trigger event we can say my audio event dot invoke which will activate the event system so we'll be able to access that and do different things upon multiple objects so we can now minimize this and this is still a triggered event that when we walk into it will do something but now we have access to an actual unity event so we can add when we walk into this we could literally add three different effects it doesn't have to be audio this could be anything we could add some particles and stuff without actually specifying in script exactly creating variables doing everything like this so this could be added to loads of different objects and make it easier now how do we activate a sound effect just like we just did almost with that event with at least one event on we can drag the game object which is the sound effect tumbling rocks to the slot will ask us to select a function so we can go audio source and we can just choose play so in this case it when we walk into the trigger we will play the audio and of course we could play another just like i'd buy an another or do different things from that so this is another way that you could do it and i won't test that out because it's very very similar another thing i will show you is how to do this with uis and buttons and things like that now if we right click go ui and we'll create just a button and it will create a canvas for us now on a button it has a unity event and it has an on click event and we can do it in exactly the same way so we can add a plus choose a game object so let's say for instance we could have the tumbling rocks again and again you could say audio source and play every time you click it will play the tumbling rock sound effect which is great if that's what you want your game to do but say you want that when your cursor enters that button you want a sound effect rather than when you click you can add a component and choose event trigger if you type that in and then you can add a new event type which will be point to end to exit pointed down so we can say pointer enter so every time your pointer enters the button which might be on screen we can then play a sound effect just in exactly the same way now you might ask me how do we actually stop a piece of audio or an audio source that might be playing well all we actually need to do is just select my audio source and just press dot stop so that will even if we're playing a piece of audio and we put stop it'll just stop it as soon as it started really so in this case we press e to play a sound effect but we might want to check if the audio source is finished playing its previous audio so we can write an if statement and we can say if and then if we put an exclamation mark we can say my audio source dot is playing and then under here we can encapsulate that statement in curly brackets again and we can say that if my audio source is playing and the exclamation mark at the beginning means if it's false so if it isn't playing anything then we can play this clip we could say that in this case if it's anything else so else if it was playing an audio uh clip we can say my audio source dot stop so we're just checking different scenarios of course we could do it the other way around so we could just take the exclamation mark so now we're saying if my audio source so if we are playing a sound effect maybe we want to overwrite that and play a different one so we can play this line up so we can play this line of code but it's entirely up to you i'm just showing you different instances of how you can control with stop with checking it if it's playing you can actually use things like dot pause too which instead of stopping it which would stop it and make you have to restart it from the beginning if you pause and then resumed at some later point with dot play it would resume from where you actually pause the audio from so they're two slight differences and there is other things where you can do an audio source play delayed so you can wait an amount of time before you play the audio effect which is similar which is my audio source dot play delayed and you could specify that you want to delay it for three seconds before you play the next sound effect it's exactly like audiosource.play it doesn't play a specific audio effect it will just delay the playing of the audio source now from here what we've just gone over the basics of being able to control it with different scripts be able to control it with different effects add our things to it now we may want to add an audio mixer to control bunches or groups of sound effects so we can edit that in a settings menu or something like that now what we can do is in our project panel we can right click create and we can choose audio mixer and i'm just going to call this our game audio mixer i'm going to double click that and open it out and we can just dock it just down here you can see it here and you can see that we have a mixer and you can have more mixes if you want snapshots groups and views groups are the things which we would group game objects together master means that every single object in the world is affected by this master now what we can do is we can press the little plus and you will see that we will get a child of the master so in this case this could be our ambient sounds so in this case the master would affect all sounds but the ambien may be well just affect only ambient sounds and then we could maybe have another which we call music and the music is what we affect and we're just going to make sure we parent this to the master as well so the master affects both of these collectively if music was a child of ambient sounds ambient sounds would also affect music and that's not really what we want so in this case we can set these two things now let's say we've activate all our bunches of audio here in our scene and we say that all wind loop that we can look at its output and we can put the output as we may want to set that to ambient sounds zombie sounds is the audio mix for that crow can be ambient sounds too as an example the tumbling rocks could be our music sample and i want full screen this so you can see so here you can hear that you need to specify um to be able to edit in play mode when you're on it so when we turn the master down everything will go down at once to nothing and then in our other instance we've got ambient sounds which can take all the way down and will only affect our ambient sounds and then if we turn music all the way down the sound of the rocks will disappear too so this is how we can adjust and you've got to remember that when you untick play mode in this instance it will remember your changes unlike the rest of unity so you might ask me then how do we actually change this volume at runtime or in script so then you can select say ambient sounds here look at one of the properties which is the attenuation of the volume we can right click that and say expose the volume of ambient sounds to script so we're going to be able to access that so we'll click that button and you can see that now we have an arrow to say that it's exposed now we can see that we've got exposed parameters down here in our audio mixer options we can click that drop down and we can see we've got my exposed param for of the volume of ambient sounds we can change this by double clicking it and we can rename this to ambient volume now we could create a new script which we could go create c sharp and we can just say this audio mixer ambient volume as just an example script we can open that in visual studio we can get rid of the starting methods but up here we need to use namespaces to access the actual audio mix as we say using unity engine dot audio then we need to create a reference to our audio mixer so we'll have square brackets serialize field private audio mixer and then as i have this is type of my audio mixer as an example and then under here we could have something which is just another update method and then we'll say if input dot get key down key code dot e again and then we can say that our my audio mixer dot set float and open brackets we can specify a name here and then our name can be our name that we'd set so it was going to be ambient volume and then we need to specify a float and remember in an audio mix you can see that it goes from positive 20 to -80 because it's in a range of decibels not actually the attenuation normally for just the basis of this so we can do is go back into our script we can create a new variable and say private float and we can just call this ambient volume so we can use another attribute for our variable which is called range so you square brackets if you put range you put up in brackets and you can set your min and max value so my minimum value will be 80. my maximum will be 20. so if we do that we'll go from 80 to 20 and i will set my normal volume to zero because that will be just a normal volume and now we can specify our ambient volume with a semicolon on the end of this so now we can minimize this go to the audio controller and if we add our audio mixer volume to that i'll just get rid of my other script you can see that i'm looking for an audio mixer so we can add our game audio mixer and you can see that the ambient volume is currently at zero we can go all the way to minus 80 or all the way to positive 20 and that's the only way that our slider can actually go in the inspector so we can actually somehow break the sliders that we have and we might just set it to minus 41.3 just for the sake of this so now when we press play and we press e we expect it to adjust the ambient slider how we would like it so when we press e you saw that our ambient went all the way down to -41 on our attenuation the audio mixer so i hope you liked all these tips that i showed you to add audio effects to your game let me know in the description if you've got any help any tips any ways to make this better anything that you've got to share with the community and i will add this to my patreon which is all the separate scripts the entire project but what a feature these 3d assets it will feature everything that you can get hold of and be able to use in your own games from the get go so be sure to come and join my discord if you want to chat come and check out my great assets on the unity store so thank you so so much for watching don't forget to like comment subscribe cheers
Info
Channel: SpeedTutor
Views: 29,859
Rating: undefined out of 5
Keywords: how to add audio to unity game, learn unity audio, learn unity sound effects, creating unity audio, unity audio tutorial, unity sound effects tutorial, introduction to audio in unity, programming audio mixers unity, unity audio mixer tutorial, scripting audio mixers, audio source play unity, audio source stop unity, unity sound tutorial, unity sound effects, audio tutorial unity, unity, unity tutorial, speedtutor, unity audio, audio unity, speed tutor unity, speed tutor c#
Id: YnIiMCnAf9E
Channel Id: undefined
Length: 25min 5sec (1505 seconds)
Published: Sat Jul 31 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.