Alright, in this lesson we're going to add some audio to our game. Sound effects, voice overs, that sort of thing, to give the game a greater sense of realism and immersion. Adding audio is actually very easy and intuitive in Unity. Let's get right to it by creating our audio folders, and then import some audio clips I created ahead of time.Let's create an Audio folder here in the Assets pane. And in that folder we'll create one for each: FX, voice overs, and we're also going to have some music loops as well. We'll worry about music in the next video I think. Right now let's just import in the FX folder, the FX assets. These ones, AirHorn, CrowdCheer, CrowdNoise and Teleport. Do it one by one. For Voice, import these; ReadyFight, TaborDuken, TaborUppercut… Some of you out there may know what I'm referring to with these. If not when you hear them maybe you'll know what I'm getting at. Alright. First what I'll do is I'll show you easiest way to get audio playing in Unity.
Let's create a new GameObject under Main Camera. Call it Announcer, and on that we need an Audio Listener component, sorry, an Audio Source component. It also needs an Audio Listener. We'll talk about that in a second. For the Audio Source, we just need to stick an audio clip on to this part. Go to Voice and take the ReadyFight clip and put it there, and simply put Play On Awake checked as true. This will play right when the game starts. I'll play this right now and that clip will play. Alright, so that was mainly just to show you how you can add audio to a GameObject without even needing to access it in code or any special access to an audio mixer, both of which we'll actually touch upon. But for right now, we'll just go through the Audio Source, and Audio Listener also has to be activated. There's one by default on the Main Camera. It doesn't matter if it's on the same GameObject as your audio clips or not. You just need an Audio Listener and the Audio Source in order to have a clip and play it. Play On Awake is pretty much your only option there if you don't want to access it through code.
Now let's just add a bunch of audio to the Main Camera directly. We're going to want to add first the CrowdNoise. Actually let's do it this way, add an Audio Source and we'll put the CrowdNoise clip there, and it will Play On Awake and pretty much loop throughout the duration of the game. It's just going to be some background noise. Add another Audio Source and this will be the AirHorn, which will Play On Awake as well, just to signal the start of the game. I'll play it just so you can hear those elements. That's just some basic use of audio clips on GameObjects without having to access them in code. What we're going to do now is put some audio clips on the Cube GameObject and then look at how we can reference it in code. For here we'll add an Audio Source, and we'll drag the Teleport effect clip. This will not be playing on awake because, again, we'll be controlling it in code. In the CubeController script, we'll actually write the code to reference this. This is going to be a teleport effect so were do you think it logically probably belongs? We want it to play pretty much when we engage teleport so it'd be here right? We'll it'd be really easy to reference the audio clip. We just write GetComponent() and we're going to want the AudioSource Component. After we grab that, we can just, with the dot accessor, use the Play() method to play it. That's all that's needed. I'll just engage the teleport so you can hear that. Alright.
Now in the Sphere GameObject, we'll also want to do something similar, but with a different effect. You want to add a Audio Source,and we'll take the CrowdCheer FX clip. Again, it's not going to be Play On Awake. We'll access it in script. In the SphereController… what we're going to do here is actually going to be kind of interesting. We're going to take that distance value that's being calculated whenever we're close to the enemy, and we're going to play the CrowdCheer clip. You can imagine when we have a lot of enemies around us, that will multiply how many clips get played and it’ll result in even louder cheers. With very little code we can get a very interesting effect. We'll start by actually, we're going to want two methods now to access the distance value. What we'll do is we'll make this a field again. A capital D for naming convention's sake, and then store that here as a Vector2 Distance. We'll also need an AudioSource, call it Audio, and in Start() we'll grab that AudioSource, just to show you you can do it this way too. We did differently in the other script, but we'll do it this way here. Now to play the CrowdNoise when we're close to the enemy, let's make a new method.
Actually first, we're going to need a new field to be set. We're going to need a CoolDownCounter, as we used previously to make that the effect doesn't get out of hand and doesn't keep layering on top of itself and getting too loud. Here we'll say int CoolCrowd, and we will initialize that here in Start() to 220. Here we'll remember to call the CrowdCheer() method that we're going to create. Make it right here. Private void CrowdCheer() So we'll start with local Int maxCoolTime. Now what we'll want to do is calculate how close we want to be to the enemy before we play that clip. We'll want it to be a little bit greater distance than this because this will result in the end of the game, but we can still take this code pretty much. Kind of went parentheses crazy here didn't I? Just replicate what we did there, and yeah. To make it a little more lenient let's say within .9 units of distance, and we'll engage that crowd cheering effect. We'll only engage it if the cool down counter for the CoolCrowd has cooled down all the way and reset. Let's say if (CoolCrowd == maxCoolTime) We will reference that audio clip, that field, Audio.
Actually, let's pan it a little bit just to give it a little bit more of a coursing effect. Sort of as we did for the Random.Range() before, we'll use minus values for the left channel and positive values for the right channel. We'll sort of get a random value between those two values. So Random.Range(0.6f) so 1 would represent all the way to the right channel, -1 would be all the way to the left channel. This would be just a little over halfway to the right channel. We modify it with, or multiply by either 1 or -1 as we did before. We'll say Audio.Play() that Play() method. Here we'll pass in, let's see, what does IntelliSense tell us? We can pass in an ulong. What's a ulong? Basically ulong is like a really large Int. I can't remember exactly the values, but it's larger than whatever the largest Int value is. It calls for a ulong so let's set it here. What this will do is it actually will delay it, so you'll get a little delay in the clip. You do want to delay it to give it a little bit of a coursing effect, otherwise if all the spheres are playing the clip at the exact same time, it'll not only amplify it, but give a weird sort of phasing or canceling audio effect. We don't want that. Let's take a Random.Range() between, so the delay effect will be anywhere from 1 to 11,000. Because it requires a ulong, we will cast that to ulong. Then we will start the cool down counter for the CoolCrowd.
Then, here we'll say if (CoolCrowd < maxCoolTime) so if it's less than 220… We count down on every frame until we reach 0 and then we'll reset it. For that, let's add another conditional. Now we're going conditional crazy. Oh well, at least it'll get the job done. If CoolCrowd is less than 0 we'll just want to make sure it resets to 220. There are better ways of doing this but this is just sort of using the tool set that we're aware of right now. I'll just put a comment up here where I say “If close to Cube, crowd goes wild.” Let's play this and try to play for a while. Oh, we got an error. Where is the error? Ah, forgot the parentheses there. Alright. Should work now. So yeah, let's try to play for a little while without getting eaten, so we can kind of hear the multiplying effect. Alright so now, just for fun, let's add some random sound effects whenever we collect a PowerUp. We added these earlier, so we're going to want to add them now as Audio Sources to the PowerUpSpawn.
First, let's add TaborDuken. Actually it doesn't matter the order because these are going to be played randomly. Then we add TaborUppercut. Sure, why not? Then we add the WhoahTabor. In case you're wonder, yeah, this is my voice overs that I created. I'm kind of a weird guy I guess, as you'll soon hear. Let's reference these randomly now in the PowerUpController. We'll want to play the audio when we eat the PowerUp, so put it right here. Since now it's a bunch of Audio Sources that we're going to be referencing, although playing only one at a time, nonetheless we need access to all of them. We're going to store to an array of AudioSource Components, so to a local variable called audio. We'll use the GetComponents in… it'll be the parent right? The parent to this GameObject which is PowerUpSpawn. Right? So GetComponentsInParents() Type AudioSource. Not AudioSettings, AudioSource. I'm not batting a thousand today. It's an array of AudioSource, not AudioSources although that's you can think of it, as an array.
Now we can access whichever audio component we want by referencing its place in the arrays index. We just want to randomize it, so let's just randomize it using pretty much the technique we've been using so far., Let's calculate a Random.Range() stored in a variable of it's own for no particular reason other than clarity maybe. Then use that as the indexer, either 0, 1 or 2. It's not going to be 3, because 3 is exclusive in the Random.Range() method that takes in ints. Exclusive max value so it's going to be 0, 1 or 2. Then we reference that as the indexer, and then Play() to play that audio clip. Alright. Now what I'm going to do before I test it, is I'm going to increase, temporarily, the frequency of the PowerUp spawning just so we can hear all these effects, make sure we hear them as we're picking them up and not waste too much time. Okay. That's just a taste of what you can do with audio. We'll next look at creating dynamic audio effects with Lerp and using audio mixers and also putting some background music in the next lesson. See you there.
Lesson 42 - Playing Audio Clips