Hi!
I want to cycle music with same key (in my case, letter “m”) but I don’t know how to write the function in.
Now I can crossfade music with two different keys but that is not I want. What should I do?
Help would be much appreciated
Here is the code:
using UnityEngine;
using System.Collections;
public class scr_MusicController : MonoBehaviour
{
void Update()
{
if (Input.GetKeyDown("m"))
{
StartCoroutine("ChangeMusic1");
}
if (Input.GetKeyDown("n"))
{
StartCoroutine("ChangeMusic2");
}
}
private IEnumerator ChangeMusic1()
{
float fTimeCounter = 0f;
while (!(Mathf.Approximately(fTimeCounter, 1f)))
{
fTimeCounter = Mathf.Clamp01(fTimeCounter + Time.deltaTime);
transform.FindChild("aud_Normal").audio.volume = 1f - fTimeCounter;
transform.FindChild("aud_Retro").audio.volume = fTimeCounter;
yield return new WaitForSeconds(0.02f);
}
StopCoroutine("ChangeMusic1");
}
private IEnumerator ChangeMusic2()
{
float fTimeCounter = 0f;
while (!(Mathf.Approximately(fTimeCounter, 1f)))
{
fTimeCounter = Mathf.Clamp01(fTimeCounter + Time.deltaTime);
transform.FindChild("aud_Retro").audio.volume = 1f - fTimeCounter;
transform.FindChild("aud_Normal").audio.volume = fTimeCounter;
yield return new WaitForSeconds(0.02f);
}
StopCoroutine("ChangeMusic2");
}
}