Today we’re taking a look at how to play a sound in Unity when pressing a key, using it’s native audio engine. It’s pretty simple, but you can use this same basic method when wanting to play a sound when entering triggers, when collisions are happening and so forth. I’m going to be showing you both how to just play a single sound and also how to map out different sounds for different keys. Have fun!
Why is this useful?
You might be wondering why this is useful at all, so here’s some reasons why. First of all you’ll get a basic understanding of how to play a sound in Unity and how the audio engine works. Imagine a game where the “G” button for example is always the same ability and always needs the same sound, this is how you would implement that and make that happen in it’s most basic form.
You’ll also get a basic understanding of some audio scripting and C# if you don’t already have it. Which is honestly extremely useful.
To Unity We Go!
Let’s start out by opening up a blank Unity project.
In the name of good folder structure, navigate to your “Assets” folder, right-click and make a new folder called “SFX”.
Open up the “SFX” folder and throw in 4 sounds. In reality you can use as many sounds as you want, but let’s start with 4 for now.
Next up, create a new gameobject and call it “sound”. On the gameobject, in the inspector tab, click “Add Component” and choose (search for) “Audio Source”, this is what you’ll use to play the sound.
On the “Audio Source” component, drag a sound into grey area next to where it says “AudioClip”. You can see that I’ve just chosen a generic ice spell sound in this case. Also, be sure to uncheck “Play On Awake”. As you can probably guess, what “Play On Awake” does is that is play the sound as soon as you press play, which is useful for something like ambience or music, but not in our case where we only want to play a sound when we press a key.
Now, click on “Add Component” again and make a script called “PlayOnKeyPress”.
Now we get to the meat of it all, the scripting. As you can see below it’s pretty basic, but let me explain it quickly. First of all we create a public variable of the type “AudioSource”, I have just given it the name keySound. By doing this, we can just reference the sound by dragging and dropping the sound in the inspector.
Inside of the “Void Update” function, which updates every frame, we basically ask the question; “If the input is the key “G”, then play the sound that keySound is referencing.”
And that’s it, hop back into Unity.
Now you’ll see that you can drag an Audio Source onto the “PlayOnKeyPress” script, that’s because you made the keySound variable public.
So, go on ahead and drag the gameobject we made before (the one called sound), into the empty field. The gameobject you drag into this field NEEDS an Audio Source component, because that is what the “keySound” variable is looking for.
And voila! That’s it! Now, when you press the “G” button while playing the game, you’ll hear the sound play.
Playing Multiple Sounds
Now, to play different sounds on different keys from one script, we need to make a few small changes.
First, go into the “PlayOnKeyPress” and change the keySound variable to an array, but typing “[” and “]” next to the AudioSource. What an array basically does, is hold a group of data, in this case different Audio Source components.
Inside of the update function, you need to assign which sound from the array what you want to play and which key you need to press. You choose which sound in the array you want to play by typing “[” and “]” next to keySound and then choose a number. Remember that arrays starts at 0, and then counts upwards. You can then take that piece of code, copy and paste it, and the change which key you need to press to play the sound.
Now, head back to Unity and delete the “Audio Source” from the “Sound” gameobject, and instead make 4 children of that gameobject called “Sound 1-2-3-4”. On each of those, add an Audio Source component, assign a sound to each of them and uncheck “Play On Awake”, just like we did before.
Now, navigate down to your “PlayOnKeyPress” script on your “Sound” gameobject, choose the size of the array (basically how many sounds you want to be played) and then assign a gameobject with an “Audio Source” component on it, to each of them.
And that’s it basically! If you press play, you’ll hear a different sound when pressing each of the keys you’ve chosen.
Hope you learned something today, and as always if you have any questions feel free to comment or send me a message!
- Go through the example above a couple of times and get a feel for how all of this works
- Map out sounds to 10 different keys on your keyboard.
- Research a bit, and find out how you can play a sound when entering a trigger instead of when pressing a key.
- Research a bit, and find out how you can play a sound when an object collides with another object, instead of when pressing a key.