In the previous article, we have created single audio system by using AudioManager. Today we are going to play the footstep audio to all of walkable creatures in game.
We will use Animation Event to help us finishing this function.
Create a new script and call it FootstepAudio.
Within this script, we will only need to call the AudioSource to play the footstep audio when the animation is stepping on the ground.
With this script, every gameobject with Animator should also add an AudioSource component into it.
Let’s do this part to Player.
Select Sprite gameobject under Player(which is where we control the animation with Animator). Then add an AudioSource component and FootstepAudio script.
Drag the footstep audio file into AudioClip column, then disable Play On Awake.
Once done, we can make the running animation to run with footstep sound.
Open Animation tab of Player(by selecting Sprite gameobject), and select Run animation.
Dragging the timeline marker to where the Player has stepped on the ground, then click Add Event button.
It should look like this.
Now select the event marker, and select the method we created in FootstepAudio script as calling event.
Now we have successfully assigned the footstep sound to the running animation.
Play it and check the result!