My 90 day journey becoming a Unity game developer: Day-36
Objective: Add explosion sound effects to enemies, player, and asteroid.
We will start with the asteroid since it’s the simplest to set up. Let’s use the Explosion Prefab to add the explosion sound since the Explosion prefab is used on multiple game objects. Open the Explosion Prefab and add an Audio Source component. Drag the Explosion_Sound audio clip into the AudioClip slot and make sure Play on Awake is turned on.
In the Enemy script, we’re going to add a variable that will serve as the handle to the audio source component. Then in the Start() function, use the variable to gain access to the Audio Source component. Now we have 2 places where we need to use the variable when the enemy is destroyed. The first place is in the Player’s IF condition right before destroying the enemy’s ship. The second is in the Laser’s IF condition right before destroying the enemy’s ship.
Next time we will add sound effects to the powerups.