Today my goal is to set up explosion sounds for the asteroid and enemies, as well as powerup pickup sounds.
Just like with the Player, we’re going to want to make sure an Audio Source component is installed on the enemy,
For this one though, we’ll want to ensure Play On Awake is off, and we’ll also be setting the sound clip in the code as well.
Now, since I’m using wait commands in the death animations, I can just play the sound before the animation begins and it won’t have any issues. I’m not going to set a death sound for my player, as I don’t want to double up on the same explosion sound when I die to an enemy ship hitting me, but if I was, I’d want to use AudioSource.PlayClipAtPoint(_explosionSound, new Vector3(0, 0, -15)); since my main camera is at Z:-15, and the distance if I place the sound directly on the GameObject would make it play incredibly soft as PlayClipAtPoint turns on (I believe?) Spatial Blend by default.
I could do the same for the Asteroid, but since it’s instantiating its own explosion already, I can do a far easier method to deal with it: I’ll just attach the Audio Source to the explosion prefab, and have it Play On Awake. This will run the explosion sound when the explosion is instantiated without any scripting required.
Lastly, my powerups! As with most things in programming, there’s many solutions for this, but since I have places in my Player.cs script to activate the powerups already, I’ve just sourced another AudioClip in the Player and used _audioSource.PlayOneShot(_powerUpPickup); as I did with the laser fire. PlayOneShot bypasses the assigned audio, meaning you can play several files without needing to assign them to the Audio Source first. Then I simply placed that into each (powerup)Active class.
This is it for now! Next I’ll be examining how to build and test my game!