Audio Fade With Toggle
I whant a toggle that when it's get off the audio Source volume get's faded to 0 and when the toggle get's on the volume of the source come again to 1 faded, how i can do that My script is wrong. This is my Script :
using UnityEngine;
public class PauseAudio : MonoBehaviour
{
public AudioSource _Source;
public float currentVolume;
public float fadeSpeed;
public bool On = true;
void Update()
{
currentVolume = _Source.volume;
Pause();
}
public void Pause ()
{
if (On)
{
currentVolume = Mathf.MoveTowards(currentVolume, 1, fadeSpeed * Time.deltaTime);
}
if (!On)
{
currentVolume = Mathf.MoveTowards(currentVolume, 0, fadeSpeed * Time.deltaTime);
}
}
}
Answer by YewoTheuDevs · Apr 12, 2020 at 05:44 PM
The thing is you cant have it fade with that function cause it will only run once. You need it to run in the update function. void Update() { if (On) { currentVolume = Mathf.Lerp(currentVolume, 1, .125f); }
if (!On)
{
currentVolume = Mathf.Lerp(currentVolume, 0, .125f);
}
_Source.volume = currentVolume;
}
Your answer
Follow this Question
Related Questions
How to fade in and out two logos inside the same scene? [c#] 1 Answer
How to fade in and out two panels in a scene? (c#) 1 Answer
Audio Fade In / Fade Out with Trigger 0 Answers
Set public gameobject by raycast hit target 1 Answer
Tanks! tutorial movament problem,Tanks tutorial problem with moving 0 Answers