You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

73 lines
1.4 KiB

using UnityEngine;
using System.Collections;
public class musicController : MonoBehaviour {
public AudioClip startMusic;
public AudioClip endMusic;
private AudioSource source;
private bool triggered = false;
void Awake(){
source = GetComponent<AudioSource> ();
}
// Use this for initialization
void Start () {
source.clip = startMusic;
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.Equals)) {
source.volume += 0.1f;
}
if (Input.GetKeyDown (KeyCode.Minus)) {
source.volume -= 0.1f;
}
}
IEnumerator fadeOut (float end, float steps){
Debug.Log ("Volume: " + source.volume);
float curVol = source.volume;
while(source.volume > end + steps)
{
source.volume -= steps * Time.deltaTime;
Debug.Log ("Volume: " + source.volume);
yield return new WaitForSeconds(0.01f);
}
source.volume = end;
source.clip = endMusic;
source.Play();
StartCoroutine (fadeIn (curVol, 0.2f));
yield return null;
}
IEnumerator fadeIn (float end, float steps){
while(source.volume <end - steps)
{
source.volume += steps * Time.deltaTime;
yield return new WaitForSeconds(0.01f);
}
source.volume = end;
yield return null;
}
void OnTriggerEnter(Collider other) {
Debug.Log ("Music Change");
if (triggered)
return;
if (other.transform.tag != "Player")
return;
StartCoroutine (fadeOut (0.0f, 0.2f));
//fadeIn (1.0f, 0.01f);
triggered = true;
}
}