The Effect of Crossfading Soundtrack

This has been a cool addition to the game!
The soundtrack changes depending on which realm the player currently is in. If the player is in the human realm, asian instruments and taiko drums plays. If the player is in the spirit realm, the soundtrack goes all electronic and choirs are heard singing.
By the time I had to implement this dynamic effect, the gameplay themes of the game were nearly done, but still usable as they were. In fact, these uploaded tracks aren’t completed either but… time constraints.

In the human realm:

In the spirit realm:

I wish I could’ve done more before uploading them, and I most probably will.

I put these soundtracks into each of their own audio source in a game object on the scene, and assigned them to a script. I made sure the spirit version was at 0 volume, since the player would begin playing in the human realm, not the spirit realm. Now I had to make these two tracks crossfade when the player switches realms.

The script I assigned the audio sources to is named SoundManager (more appropriately named as AudioManager) and it manages the crossfade effect as well as playing sounds.
It has the boolean variable m_currentRealm that is being set to the same boolean value that decides which realm the player is in (LevelData.m_spirit). In each update, these two values are checked if they’re equal to each other and if they aren’t then it means the player is in another realm and the soundtrack needs to change.

A coroutine function that creates the crossfade effect receives different parameters depending on what realm the player is in. A coroutine, as explained in a previous post, executes a code that runs alongside the main one, so the program doesn’t have to wait until that code has fully exectued.
The parameters given is of course the audio sources so the coroutine knows what volume of an audio source to lower and vice versa.

Before lowering and raising any volumes, a float is instantiated with a value of 0f. It acts as a counter (timeCounter).
Next, a while loop runs as long as timeCounter is not similar to the value 1f, being done with Math.Approximately(). In the beginning of this loop, the timeCounter is being added with 0.2f.  Then the volume of the audio source that fades away, gets reduced from it’s maximal value (1f) with the new value of timeCounter. After that, timeCounter gets assigned to the volume of the audio source that fades in.
A brief period of time follows by using ‘yield return’ and then the loop repeats.

When timeCounter is similar to 1f, the coroutine proceeds to assigning a couple of values. It first of all makes m_currentRealm equal to LevelData.m_spirit so the SoundManager is updated on where the player is and then it makes the boolean value m_changing go false again.
I never mentioned m_changing. All it does is making sure several coroutines doesn’t start executing. It only does that when m_currentRealm and LevelData.m_spirit is unequal and m_changing is false. If that if statement passes, m_changing goes true.

That’s what is being done when a crossfade effect occurs. I did not come up with this by myself, and searched around the web in how to create the effect. Nonetheless, I’m quite satisfied to have achieved making the soundtrack dynamic and responsive in this kind of way and it’ll surely make the game more immersive for the player!

About Antonio Huayna Ackalin

2016 Programming