Playing and controlling different sounds, each with its own AudioSource component
The approach in the previous recipe (using PlayOneShot(...)
with a single AudioSource
component) is fine for one-off sound effects. When further control is required over a playing sound, each sound will need to be played in its own AudioSource
component. In this recipe, we’ll create two separate AudioSource
components and pause/resume each of them with different arrow keys.
Getting ready
Try out this recipe with two audio clips that are several seconds long. We have included two free music clips inside the 04_02
folder.
How to do it...
To play different sounds with their own AudioSouce
components, perform the following steps:
- Create a new Unity 2D project and import the sound clip files.
- Create a new empty GameObject in the scene named
Music medieval
, containing an AudioSource component that is linked to the186772__dafawe__medieval
AudioClip. You can do this in...