- Home /
Cycle things with one key
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");
}
}
Answer by GuyTidhar · Jun 22, 2011 at 07:41 AM
public class scr_MusicController : MonoBehaviour
{
public string[] sourceNames = new string[] { "aud_Normal", "aud_Retro"};
private AudioSource[] audioSources;
private int currentSound;
void Start()
{
// Set up the list of audioSources to be switched
if ( sourceNames != null && sourceNames.Length > 0 )
{
audioSources = new AudioSource[sourceNames.Length];
for(int i=0; i<sourceNames.Length; i++)
{
audioSources[i] = transform.FindChild(sourceNames[i]).audio;
}
currentSound = 0;
enabled = true;
}
else
{
Debug.LogError("No AudioSource names set - disabling script");
enabled = false;
}
}
void Update()
{
if (Input.GetKeyDown("m") )
{
StopCoroutine("ChangeMusic");
StartCoroutine("ChangeMusic");
}
}
private IEnumerator ChangeMusic()
{
float fTimeCounter = 0f;
int oldSound = currentSound;
int nextSound = (currentSound + 1) % audioSources.Length;
currentSound = nextSound;
while (!(Mathf.Approximately(fTimeCounter,1f)))
{
fTimeCounter = Mathf.Clamp01(fTimeCounter + Time.deltaTime);
// I fade off any sound which is not our target sound (the "nextSound")
// since you might be cycling a sound while the last one or more crossfades
// were not done yet
for(int i=0; i<audioSources.Length; i++)
{
if ( i != nextSound )
audioSources[i].volume = 1f - fTimeCounter;
else
audioSources[i].volume = fTimeCounter;
}
yield return new WaitForSeconds(0.02f);
}
// You do not need to stop a coroutine at the end of its scope - the end of the
// scope means the coroutine is about to stop processing. You only use
// StopCoroutine when you want it to stop in the middle or processing
}
}
(This script should enable you to crossfade between several audioSources). I'm assu$$anonymous$$g you FindChild function returns a script that has the parameter audio of type AudioSource...
Whoa! Didn't think that the script required that much modification. It works now as intended! Thanks for your time and effort :)
I added a more generic support so you could fade not only 2, that's why it got a bit more complex
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Timer variable set to zero 2 Answers
Why is instantiated animator prefabs are not working properly? 2 Answers
Music code not playing music. 3 Answers