Coroutine and SetFloat not working as expected
I have set up a Coroutine to fade a mixer channel in and out depending on being within a trigger using a lerp to fade over time. The fade-in is working correctly but when I leave the trigger area the mixer is not updating.
I have debugged and the exposed parameter, arpVol, is apparently being updated by SetFloat but nothing changes on the actual mixer itself.
public AudioMixer masterMixer;
public float minimum = -80f;
public float maximum = 0f;
public float speedOfIncrease = 0.08f;
public float arpVol;
//private void SetArpVol(float arpVol)
//{
// masterMixer.GetFloat("arpVol", out arpVol); //pulls value from Arp mixer attenuation
//}
void OnTriggerEnter(Collider coll)
{
Debug.Log("trigger entered");
StartCoroutine(fadeInSound());
StopCoroutine(fadeOutSound());
}
void OnTriggerExit(Collider coll)
{
Debug.Log("trigger exited");
StartCoroutine(fadeOutSound());
StopCoroutine(fadeInSound());
}
IEnumerator fadeInSound()
{
Debug.Log("fadeinsound()");
var originalValue = 0F;
masterMixer.GetFloat("arpVol", out originalValue);
var speedLocal = speedOfIncrease;
// -80 to 0
while (arpVol <= 0f)
{
Debug.Log("fadeinsound() while");
masterMixer.SetFloat("arpVol", Mathf.Lerp(originalValue, maximum, speedLocal));
speedLocal += 0.1f * Time.deltaTime;
yield return null;
}
}
IEnumerator fadeOutSound()
{
Debug.Log("fadeOUTsound()");
// 0 to -80
var originalValue = 0F;
masterMixer.GetFloat("arpVol", out originalValue);
var speedLocal = speedOfIncrease;
while (arpVol >= -80f)
{
Debug.Log("fadeoutsound() while");
var d = 0F;
masterMixer.GetFloat("arpVol", out d);
Debug.Log("mixer varpvol is now: " +d);
var increase = Mathf.Lerp(originalValue, minimum, speedLocal);
//Debug.Log("increase over time: " + increase);
masterMixer.SetFloat("arpVol", increase);
speedLocal += 0.1f * Time.deltaTime;
if (arpVol >= -70)
arpVol = -80;
yield return null;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Stop previous coroutine and start new one from beginning 1 Answer
How to use one button to play different audio for different Targets? 0 Answers
Trying to move buttons via Coroutine and transform.Translate 0 Answers
AudioSource.Play plays every sound at once,AudioSource.Play Plays every source at once 0 Answers
[Tanks Tutorial] The "engine driving" AudioClip doesn't start 1 Answer