Gun Sound Effects with an Audio Config ScriptableObject | Gun Series 5 | Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
audio feedback is something that adds a lot of immersion into your game and a lot of Indie Games end up kind of skimping out on this in this video we're going to add audio to our guns so we play gunshots whenever we shoot play different clips whenever we have no ammo and even have a different sound whenever we shoot the last bullet we'll quickly also add in playing audio effects with our impact system to make sure that the bullets play sound whenever they hit an object hey Chris here from Mom Academy here to help you who me yes you make your game Dead dreams become Reality by helping you add a layer of immersion to your game with sound effects since this is part 5 of the gun series you're probably not going to be surprised by what we're going to implement here we're going to implement a new audio config scriptable object where we will Define different firing Clips a clip for when we're out of ammo a clip when we play the last bullet and of course a reloading clip all that will just hook up into the gun scriptable object make a couple of tweaks to how the gun shoots because we need to account for trying to shoot whenever we don't have ammo update so you can play the reload sound and hook it all back up into the inspector I am going to be demonstrating this with an asset that I have from the asset store epic or sound effects that link in the description if you're interested in this one it's pretty good in some cases I wish there were a little bit more variety of things but these are the sound effects I'm using in lava survival and I'm pretty happy with them in the repository we're gonna have some less variety of sounds that I got from open game art that way you can have all this working just by checking out the repository remember that the full project is always on GitHub Link in the description I don't think there's a lot to say here this one's pretty straightforward let's jump in and start implementing this let's jump right into that audio config scriptable object that extends our scriptable object and add the create asset menu file name audio config menu name guns slash audio config and I'll pick the order five we've done this a bunch of times before so that should be pretty familiar on the audio config I can think of a few things that we'd want to consider one would be how loud we want the clips to play so we'll put public float volume and set that to 1f by default we can even attach a range from zero to one because that's what the audio Source expects a public audio clip array called buyer Clips will choose from these which audio clip to play whenever we're shooting and you could stop there but something that maybe makes it a little bit more engaging would be if we have a public audio video clip empty clip which would be the clip to play whenever we are out of bullets and the player tries to shoot a public audio clip reload clip which will play whenever the player clicks reload and finally a public audio clip last bullet clip this one may be less popular but I've noticed in games like Battlefield 5 whenever you shoot the last bullet in your clip and plays a distinctly different sound to inform you hey you're out of ammo before you start you know keep spraying and getting the empty clip well to find some public methods to handle when we should play each of these clips so that way it's all encapsulated within this class with the public void play shooting clip that accepts the audio Source we want to play from and a Bool is last bullet that will set the false by default we'll check if it's the last bullet and the last bullet clip is not null then we'll play one shot that last bullet clip with this volume in any other case we're just going to play One Shot again we're going to pick a random clip from the fire Clips with fire Clips indexed by random range zero to fireclips dot length and again passing the volume as the last argument then we'll Define the public void play out of ammo clip which accepts the audio Source again [Music] we'll check if the empty clip is not in all we'll play that again with Play One Shot empty clip and volume of course the last one would just be play reload clip same thing here just reload clip is not null play that clip from the audio config let's Implement how do we use the audio config in the gun scriptural object up here at the top where we have all the other scriptable objects we'll just add the public audio config script of object audio config and because we have to have an audio source to play from we're going to get that from the instantiated model I'll Define a private audio Source shooting audio source and exactly as we did before we're going to get the audio Source from the instantiated model I'm going to get it from the root level because I would expect that the audio Source we have it on the top level for whatever we're going to shoot with you can use git component in children if you didn't want to structure your prefab that way if we open up the shoot function where we're actually doing the shooting right here wherever we're saying hey we're actually going to do the shooting we're going to just play that firing audio clip we'll pass the Islas bullet with ammo config.current ammo equals one before we subtract the bullet from the current ammo clip that will cover the vast majority of situations where we're trying to actually play a clip but remember we had a couple of other cases one is what happens if you try to shoot and you don't have ammo and the other one is reloading let's talk about reloading next I think that's the simpler one in the player action where we're handling the user input we have this check on update 8 should manually reload or should Auto reload we then start trying to reload and most of the reloading is handled by the animation events because so far we've only cared whenever it's done but now we want to play an audio clip as soon as the user starts to reload so it'd be very convenient if from the gun scriptable object I could say something like start reloading go back to the unscriptable object and in here we'll just proxy the call to audio config dot play reload clip that's in the shooting audio source so that should cover our reloading case perfectly the last thing is how do we play a sound whenever the user is out of ammo remember that tick will only call shoot if player wants to shoot and we have some ammo so if they want to shoot and they do not have any ammo our initial Instinct might be we can simply play the out of ammo audio clip but we need to consider that there's a delay before we can actually try to shoot so I'm going to actually rename this function shoot to try to shoot and then in here only if we're actually going to try to shoot which is the second if what we're going to do is say last shoot time equals time.time so we still keep that delay between each attempted shot then we're going to check ammo config clip ammo is zero meaning we're completely out of ammo we're going to play that out of ammo clip and then returns we don't do the rest of whatever this is going to try to do all right we're still getting the delay as expected but we will not play the shooting system we won't play the shooting clip we won't subtract ammo all that stuff back in tick we need to make sure we just call try to shoot every frame because we've now guarded against doing the majority of the shooting in tried to shoot if the time has elapsed where it can shoot let's go back to the unity editor hook all this up create these audio config crypto objects now as usual in the guns folder I will create a new folder called audio and in here I will create two audio config scriptable objects one for the clock and one for the M4 and I'm going to put in some maybe better sounding audio clips that I personally am currently using in llama survival I can't include those in the repository but I have included some examples so whenever you pull this from the repository there will be some clips but they won't be the exact same ones you're about to hear and for the reload I'll use this gun reload audio which came from open game art so that one's included in the repository just for fun so we can hear it I'm going to put the last bullet clip as a Glock bullet I didn't have a good audio clip for that so we'll just hear something different on the Glock will do a very similar process and we have three variations for the fire Clips so we're going to use those for the empty we'll pick revolver dry fire put reload for that and again for the last bullet clip I know we'll pick a different sound one thing I will say is the volume of one's probably too loud so I'm going to cut that in half make sure to link those back up on the main unscriptable object [Music] and finally let's make sure that there is an audio Source on both the pistol and the M4 I'll just disable play on awake leave most of everything else alone except remove Doppler because that sounds a little bit funny sometimes foreign off auto reload so that way we can hear that empty clip instead of just as soon as we empty the clip immediately reload one other thing I'm going to quickly set up the impact system to play an audio clip on hit the full video for the impact system is going to be linked in the description card on the screen so I'm not going to go super deep in depth on this because I already covered it in a separate video but I think since we're talking about audio it's important to also talk about how are we going to play audio effects so I'll just create an empty game object called impact audio source and attach the exact same audio Source we just made to it we're going to make that into a prefab then in our impact effects folder I'm going to create a new impact Effect called play audio effect and I'm going to do this where we just have one impact sound you can customize this per impact type meaning for each gun you could have a different impact sound for each material that's hit could have a different impact sound for today we're just going to make them all play the same thing with the audio Source brief app we're going to drag that audio Source we just created and for audio clips these are coming from the Epic War sound effects collection and I'm just going to select a bunch of bullets impact dirt ones as the possible options for the volume range I think these should probably be less loud than the actual gun shooting so I'm going to do like 25 volume and we'll just leave that alone and see how it sounds then on each impact effect we're just going to add a play audio effect and use that one we just created so for each surface type we're going to make sure that place the particle system and plays an impact sound thank you for doing the shooting clip for hearing the impact by clicking load it'll play the clip it's not synchronized to our animation so we might want to speed up the animation or have a longer reload clip but mostly that seems like it's working okay there's one last problem to consider here remember that this M4 and maybe some other high rate of fire guns that you have are shooting a bunch of bullets and we're also going to play an audio sound effect listen to this if I just sprayed the entire clip a lot of the sound effects just didn't play let me do it again one more time listen closely pretty weird right everything seems to be working we're playing all audio clips it's just we're not hearing them we open up the project settings audio there is a kind of hidden configuration as you may have not heard of before something called the max real voices this limits how many simultaneous sounds can be played can be picked up by an audio listener Max virtual voices allows you to have more than the real Voices playing and once a real voice becomes available because it's completed it will pick up on the virtual voice the next one in the queue so because we have a very high rate of fire and we're playing a bunch of impact sounds all at the same time we're exceeding this limit of 32 Max real voices if I update this to 64. I'll link you to the documentation on this I haven't seen in the documentation where they describe how many voices are Max total per platform so you will definitely want to test this on your target platform to see is the max real voices a reasonable number for your Target platform on my computer 64 plays fine so I'm going to use that it's also important to note that that the more real voices you have actively playing the higher CPU usage will be dedicated to audio and now listen to the difference [Music] you heard every single bullet and impact effect [Music] so if you're running into this case where you're very sure you're playing the audio and it's just it's not playing check out if your max real voices needs to be increased if that solves your problem I hope that's pretty straightforward there wasn't a lot new here it's just reasoning the same Concepts we've been using this whole time with the scriptable objects both configuration and hooking it up together in the unscriptable object again the full projects on GitHub Link in the description all of my projects are always on GitHub you can always check out the code after you followed along in the video remember this is part 5 of the gun Series so if you were a little bit lost you can go back to part one watch the whole thing and see how we've implemented all of this from scratch if you want to stay up to date when the next videos come out make sure you've liked and subscribed for those new videos posted every tutorial Tuesday and if you want to support this channel you can go to patreon.comacademy get your name up here on the screen get a voice shout out starting at the awesome tier speaking of those awesome supporters there's Gerald Anderson Autumn K Matt Parkin Ivan rulin and Paul Berry and that's a tremendous tier there's Bruno gosich and at the phenomenal tier there's Andrew Bowen and Andrew Albright thank you all for your support I am incredibly grateful
Info
Channel: LlamAcademy
Views: 6,900
Rating: undefined out of 5
Keywords: Unity, Tutorial, How to, How to unity, unity how to, llamacademy, llama academy, gun, gun series, gun audio, shoot sound, shoot, sound, sound effect, sfx, sound effects, gun sound effects, gun shooting sounds, scriptable object, scriptableobject, audio configuration, unity gun sound effect, configure gun sounds, series, gun tutorial, guns tutorial, gun tutorials, unity gun tutorial, unity gun tutorials, unity guns tutorial
Id: hV3BAw2c9Io
Channel Id: undefined
Length: 12min 43sec (763 seconds)
Published: Tue Jan 03 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.