Hi!
I have in my game a maze, where the music changes based on your progress in the maze. It works pretty well, but it has a few issues. The biggest one being the audio doesn’t change going backwards. Here is the script I’m currently using (I agree, the current naming system I have is horrible).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FMODMusic : MonoBehaviour
{
private static FMOD.Studio.EventInstance Music;
// Start is called before the first frame update
void Start()
{
Music = FMODUnity.RuntimeManager.CreateInstance("event:/Music/ParameterBasedMusic");
//Music.start()
//Music.release();
}
// Update is called once per frame
public static void Progress(float ProgressLevel)
{
Music.setParameterByName("Progress", ProgressLevel);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Intro")
{
FMOD.RESULT result;
result = Music.start();
if (result == FMOD.RESULT.OK)
Debug.Log("Successfully started the music");
else
{
Debug.Log("Failed to start the music with the result of " + result);
}
result = Music.setParameterByName("Progress", 0f);
if (result == FMOD.RESULT.OK)
Debug.Log("Entered First Area");
else
{
Debug.Log("Failed to change Music Progress with the result of " + result);
}
}
if (other.gameObject.tag == "Intro2")
{
FMOD.RESULT result;
result = Music.setParameterByName("Progress", 1f);
if (result == FMOD.RESULT.OK)
Debug.Log("Entered Second Area");
else
{
Debug.Log("Failed to change Music Progress with the result of " + result);
}
}
if (other.gameObject.tag == "First")
{
FMOD.RESULT result;
result = Music.setParameterByName("Progress", 2f);
if (result == FMOD.RESULT.OK)
Debug.Log("Entered Third Area");
else
{
Debug.Log("Failed to change Music Progress with the result of " + result);
}
}
if (other.gameObject.tag == "Second")
{
FMOD.RESULT result;
result = Music.setParameterByName("Progress", 3f);
if (result == FMOD.RESULT.OK)
Debug.Log("Entered Fourth Area");
else
{
Debug.Log("Failed to change Music Progress with the result of " + result);
}
}
if (other.gameObject.tag == "Third")
{
FMOD.RESULT result;
result = Music.setParameterByName("Progress", 4f);
if (result == FMOD.RESULT.OK)
Debug.Log("Entered Fifth Area");
else
{
Debug.Log("Failed to change Music Progress with the result of " + result);
}
}
if (other.gameObject.tag == "Desert")
{
transform.position = new Vector3(83f, 1.5f, -14f);
FMODUnity.RuntimeManager.PlayOneShot("event:/Portal", transform.position);
FMOD.RESULT result;
result = Music.stop(FMOD.Studio.STOP_MODE.ALLOWFADEOUT);
if (result == FMOD.RESULT.OK)
Debug.Log("Stopped Audio");
else
{
Debug.Log("Failed to stop Music Progress with the result of " + result);
}
}
}
}
Basically, if I were to go from section 4 to section 3 in the maze, I would like the music from section 4 to calm back down to the music of section 3. But it doesn’t do that, no matter how far back I go, it continues to play the music of section 4.
I am debugging, and it shows that as it goes backwards, it does pass through the triggers and register the change in parameter. But the audio itself doesn’t change. Here is how I currently have it set up:
In the progress tab, it goes up and down visually if I play with the parameter slider.
Thanks