Atmospheric sounds

Now, we will create a system to handle atmospheric sounds. Atmospheric sounds are sound effects played in a game to give your scene more immersion. Some good examples are wind blowing in a field, the sounds of drowned out chatter in a pub, your character breathing hard after running for a while, and so on.

Creating the script and variables

To start off, we will need to create a new script and name it ATM_Manager. Next, we will create a few variables needed to play our sounds:

public List<AudioClip> tmpList = new List<AudioClip>();
public List<string> keys = new List<string>();
public List<KeyValuePair<string, AudioClip>> atmList = new List<KeyValuePair<string, AudioClip>>();
public float atmVolume = 1.00f;

The first three variables that we create are lists. The first of our lists is an AudioClip list, which will hold the sound files that we will use. Next, we create a string list, which we will fill with the name of the sound file that we want to call within our code. The last list is a KeyValuePair list, which will put together the strings and AudioClip variables we just created, and put them in a list that we will call within our code. The last variable we create will be for the volume of the sounds.

Initializing the variables

To give a value to our list that we will use within the code, we will create a Start function that initializes the list. Add this to your Start function:

void Start()
{
  audio.volume = atmVolume;
  int i = 0;
  atmList.Capacity = keys.Capacity;
  foreach(AudioClip ac in tmpList)
  {
    atmList.Add(new KeyValuePair<string, AudioClip>(keys[i], ac));
    i++;
  }
}

First, we assign the volume of AudioSource to our volume variable. Next, we create an iterator for what we will do next. To assign our internal list, we set its capacity equal to the capacity of the string list. Next, we run a foreach loop to check for every AudioClip within the temporary list that we created earlier. Finally, for each of the audio clips, we add a new KeyValuPair item, which includes the name that we want to call within the code, and the associating AudioClip to it.

Playing the atmospheric sounds

We will create two possible ways to play our atmospheric sounds. First, we will allow the sound to play and have it loop, which would be helpful for a rain sound effect or wind sound effect. Add this function to your script:

void PlayRepeat(string atmSong)
{
  for(int i = 0; i < atmList.Count; i++)
  {
    if(atmList[i].Key == atmSong)
    {
      audio.clip = atmList[i].Value;
      break;
    }
  }

  audio.loop = true;
  audio.Play();
}

This function takes a string. This string will be used to select the sound to be played. A good example for this would be if you wanted to play a rain atmospheric sound, you would have a key that is assigned as Rain in your KeyValuePair list and its AudioClip value would be a rain sound file.

Within the PlayRepeat function, we use a for loop to iterate through the KeyValuePair list. If one of the keys in the KeyValuePair list matches the string passed to the PlayRepeat function, we assign the clip of the AudioSource to that key's value, which would be an AudioClip. Lastly, we break the for loop, set the AudioSource loop property to true, and play the sound.

Next, we will add a function that will allow us to play a sound that doesn't loop. Add this function to the script:

void Play(string atmSong)
{
  for(int i = 0; i < atmList.Count; i++)
  {
    if(atmList[i].Key == atmSong)
    {
      audio.clip = atmList[i].Value;
      break;
    }
  }

  audio.loop = false;
  audio.Play();
}

This function will run in the same way as the PlayRepeat function, except we aren't setting the loop property of the AudioSource list. Since the loop property isn't being set to true, the sound will only play once.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.170.134