How to Add Sound, Music, and 3D Audio for 2D Unity 2022 Games

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody chris here and in this video i'm going to be showing off you how to set up sound effects and music inside a unity 2022 so for a 2d game like this i'll also be showing you how to do positional audio where when the player moves further or closer to an object which is playing sound effects or music then it will become louder or quieter depending on that and also if you have multiple channel audio then the position of the audio will be reflected in your audio playback as well so if you were using a headset and you were on the right then it would play on the right ear or if you were on the left then it would play on the left ear so let's get started with just setting up a simple audio source so you can either do this as a component attached to any object in your scene or you can create a separate object in your scene by right-clicking on your scene hierarchy going down to audio and then choosing audio source so you'll notice right after you create this game object that it has an audio source attached to it so this is the component which actually plays the audio and in order to play audio you need to set an audio clip here so it doesn't matter if it's a sound effect or a music it's still going to be set as an audio clip inside of audio source and then you would just tell that to play you have the option of play on awake which is potentially helpful for music because that can mean as soon as the scene loads it's just going to immediately play that and you can also check loop if you want the same music track to play over and over again with sound effects you might not want it to do play on awake and you may not want it to loop so you can just tell it to play the audio clip in code which i'll show you a little bit later on so for audio clip here if you have a music track or a sound effect in your project you should be able to go over here and select it and find it in your project so selecting an audio clip any compatible mp3 or ogg vorbis file you can just load that in there and with play on awake and loop we should be able to just play this music as soon as the scene loads so now we can go ahead and hit play and we should be able to hear the music regardless of where i move around on the screen it's still going to be playing the music as is because it's set up as 2d audio so that is handy if you just want global music that's going to be playing in the background all the time and you don't care about the positioning with respect to where your player is so if you do want positional audio though what you'll need to do is set up these 3d sound settings and spatial blend settings so for right now i'm going to disable this audio source and let's actually attach a audio source component to this barrel over here so i'm going to click on the barrel add component search for audio source and you're going to see the same settings as the separate game object so just like before you would select a audio clip so i only have one in the project so we're just going to be selecting that music and then you need to turn the spatial blend from 2d all the way over to 3d so what this is going to mean is that the position of your audio listener is going to matter when you're talking about what you hear in terms of audio playback so with spatial blend set to 1 then the position of our audio listener is going to actually matter now in terms of how loud the volume will be and on which audio channels it's going to play so you might be wondering where is the audio listener well by default it would be on your camera and you can only have one in your scene so if you click on the camera the audio listener here we're actually going to want to remove that and add it to the player because we're trying to listen as if we are the player character on the screen so the position of the player is most relevant not necessarily the camera so i'm going to remove the audio listener component here and we're going to click on our player object i'll actually click to jump into the prefab to add the sends so that whenever i create a copy of the player it's there in the game and then type in listener here for add component and choose the audio listener so the audio listener has no settings it just needs to be there and then that will be the position at which you hear the audio playback so now we need to deal with the 3d settings click on the barrel and we already set the spatial blend to 3d so that's going to make all this stuff down here under 3d sound settings relevant so doppler level is going to change the pitch depending on if our player is moving away or close to the audio source in this case since we're playing music i want this set to zero because i just want the music to play back as it was originally intended if you are trying to play back sound effects like a sword swing then you might try experimenting with the doppler level but if you want it off just set it to zero and then it won't mess with the pitch and then we need to set the min distance and the max distance so you can see these thick gray lines are the units for this game and you can see about eight of these grid squares fit into the actual unit grid uh because the pixel art here is working on a 16 by 16 grid so the max distance being set to 500 is actually enormous if you think about it it would be so many units way off the screen here so i need to change the max distance and set it to something like 2 and you'll see that the outer ring for the max distance appears here so this would be the max distance at which you'd be able to hear the audio playing back from the barrel so 2 as a number here just makes a lot more sense for this game but that depends entirely on the scale of your art so for testing purposes set it to a value which you could actually test getting to the outer rim here and then the min distance is the distance where the audio starts to fade out so if i move this closer to the barrel then that means and it will fade to a volume of looks like about 0.2 when it gets to this max distance so for simplicity you can actually change the volume roll-off from logarithmic to linear because with linear roll-off it's just going to be a consistent decrease in audio volume until it gets to the end point with logarithmic you can see that it has a curve here so it's going to be very fast at the start right after you get to this min distance and then when you get to the end it's not going to be losing too much more volume so just easier to understand with linear roll off i think for just testing purposes but the way you set this up is up to you so with these settings doppler set to zero we have our max distance set to something more reasonable for the scale of our game and spatial blend set to 1 then now we can get the 3d effect as we move around the screen so with the headset i'm wearing right now i'll be able to hear the audio on the left and right depending on if the player character is to the left or the right of the box and you can actually hear a difference when it is above the object or below it as well i don't know if that will come back in the video but let's just go ahead and test it so i'm going to hit play let's go over to the simulator tab and i'm going to get outside of the audio falloff range so we can actually see the audio listener moving because it's attached to our player and as we get close to this end it's just going to get quieter and quieter until it's actually not playing back at all so this is the cutoff point you can't hear anything but if i get right back inside the music comes back so that's basically how you control things with the min distance and max distance let's get all the way over here so quite loud at this point and if we go above the barrel and to the top left of the barrel so looking at obs i can see that the audio is outputting to the left and right channels depending on the audio listeners relation to the barrel which is what we're looking for [Music] and so that's pretty much the settings for 3d audio in a nutshell even applied to a 2d game it's just important for the audio listener to be on the player in this regard so now we should talk about when you want audio to play but not at the start of the scene play on awake but actually in code you would have some event trigger and then you would do audio source dot play so i'm also going to take the audio clip here and set it to none because with one audio source you can actually change the audio clip in and out so you might not want to create 50 audio sources in your scene for 50 different sound effects you might play in some cases you might have different audio clips play from the same audio source for instance each npc or object might only have one audio source but there might be different sound effects that you play from that same audio source for instance different variations of an object destruction sound so in order to script out the audio source we can add component so i wrote something a little earlier just a script for the barrel so this barrel script is going to automatically find the audio source attached to this game object the barrel and we can set up an audio clip here in order for the audio source to play but we're doing this after the script loads so if we go ahead and edit the script here we can see it's pretty straightforward so we get a audio source and an audio clip the audio source we find that on the game object when the script starts and the clip on the audio source that's this field right here audio clip we can set that in code and the reason this is important is that means that when the game's actually going you can change the audio clip in and out all you need to do is audio source.clip equals to the new clip that you want to play and then you tell the audio source to play the current clip so this is effectively going to do the same thing as having audio clip here setup and then play on awake because this is written in the start function but all you would need to do to change the clip out and play a sound effect at any point in time would be to have these two different lines written in a different function um that you play at some given event in your code so let's go ahead and set up the audio clip here i'm going to select the music again audio source once again that's going to be acquired after the scene loads and then the audio source is going to be told to play the music so we can have loop here checked uh but i'm going to leave play on awake checked so without play on awake checked it would be required that we'd have to tell it to play in code so let's go ahead and test it i'll hit play you'll see that the music plays and when the game loads the audio clip is set here so if i turn it off you'll see it goes back to none so this was set up in code play on awakes not set to true so the time when it's actually told to play is this line right here so everything i just told you right there is going to be really relevant when you're talking about sound effects so when a character gets hit then you would have a on hit function and you would just do audio clip equals the clip you want to play and then audio source stop play and you can play that clip if uh if you're only ever going to use the audio source for one clip then you don't need to actually change it every time you could just leave it defaulted as whatever is an audio clip for your audio source as well so in a nutshell that is pretty much everything you need to know about how to play audio and music inside of unity 2022 and how to set up positional audio for even a 2d game like this so thanks for watching to the end i've been chris and i will see all of you in my future video content
Info
Channel: Chris' Tutorials
Views: 13,531
Rating: undefined out of 5
Keywords: unity, unity tutorial, unity sound, unity sound effects, unity sound tutorial, unity sound play, unity audio source, game development, game design, game dev, unity 2d, indie game development, indie dev, unity sound setup, unity audio, unity how to add music, unity how to add sound effects, unity 3d sound, unity sound manager, unity add music, unity music tutorial, how to add music in unity, add music to unity, how to add music to unity, unity 2022, unity 2023, 2022
Id: -euVKBFaq8U
Channel Id: undefined
Length: 11min 54sec (714 seconds)
Published: Sun Jun 19 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.