Hey everyone and welcome back to my Microgame Platformer series! Today we’ll take a look at how we can add footstep sounds to our character based on the animation, as well as delve into a little bit of C#.
Getting The Sounds Roaring and Ready
For the footstep sounds, I decided to take some grass footsteps from a library and cut them up into 10 semi-equal variations. I normally like to do at least 10 variations for these sorts of sounds to get some good baseline randomness when we implement them into the middleware.
If you can, I really recommend going out and recording your own grass footsteps, it’s harder than you think!
Once you have your footsteps ready, export them and head over to FMOD.
Inside of FMOD, create a new folder called “Character” and then create a 2D event called “Character_Footsteps” inside of that folder. We’re going to be using this folder for all sounds relating to the character going forward.
Create a new “Multi-Instrument” in the “Character_Footsteps” event and drag your footstep sounds into it. We’ve done this in the previous blog post, so I’m not going to show it again here, but feel free to head back if you don’t remember how to do this.
I’ve applied some mild pitch and volume randomization to the sound, as you can see on the screenshot below.
And that’s actually it for the FMOD side of things, remember to build the soundbank (F7), and let’s head over to Unity.
Getting Footsteps Implemented In Unity and Animation Tagging
Let’s start by writing the script that will handle the footsteps in Unity, it’s pretty simple. First off, start by creating a new script on your “Player” gameobject. Call it “FootstepHandler”.
If you don’t know how to make a new script, choose “Add Component” and then start typing the name you want to give your script, it will then give you the option to create a new script.
It’s always a good idea to name your scripts after what they do.
At the top of the script you need to access FMOD, so that we can use the script to tell FMOD what do to. Your script should look the same as mine except from the “using FMODUnity;” and “using FMOD.Studio;” lines. So, write those as well.
These are the two lines that will give your script access to FMOD.
To get a sound from FMOD to play, we first need to create a variable (the red-ish line on the screenshot below) that basically stores some data of a certain type for us. In this case we need the type “EventInstance”, which is basically an instance of an event from FMOD. We’ll call this variable “footstepSound”.
Now, we need to give the variable some sort of data. So, we’ll make a new method called “PlayFootstep()”. Inside of this method, we assign “footstepSound” to an event in FMOD by writing the folder path, thereby giving the variable some data to handle (in this case a sound from FMOD).
A method is basically a piece of code that is called via it’s name. When it’s called, everything inside of the code will start executing.
Next we tell the variable that it should play the sound using the line “footstepSound.start();”. These three lines in the screenshot below is basically how we get most sounds to play in Unity. We just need to put them places that makes sense, for example when the player takes damage, jumps and so forth.
(By the way, it’s totally okay to just save the screenshot down below and refer to it in the future).
Now that the scripting is done, we need to get the footstep sound to play whenever the character takes a step, for this we’ll use animation tagging.
Navigate to the folder below and double-click on the “PlayerRun” animation.
Make sure the “Animation” window is selected (the red circle). What you see in here is basically the animation divided into different frames.
You’ll get a black box on the right side, drag the “Player gameobject” into that and you’ll see the character appear (green line).
You can the use the play-bar (yellow line) to scrub through the animation and see exactly on which frames the feet touch the ground. On my end, it’s frame 7 and 20.
In the animation window, you can then click on frame 7 for example and then click on the “Add Event” button on the left.
I forgot to mark it, but it’s the small white arrow with a plus on the left side. Once you’ve clicked this on frame 7, you’ll see a small event pop up in the animation (the yellow circle). This is what we’ll use to play the sound.
When clicked on an event, you’ll see the inspector change ( it should look like the screenshot below minus the text in “Function”).
What we need to do here, is to point the event to the script we made before. So inside of Function (which is the same thing as a method), write “PlayFootstep” which is the name of the method we made inside of the script before.
Do the same thing again for frame 20. This process is what is referred to as “Animation Tagging”.
And The Extra Stuff
And that’s actually it for getting the sound to play. When you play the game now, you’ll hear your footstep sounds whenever the character takes a step.
We also need to add an FMOD listener to the game (which I forgot last time), so go to your main camera, click “Add Component” and search for the “FMOD Studio Listener”.
What a listener basically does, is act as the ears in the game. Thereby making you able to hear correctly positioned 3D sounds and such.
And that’s it for today, hope you learned something and that it was fun!
See you next week! 🙂