2D Shooter: Sound to Explosions
Objective: Add sound effects to the explosions.
We will need to do this twice: Once for the asteroid explosion, and another for when the enemy explodes. The Asteroid should be straightforward.
All you need to do is add the audio clip to an audiosource component on the explosion object, and make sure “play on awake” is checked. This will make it so that as soon as the explosion is spawned, the sound effect will play.
Since the enemies use a different explosion animation, we’ll need to add another audiosource component to the enemy object. Add the explosion sound to this as well, and now its time to go into the script.
Make a variable and get the audiosource component in the start method.
Now call it when the enemy gets hit by the laser, and the player.
Remember to call the sound before the object is destroyed, otherwise there won’t be an object left to make sound. Now you should be good to go!