Chapter 8. Aural Integration

In this chapter, you will learn about adding audio in our game in various ways. Combining the usage of background music, atmospheric sounds, and event-driven sound effects, you will have a game that feels very much alive. Audio plays a major role in how players perceive the game world, and can be as important as graphics in setting the mood of a scene.

In this chapter, you will learn about the following things:

  • How to create a random music system
  • How to create a playlist-styled music system
  • How to integrate and manage atmospheric sounds
  • How to create an event-driven sound effects system

Background music

The first part of audio that we will cover will be the background music. Having music in your background can set the mood of a scene, keep the player entertained on a subconscious level, or even be gameplay mechanics that the player interacts with. We will create a dynamic system that will allow us to play songs randomly or in a playlist style.

Creating a random system

The first step in creating our background music system will be to create a new C# script and name it BG_Music_Manager. Before we start scripting, add the using statement to the top of the script using the other using statements:

Using System.Collections.Generic;

We will need the using statement so that we can use lists. Next, we will create a few variables and add these to our script:

public List<AudioClip> SongList = new List<AudioClip>();
public float bgVolume = 1.00f;
public int curSong = 0;
public int ranMin, ranMax;
public bool playRandomly = false;

Our first variable is a list of audio clips, which we will use to hold the songs that we want to play in our game. The next variable is float, which will determine how loud our music is. The int variable after that will be used as an iterator for the playlist system when we implement it. The next two integers will be used for our random music system. Finally, the last variable we have is a Boolean value that we will set to pick the system we want for our background music. Now, we will add the function that will play a random song from our list:

void PlayRandom()
{
  if(!audio.isPlaying)
  {
    audio.clip = SongList[Random.Range(ranMin, ranMax)];
    audio.Play();
  }
}

Before we play a song, we check to see if there is a song playing currently. If there is no song playing, we select a song and play it. To select a song, we grab one from our AudioClip list randomly. To get the random number, we use the minimum and maximum variables we created earlier and place them inside the Range function of the Random class. Using this function will get us a random number between our variables. Once we have a song assigned, we play it.

Adding a playlist system

Next, we will add our function that will run the playlist:

void Playlist()
{
  if(!audio.isPlaying)
  {
    if(curSong > SongList.Capacity)
    {
      curSong = 0;
    }
    else
    {
      curSong++;
    }
    audio.clip = SongList[curSong];
    audio.Play();
  }
}

The first thing that we check for when running this function is if there is already a song playing. If there is a song playing, the code will wait until the song stops; if there is no song playing, our code gets executed. To execute the code, we use the iterator variable that we created earlier; if the value is higher than the number of songs we have, we reset it to zero.

If the value of the iterator variable is less than the number of songs we have, then we iterate it and move on to selecting our song. To select the next song to be played, we grab it from the AudioClip list and assign it to the clip property of the GameObject's AudioSource component. Lastly, we call the Play function of AudioSource to play the song.

Implementing the audio systems

Now that we have created our two systems to play music, we will need to finish off our script with a few other features. First, we will create the Start and Update functions as they will be needed for this script to work:

void Start()
{
  audio.volume = bgVolume;
  ranMax = SongList.Count;
}
  
void Update()
{
  if(playRandomly)
    PlayRandom();
  else
    Playlist();
}

In the Start function, we assign the volume of AudioSource to our volume variable. Next, we set the maximum value to the variable for our random function to the amount of songs that we have in our list. In the Update function, we call the functions that actually play the music. To do this, we check the bool variable that we created earlier to decide which system to use.

An added feature we can place in our script will allow us to play a single song repeatedly. Add this function to the script:

void PlayRepeat(AudioClip Song)
{
  audio.clip = Song;
  audio.loop = true;
  audio.Play();
}

This function will take the AudioClip value, which will be the song to be played. Next, we assign the song to AudioSource, set its looping property to true, and finally, play it.

The last feature that we will add to our script will allow us to change the speed of the song by using the pitch property. Add this function to the script:

void ChangeSpeed(float Speed)
{
  if(Speed > 3)
    Speed = 3;

  if(Speed < -3)
    Speed = -3;

  audio.pitch = Speed;
}

This function takes a float variable, which will be used to determine the speed of the song. The maximum speed value is 3, the minimum value is -3, and the default value is 1. When the speed is changed with this function, we check to make sure that the new value is within these boundaries. Once we check the value and it is correct, we set the pitch of AudioSource to the new speed.

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

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