> For the complete documentation index, see [llms.txt](https://steijn.gitbook.io/devlogs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://steijn.gitbook.io/devlogs/devlog-personal/duos/fmod-in-unity.md).

# FMOD in unity

What would be a decent way to make adaptive music for our game.

First we'd need to identify what parameters would be needed for our music. \
After having a discussion we came to the conclusion there'd be four distinct parameters.\
\
1\. PlayerReady - to show if the player was ready to begin\
2\. WaveStarted - to show wether or not a wave is currently ongoin\
3\. EnemyDistance - to show how close the enemy is to the base \
4\. PlayerHealth - to show how much hp the player has / if he's lost the game

Using these four parameters i set up the fmod project using the music we settled on called "moon garden"&#x20;

![](/files/-MYsrMZm2QcU5r5m4GRN)

At first i couldn't get the loops just right so i had to ask Jack H. for feedback as well as for pointers, he helped me listen to the notable notes that i could use as a reference point. This resulted in less jarring loops. \
\
Afterwards we had to implement this into unity, for this i'll be using the following [tutorial](https://www.youtube.com/watch?v=QujIch7TPBU)

```
FMOD.Studio.EventInstance backgroundMusic;
// Start is called before the first frame update
void Start()
{
    backgroundMusic = FMODUnity.RuntimeManager.CreateInstance("event:/backgroundmusic");
    backgroundMusic.start();
    PlayerReady();
}

public void PlayerDeath()
{
    backgroundMusic.setParameterByName("PlayerHealth", 1); //0 = playerAlive, 1 = playerDead
}
public void WaveStart()
{
    backgroundMusic.setParameterByName("WaveStarted", 1); // 0 = wave not started, 1 = wave started
}
public void WaveStop()
{
    backgroundMusic.setParameterByName("WaveStarted", 0); // 0 = wave not started, 1 = wave started
}
public void PlayerReady()
{
    backgroundMusic.setParameterByName("PlayerReady", 1); // 0 = not ready, 1 = ready
}
public void EnemyDistance(float enemyDistance)
{
    enemyDistance = ((1000 / 200) * enemyDistance); // to offset the difference in the project and current enemy distances


    backgroundMusic.setParameterByName("EnemyDistance", enemyDistance);
}
public void StopInstance()
{
    backgroundMusic.stop(FMOD.Studio.STOP_MODE.IMMEDIATE);
}
```

Using this i've managed to make it change the parameters ingame.&#x20;

It was clear that fmod can be used for all events. from towers shooting to enemies attacking or background music. For the next project i'm considering placing everything inside of the fmod project sound-wise so that we have a single place to edit and change sounds to our liking.&#x20;
