Introduction to AUDIO in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
👍︎︎ 2 👤︎︎ u/raikuns 📅︎︎ Jul 14 2021 🗫︎ replies
Captions
in this video we're going to have a look at adding audio to your game the first part of the video will be an introduction to audio in unity we'll be setting up an audio listener and adjusting some audio source components to add sounds and then in the second part of the video I'll show you how to make an audio manager the audio manager makes it really quick and easy to add new sounds and play them through script another link where you can download it in the description but to those of you who are interested in how you can program the audio source component we'll go through and make an audio manager from scratch alright let's get started so as you can see I have a project open this is shrinking planet it's my newest Ludum dare submission and I've gone ahead instructed from all the audio so when we play the game now it should be totally silent so let's add some sounds the first thing we need when adding audio to our game is an audio listener the most common place for this is the camera if you select your main camera you will probably see an audio listener there already if not you can always just go and add it by hitting add component I'm searching for audio listener all this component does is well listen for sounds that also means that the game object that you put your audio listener on will be the place from where you hear sounds in 3d that's why it's most commonly put on the camera the next thing we need is an audio source if we go to any game object in our scene I'm just going to select the player we can go ahead and add a new component let's add the audio source this component it's responsible for playing an audio clip that's also the first thing we have to specify I've gone ahead and imported a few audio clips usually supports most file formats or design mp3 if we click on any of them a few settings will appear you can squeeze some performance out of your audio by messing around with these I'll have a link in the description if you want to read more about that and if we go ahead and hit play we can have a listen so let's now select a player and drag in the car sound by default our audio source is set to play on awake so for now hit play we should actually hear the sound awesome we have a bunch of ways we can configure this most important is the volume of our sound and the spatial blend this controls whether or not you want your son to be played in 2d or 3d space if you select 2d it will always be played with the same volume in both speakers however if we slide it towards 3d the volume of the sound is going to depend on how close our audio listener is to the audio source and it's also going to factor in stuff like the Doppler effect where sounds moving towards you or more high frequent and sounds moving away you can pitch down so let's try and set this to 3d and have a listen I think that's a lot better for our game finally this sound should be looping and so I'm going to check off the loop now in my game I have meteors that crash down upon the planet and when they hit the planet I want them to make an explosion sound luckily I have it set up in such a way that every time a meteor hits the ground it's going to spawn a crater object and so we can simply go ahead and find the prefab for this crater add an audio source and I have a sound set up for this that the meteor hit it sounds like this so we can simply drag that in make sure it's set to play on awake and you'll be using the fact that play on awake means that the audio will play as soon as the object gets spawned in so if it's enabled right when the scene starts it's going to play right away but if it's spawned in at a later point it's going to play at that point and we definitely want this to be in 3d space but we might go for something like point 8 if we now hit play and wait for a meteor to come crashing we can hear the impact so you should now be able to go through and add audio sources to many of your game objects I've gone ahead and added one to the meteor as well this much like the car is said to play on awake and is said to loop and it's just the sound of the media flying through air and so without any coding we've actually added a bunch of different sounds to our game however some sounds aren't tied to a particular game object and except that this could be the click sound you hear when you hover over a button or the game over voice you hear when your player dies as well as of course music there's a bunch of different ways to go about this we could go in and add a bunch of empty game objects with an audio source on each and then through script access that component and called dot play when necessary but I don't think that's be clean so I've come up with a simple audio manager that I use all the time I'm of course going to have a link to it in the description in case you don't want to code it for yourself but in case you're interested here's how it's done so first let's go and create an empty game object let's reset the transform and let's call this one Audio Manager let's drag the game object to the top so we can always see it let's then hit add component and we want to add a component called Audio Manager it's going to be of type C sharp and let's say create an app let's double click it to open it up in visual studio in the top here we can now delete the two using tags and instead write using unity engine dot audio many of the new audio features in unity are now wrapped inside of this namespace so it's a good idea to include it whenever you're working with sound now the main idea for this Audio Manager is to have a list of sounds where we can easily add or remove sounds as we go and each sound has some different properties it has an audio clip a volume and pitch setting the possibility to loop and you can add stuff like volume and pitch randomness or spatial blend then when we start the game we go through the list and for each item we add an audio source with the appropriate settings then we want to play a sound we simply call a play method on the audio manager where we input the name of the sound that we want to play and the audio manager is then going to find the source with that name and play the sound this makes everything so easy to use so first we need a list of sounds but we want to control what data is stored in each sound and so let's go ahead and create a custom class called sound to do that we go inside of unity we go to the project and hit create c-sharp script and let's call this one sound women double click it hit reload all and in here we again want to be using unity engine dot audio we also don't want this to derive from mono behavior and we want to delete the two methods the first thing we want is a reference to an audio clip so let's write public audio clip and it's called it clip we also want the volume let's write public slowed volume and the pitch public slowed pitch now we'll be adding some more to this as we go but this is the base of a sound class so if we save this we can now go into the audio manager and in here we create a public sound array and let's call it sounds now when we save that and go into unity and have a look under the audio manager we would expect list to appear however it does not the reason why is that whenever we create a custom class and we want this class to appear in the inspector we have to mark it as serializable so right above the class we make two square brackets and we write system that's serializable now when we save and head into unity we can see that we have a list of sounds let's add an element and the first element here has a clip a volume and a pitch now we can definitely make this more interesting first off let's go ahead and add a name we'll make this a public string name and we can also add sliders to our volume and pitch to do that we use the range attribute where we input a minimum and maximum value we want our volume to go between 0 and 1 and we might want our pitch to go between 0.1 and 3 if we now save and go into unity we can see it looks much better let's name this first element player death and I'm going to drag in my player death sound now in our audio manager whenever we start the game we want to loop through a list and for each sound add an audio source let's do this inside of the awake method awake is pretty much the same as the start method except it's called right before so we set up everything in our wake method so that we can play sounds in the start to look through our sounds we go for each sound and we'll call the sound that we're currently looking at s in our sounds array we want to go ahead and add an audio source component we can do that on our current object or in a child object for the sake of simplicity let's just do it on the current object so we'll go game object dot add component and the component we want to add is the audio source and of course we want to store this audio source in a variable so that later when we want to play the sound we can call the play method on the audio source the simplest way to do this is going to our sound class and add a public audio source and let's call it source of course we don't want this to show up in the inspector because it's a value that we publish automatically in the awake method and so we can mic this with an attribute called hide in inspector so now even though the variable is public it won't show let's say that go into the Audio Manager and now we can set s meaning the sound that we're currently looking at dot source equal to that new audio source component and so we can set s source clip meaning the clip of a audio source equal to a star clip and if we want to control our volume and pitch as well we can set s that source dot volume equal to s that volume and s that source dot pitch equal to s dot pitch so if we save this now and head into unity and hit play we see that at the start of our game and audio source gets added for each sound in our sounds array and that's the clip the volume and the pitch all get copied over let's exit play mode and let's add a weight to play this sound to do that we create a new method let's make this instead of the update method and this one is going to be public because we want to be able to call this method from outside the class let's call it play and it's going to take in a string with the name of our sound then all we have to do is loop through all of our sounds and find the sound with the appropriate name we do that by creating another for each method and checking the name for each element or we could go up here and say using system and this now allows us to find a sound by going array dot find we want to find the sound in the sounds array and we want to find the sound where sound dot name is equal to the name now this type of syntax might be a bit new to you but it allows us to write code hole that faster when then store the sound that we found in a variable called s and finally we can go s dot source dot play of course this is going to throw an error if you don't find a sound with your program name we'll have a look at that in a second so now our audio manager should actually work we have a way of defining the sounds that we want we set up audio sources for each sound when we play the game and we have a method that we can call whenever we want to play a certain sound so now we can find the place in our code where we want our player to die let's go into player and here I have a script called player collision under this on collision enter function I check if what we hid was a meteor and if it was we want to go ahead and in the game and destroy the current game object this is also why we want to play our sound of course we need a reference to audio manager we could create a variable up here and reference it in the inspector or since this is only going to be called a few times we could use find object of type and the type that we want to search for is Audio Manager we then called dot play and then the name of the sound that we want to play in our case that's player death so now with a single line of code that we could put anywhere in any of our scripts we can play a sound of our choosing and so we should hear that when we hit play and if I manage to collide with something I'll play a blow us up and we can quickly expand on this to add some music let's add another element to our array that's called this one the theme and I'm going to drag in my main theme here let's set the volume to something a bit lower and make sure the pitch is set to one and we can play this from anywhere let's just play it from within our audio manager let's add a void start method and here we simply call play and the sound that we want to play is theme so now when we hit play we can hear the music playing of course we want our main theme to loop to add this option we go into our sound class and we add a public bowl called loop then inside of our audio manager where we set up our different sounds we go s dot source that loop equals s that loop and now we have an option for looping let's go ahead and enable that and when we now play we can see that our main theme is set to loop let's see what happens if we make a typo so if we accidentally put a cue at the end of theme this happens more often than you think if we now hit play it's going to try and play a sound that is in there so we get a null reference exception to get around this we simply check if s is equal to null and if it is we return and so we don't try playing a sound that is in there now when we hit play even though the name is still spelled wrong nothing happens and we can even go in here and add a tiny error message for ourselves so I'm just throwing out a warning saying that the sound with this name wasn't found and now we pretty much can't break it let's change the name back here let's have a look at what happens if we add this to multiple seems easiest way to do this is make a prefab out of the audio manager then open up the other scene in our case theme menu and drag the audio manager back in the problem with this however is that if we hit play and transition to another scene our music gets cut off and we don't want our music to restart every time you change the scene we want our sound to be unaffected to change this we make the audio manager persist between scenes let's go into visual studio let's go to the top under our wake method let's add some lines and all we need to do is call the don't destroy onload method and as the target we put our on game object now this also means that if we currently hit play and transition to a new scene we actually have two other managers the one from the first thing and the one from the second now that's of course not something we want so to get around this we create a public static audio manager and we'll call this instance this is a static reference to the current instance of our audio manager that we have in our team and we want to make sure that there's only one instance so we simply go in here and check if instance is equal to null meaning that we don't have an audio manager in our scene well then we set instance equal to this object if not meaning that we already have an instance in our scene we want to remove this object destroy game object and just to make sure that no more code is called before we destroy the object we also hit return now depending on whether or not you've worked with singleton patents before that might be a bit confusing to you but just know that now when we have play our audio manager is set under the don't destroy no scene with a play and there's still only one audio manager and you can hear that our music simply continues that's it for audio managers [Music] that's pretty much it for this video I hope you enjoyed it if you did make sure to subscribe so dome it's a future one so thanks for watching and I will see you in the next video thanks to all the awesome patreon supporters who donated in April and a special thanks to Derek huge jerk face tamara phi james callaghan Sybok Nami Coca brow and Jason Lotito if you want to become a picture of yourself you can do so a pinch ENCOM slash correctly
Info
Channel: Brackeys
Views: 738,962
Rating: undefined out of 5
Keywords: brackeys, unity, unity3d, asset, assets, beginner, easy, how, to, howto, learn, course, series, tutorial, tutorials, fix, tip, game, development, develop, games, programming, coding, basic, basics, C#, audio, audio manger, manager, sound, sounds, effect, sound effect, effects, music
Id: 6OT43pvUyfY
Channel Id: undefined
Length: 14min 26sec (866 seconds)
Published: Wed May 31 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.