Exercises

Our footstep audio is likely going to be the most commonly heard sound effect in the game. The constant repetition can wear on the player, causing listener fatigue. In order to help mitigate that, let’s create an array of footstep sound effects to draw from.

1. Open the PlayerController script. Make the changes shown in Listing 11.10.

Listing 11.10 Changes to the PlayerController Script

public AudioClip[] footstepSounds;void PlayFootstepAudio(){  this.audioSource.clip = footstepSounds[(Random.Range(0,footstepSounds.Length))];  this.audioSource.Play();}

2. Select the Player GameObject.

3. Set the size of the Footstep Sounds array to 3.

4. Assign a different footstep sound to each field in the array.

Get Learning 2D Game Development with Unity®: A Hands-On Guide to Game Creation now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.