So now that we have some sound in our game, let’s get some variety to what the Player is hearing.
First, we you separate out our SFX into groups that we can pull random samples from. Here, I have General, Explosion and Laser. We won’t be pulling randomly from from general, as this is were we will store all our one off sounds for the UI and such.
Let’s focus on working with the Laser SFX for the Player.
Since we want to keep the other classes as clean as possible, we will make some changes to our AudioManager first.
We have replaced the _SFX variable with the three variables we decided on above, _generalSFX, _explosionSFX and _laserSFX.
Then if modify our PlaySFX by giving it a new Parameter of SFXGroup, we can use a switch statement to request specific sound effect from a certain SFX group.
Now to get a random effect with as little fuss as possible, we can make on Overload for PlaySFX() by changing the required parameters. In this case, we will remove the ClipID argument.
We can set a local method variable of RND to hold our random numbers. And our Switch statement will be just like the last one but with two changes. Case 0:, our General SFX group will not allow Random requests, so we replace the PlayOneShot with a LogError just in case aonther developer tries to access this functionality and doesn’t see why the SFX isn’t playing. Second, we add a Random.Range call in Cases 1 and 2 to get a INT that falls in the range of the arrays. Then we pass the INT into the PlayOneShot.
And now in the player, we adjust the PlaySFX() to pass the SFXGroup through to the AudioManager’s PlaySFX(SFXGroup). If we ever want the player to play one specific sound, we can call the other version of the Method by adding in the Clip ID to our call to AudioManager.
With these small changes and a collection of (not too different) laser Sound effects, we have improved the Game’s quality further.