RandomAudioPlayer

Checked with version: 2017.3

-

Difficulty: Beginner

This script allows you to play a random sound chosen from a list of user-assigned clips. You can see it being used to control different sounds on the Ellen prefab (check under the SoundSources child GameObject of Ellen). For example, looking at FootstepSource, you can see that it defines a list of four footstep sounds that the footstep randomly picks from, with an added pitch randomization. That example also includes the use of overrides per specific tile - an override for the Alien Tile plays a different footstep sound than if Ellen was walking on a grassier surface. To add overrides to GameObjects that aren't a tilemap, check the AudioSurface section below.

Scripting side

On the scripting side, RandomAudioPlayer has a PlayRandomSound function that is called by other scripts when a sound needs to be played (e.g. the footstep sound is triggered by the function PlayFootstep in the PlayerController, while the function itself is called by the Animation Clip on the frame where the feet make contact with the ground.)

This function can take a TileBase to choose override sounds (e.g. footsteps use the current surface the Player is on, or a bullet colliding uses the Tile of the surface it just collided with).

AudioSurface

AudioSurface is a script you can add to any GameObject that is not a tilemap. It allows you to define which Tile should be used when the Audio Player looks for an override sound (e.g. a stone box would use an AudioSurface with the Alien Tile as the tile setup, so walking on it would trigger the "stone footstep" sound and not the normal "ground" sound).

Related tutorials