Simple Sound Manager (Unity Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome I'm your code machine in this video we're going to make a simple sound manager that can help playing a multitude of sounds - at your game let's begin so here's what we want to create I have a player character and I can move them around as you can hear there are footsteps when he moves I can use my sword and there's a sound I can attack and kill enemies and they also have sound when hit and killed and over here I have a nice great and if I approach it there's a very nice item sound here we also have a simple button in which we're going to add mouse over and mouse click sounds so I pass over and I click and those are all our sounds so let's figure out how to create a class to handle playing all these sounds alright let's get to it so here is that same scene with no sound I have my player moving attack the enemies and here we got the treasure chest all right over here in the project files I have all the sounds in this folder these sound effects were created using chip tone which is a great free tool to create very simple sounds so let's start off by creating class to help us play sounds so in here make a nice sharp script this will be our sound manager so this class will be responsible for playing all of our sounds let's first make it static so we don't accidentally in sin she ate it and remove monobehaviour alright so first let's make a very simple function to play a single sound so we make a public static void play sound and in here let's create a game object then we're going to add the component for the audio source which is the component that actually plays our audio and in that component we can call the function play one-shot which plays a single audio clip so in here we need a reference for our audio clip so for that let's add it to our game assets class this game assets class is something very useful that I use in pretty much all of my projects I've done a whole video on it so you can check it out see more in that view but essentially we have a game object on the scene and it contains this script then here we have a static instance and then this class has a bunch of public fields so in here for general I can add a public audio clip for the player attack and now I can go into the editor and here is the game object that contains the game asset script and now in here I can just drag the player attack reference so now I can go back into the sound manager and here I can now grab the reference by going into game assets that instance dot grab the player attack and that's pretty much it for playing a very similar sound so now we need to trigger this somewhere so let's go into the character sword handler which is my player script this is the class that is handling movement and attacks for the player so let's go down here to where we are handling our attack again the entire code of this is not relevant to our sound manager we just need to figure out where we're going to call the function so in here we have the Hemel attack and in here we are playing that attack so it's in here that I go into the sound manager and I thought to play a sound okay so that should do it let's test okay here's my player now if I attack yep there's the attack sound being played whenever I attempt okay so we can successfully play a simple town now let's change our code to be able to play different sounds so let's go into our sound manager and in here let's create any new to keep track of own for a possible sounds so we do wait for like a new let's call sound and in here we have all of our possible values then on the play some function we receive a sound value and now we need to know the audio clip that matches this sound so for that let's first go into the game assets to figure out how we're going to store our references and in here we're going to create a class to handle both an audio clip and a sound so we make a public class and let's just call it sound audio clip we need to set the class to serializable in order to make it show up in the editor and in here we have a public sound manager that sound and a public audio clip for our audio clip alright so we now have a nice class that handles both pieces of data so instead of having a bunch of fields in here we simply have a ray of Sun audio clip alright so now we can go into the editor to fill up our array here's our game as the script as you can see it contains an array of sound audio clip and in here we can add elements and as you can see each element has a sound and an audio clip so let's film them up alright we have our array being filmed with all of our references so now all we need is to find the correct audio clip that matches the sound that we want so let's go into the sound manager and in here let's make a function to do just that so here we can cycle through the game assets the sound audio clip awright we cycle through the array and we see if this sound your audio clip that sound equals the sound that we're looking for then we return the sound audio clip dot audio clip and after the for each we simply return null however it should never really reach this point so let's do a debug log error and just like that we have our very nice simple function so now up here instead of using this we use our function we go get audio clip and pass in our sound okay that's pretty much it so now we can go back into the player script and in here we play the sound and this is the attack code so we play the sound player attack okay now let's also go into the enemy handler this is the class that handles the enemy so let's see where the enemy is taking damage here is the function where the enemy takes damage and then here we can simply do sound manager and we play the sound and in this case is the enemy hit and then down here we have what happens when the enemy dies so we play the enemy dead alright so all this should be working and we should now have three different sounds being point let's see here's the player and there's the enemy attack sound and now on the enemy and yet there it is the sound when the enemy gets it and when he does so all of our sounds are correctly playing exactly as intended so with all these sounds currently working let's make the player move sound let's start off by doing it in the most obvious way so in here let's go on the handle movement function and if we are not idle which means we are moving then we want to play the sound for the player move so let's see what happens if we do just this so here I am and if I move and yep that's a pre horrible sound so it's certainly not what we want so let's see why that's happening now the reason is pre obvious which is because we're calling this function on every single frame so essentially we're playing the move sound on every single frame which were a simple game like this means we're hearing the sound of thousand times per second obviously that's not what we want so let's add some sort of timer to only play the sound every certain amount of time so let's go into the sound manager and in here and let's create a dictionary which won't contain our timers now a dictionary holds that based on a key and a value so it's perfect for storing timers since we can use our key as our sound and for the value we use a float so now we have our play function and before we actually play the sound let's figure out if we should play that sound so let's make a function to answer that question now in here we do a switch on our sound and by default we're going to return true so we only need to handle the cases that are different so in this case all of our other sounds are meant to be played exactly as intended however we do a case for the player move that one is not supposed to be played every time so let's do this first we test if the dictionary has that key and if it does contain that key we test for the time that is inside that key essentially the flute value that won't be stored is the last time that the sound was played and in here we define how often we want to sound play so let's define a float so let's say we want the sound to play at most every 50 milliseconds then we test if the value stored in the key pause this timer is under the current time so this is the current time this is the last time it was played and this is the delay we want between each time so if don't last time plus delay is under the current time then we can play again and if not then we return false and in here before we return true we need to update what's on our dictionary so we go into the dictionary for our sound and we update it with our current time okay so again we have this function which returns if a sound can be played now for most sounds by default it will return true since for most sounds we want them to play every time they're called however for this particular sound we don't want to play all the time so we have this code in here we check the dictionary if it contains a key for that sound we get the last time played which is what is stored on that dictionary we define a delay between each time we want the sound to play and then we simply test if it's time to play if so we return true if not we'll return false so now we need this to go up here on to our play sound function and here we're simply going to play only after checking if we can play the last thing we need is simply to initialize this variable so let's make a public static void for the initialize function which won't initialize this and let's set the key for the sound dot player move let's set that one to 0 and I'm going to simply call it on the basic game have more function all right so let's test okay here I am now if I move yep there you go the move is no longer playing on exactly every single frame but every 50 milliseconds and all the other sounds still work exactly as intended now we just need to play the treasure sound so let's do that let's just go into the treasure crate script this extremely simple it simply has a Collider so in here we open treasure so it's in here that we simply go into the sound manager and we tell it to you play the sound and we pick the treasure sound and it's that simple to add a new sound so again the move sound the attack and the enemy kill and if I go into the crate yep there you go there's a very nice treasure sound all right so all of our sounds are currently playing however as you might notice they are not being played in 3d space so there's currently no difference between don't left and right speaker so let's make them as 3d sounds here in the sum manager let's add a new function that receives a sound and also a position and in here we're mostly going to do the same that we're doing in this function however we need to position the game object so we do game object our transform and we set a position to our sound position and instead of being point one shot we call play the play doesn't take any parameter so instead of putting the audio clip on the point one shot we put it on the audio source clip and the audio source also contains a bunch of different fields which define how the sound is played and how it acts like for example in here you have the top level but for now let's just write default and see how it sounds so now we need to call this function so let's go on to the character for the player move we call that function and then the player position and it's that simple so let's see where else we have calls to our cell manager so in here do the same thing come with the get position and same thing on the enemy and finally on the treasure no let's just leave it as a 2d sound alright so let's see if we can tell the difference between the left and right speaker okay so here I am and the camera is following the mouse position so I can put the mouse on the left side and we should be able to see the sounds more on the right side and yep there it is the move sound on the right side now put the camera like this and now the sounds are on the website and if as you can see we got some nice 3d sound and if we pause the game we can see these sound game objects that are being created and in here you can see the various options that you can tweak to see how your sound is played you can change for example if volume removed from logarithmic to linear domain in maximum distance this is the most important one with this patient point which needs to be in 3d in order to actually see the 3d position and then you can also mess around with volume pitch and so on these are the settings I put to achieve that result alright so as you can see we have all of our sounds correctly working now we want to add some button sounds so over here I have a random testing button it's using the button UI script which is part of the code monkey Tony's that as always you can grab for free from unity code monkey comm this class and owns a bunch of things related to button so I can use it to easily make this UI element into a button here is the button and as you can see it changes color when I pass the mouse over and I can click on it all right now in order to add sound to our button we're going to use something really cool a extension method so let's go into the sound manager and in here we're going to make a function to add button sounds and in here we're going to put the parameter as this button UI for the button you are the dis keyword means that this is an extension method we're essentially adding the Add button sounds method to the button UI class without actually modifying the code inside that class this is great for adding functionality to a previously written class so in this case makes perfect sense to add sounds based on this sound manager to another class which does not feature sounds so we can go into the window code and here we get the reference for the button component and we can simply grab that reference then we call the function add button sounds and as you can see this is an extension method that lives on the sound manager but it's applicable when we use on a button so it seems like we're calling a function on the button UI class but really we're calling a function on the sound manager again extension methods are really cool and perfect for this type of use so now on the sound manager we can now add our sounds into this button so let's first go up here and add the enum values for our sounds and down here we go into the button and we access the functions for clicking and we simply do our cell manager and we play the sound for the sound dot button in this case the button click and just like that we're adding sounds into a button so let's test there's the button and when I pass the mouse yep there's the mouse over sound and if I click and there's the click sound so as you can see this is a great way to add sounds to a component different class without actually modifying that class now let's just clean up some things in our code let's go into the play sound function and in here we are creating a new game object every time we play a sound however we don't need to do that since for play one shot we can reuse the same game object which will obviously improve performance so let's go up here to make a field and on the play sound we check if the one-shot game object if it is known and if it is we're going to create it exactly as we did in here and just like that we're only going to create this game object exactly once and every other time the game object won't be reused and up here we're playing a sound at a certain position we can't just use one game object since we need to position it however we can destroy it after the sound is finished playing so we can simply do a destroy we want to destroy this game object and here we can choose the time to destroy so we can simply go into the audio clip and we get the in length of that article so as soon as it finishes the game object gets destroyed so let's see if everything still works exactly the same Here I am my kisumu sounds are still playing it's still attack and kill the enemies there's the test button and there's the trophy button however if I pause the game we can go in here into our our key and as you can see the only game object we have is the one-shot sound so we can resume and hear a look at the hierarchy and there you go as you can see the sound is played and it immediately vanishes as soon as the sound is finished and in here if you wanted to make this even more performant you could use a object pool to reuse these sound objects instead of constantly creating and destroying them so there have it we created a simple sound manager class that is responsible for playing all sounds in our game we can play a sound normally or position it in 3d all the code is easy to modify with each sound being a value in an animal that could be expanded to contain any sounds you might need as always you can download the project files and utilities from ent code monkey comm if you have any questions post them in the comments and I'll do my best to answer them subscribe for more videos and I'll see you next time you
Info
Channel: Code Monkey
Views: 122,537
Rating: undefined out of 5
Keywords: unity audio play, unity sound manager, unity how to play sound, unity sound effects, unity sound, unity sound effects manager, unity audio, unity sound trigger, brackeys, unity tutorial, unity game tutorial, unity tutorial for beginners, unity 2d tutorial, unity 3d, unity 3d tutorials, unity tutorial 2d, unity game, beginner, easy, unity2d, unity3d, unity, basics, basic, game dev, code monkey, how to, learn, tutorial, course, game development, to, how, series, tutorials, unity 2019
Id: QL29aTa7J5Q
Channel Id: undefined
Length: 19min 27sec (1167 seconds)
Published: Sun Mar 10 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.