Creating a Dynamic Music System


Prelude:

There are multiple different ways to do dynamic music, but the two most prominent ways are track based and instrument based.

Track-based dynamic music is best when you have different versions of a song for different scenarios.
Instrument-based dynamic music is when different individual instruments are added into the music based on what's happening in game.

Track-based music is best when you have a song change its style to fit gameplay, like going from calm music to battle music, but the melody stays the same, and instrument-based music is best where many things are happening in game and you decide to give a musical identity to each, allowing you to toggle them on/off if they are present in game.

Hades is a great example of track-based dynamic music, and the Pikmin series is a great example of instrument-based dynamic music.

In this tutorial, I will showcasing how I implemented dynamic track-based music into Beyond the Clouds.

Tutorial:

Start by creating the different versions you need of the same song, I created a calm, battle, and boss version of the same song. Major things to keep in mind: all of the different versions should follow the same overall progression, have the same melodies, and be the exact same length. The length is the most important part, but keeping similar progressions and melodies will help the final result be more seamless.

Here is the music with visualizations to showcase how each is the same song in essence, but with different instruments and formatting they can be used for different situations:

Once your music is made and properly exported (remember to keep them all the same length), then we can finally go into Unity.

I started with an empty game object and I placed an audio source on it for each different version of music.
Set up the audio files as the AudioClip,  set the mixer if you have one, and make sure each of the AudioSources have Loop and PlayOnAwake variables toggled on. Here is an example:

After this is set up, I create and attach a script called "DynamicMusic". The plan is to use this script to make the game object a singleton, allowing things to access functions inside the class without a direct reference to the object.

Start by setting up the static variable of the class:

Then, in the Awake function, you can set the proper check to assign the instance or delete the game object, since this script should only ever be on one object at a time.

I have the object being set as a DontDestroyOnLoad to transfer between scenes and keep the music seamless, but you can also just have a dynamic manager object in each scene.

Also make sure you have variables for each AudioSource connected to the game object:

Next I add a coroutine method to  change volume over time for any of the audio sources.

For ease-of-access I also added a public enumerator and a variable to hold which version the music is on:

The final major step is to add a method to call to actually change the music! This method will check the context put in as a parameter, then switch to the appropriate song while muting the others, in the given time.

After you have connected everything in the inspector,

you can call the class instance from anywhere else in your project, and if you have the dynamic music object set up in your scene, it can dynamically change the music!

Here I made a simple script to change the music in Start(). It checks if the instance of the class exists, and if it does then it changes to the music you choose in the inspector.

In the demo I am using developer keys to change the music, but you can call the music changes using gameplay!

This system can have added complexity and even be combined with the instrument-based dynamic music.

Get Beyond the Clouds

Leave a comment

Log in with itch.io to leave a comment.