Scripting audio feedback
As with the VFX, audio also needs to react to what is happening to the game to give a better sense of immersion. Let’s start adding sound to the explosion effect that enemies spawn when they die, which doesn’t necessarily need scripting itself, but is a result of the script that spawned the explosion in the first place:
- Download an explosion sound effect from the internet or the Asset Store.
- Select the Explosion prefab we spawn when the enemies die and add Audio Source to it.
- Set the downloaded explosion’s audio clip as the AudioClip property of the audio source.
- Make sure Play On Awake is checked and Loop is unchecked under Audio Source:
Figure 14.22: Adding sounds to our explosion effect
As you can see here, we didn’t need to use any script. As the sound is added to the Prefab, it will be played automatically at the very moment the Prefab is instantiated. Now, let’s integrate...