UnityGameProjectsCode / AbandondedGame / SoundManager.cs
SoundManager.cs
Raw
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SoundManager : MonoBehaviour
{
    public AudioSource primaryASource;
    public AudioSource secondaryASource;
    public AudioSource tertiaryASource;
    public AudioSource fourthASource;
    public AudioClip deathSound;
    public AudioClip windSound;

    private float timeSinceSecondStart;
    private float timeSinceThirdStart;
    private float timeSinceFourthStart;
    private bool secondLerpComplete;
    private bool thirdLerpComplete;
    private bool fourthLerpComplete;

    private void Update()
    {
        if(GetSecondSourcePlayingState() && !secondLerpComplete)
        {
            timeSinceSecondStart += Time.deltaTime;

            float lerpAmt = Mathf.InverseLerp(0, 40, timeSinceSecondStart);
            float adjustedLerp = lerpAmt / 10;

            secondaryASource.volume = adjustedLerp;

            if (adjustedLerp >= 0.05f)
                secondLerpComplete = true;
        }

        if(GetThirdSourcePlayingState() && !thirdLerpComplete)
        {
            timeSinceThirdStart += Time.deltaTime;

            float lerpAmt = Mathf.InverseLerp(0, 40, timeSinceThirdStart);
            float adjustedLerp = lerpAmt / 10;

            tertiaryASource.volume = adjustedLerp;

            if (lerpAmt >= 0.05f)
                thirdLerpComplete = true;
        }

        if (GetFourthSourcePlayingState() && !fourthLerpComplete)
        {
            timeSinceFourthStart += Time.deltaTime;

            float lerpAmt = Mathf.InverseLerp(0, 100, timeSinceFourthStart);
            float adjustedLerp = lerpAmt / 10;

            fourthASource.volume = adjustedLerp;

            if (lerpAmt >= 0.05f)
                fourthLerpComplete = true;
        }
    }

    public void EnableSecondSource()
    {
        secondaryASource.Play();
    }

    public void EnableThirdSource()
    {
        tertiaryASource.Play();
    }

    public void EnableFourthSource()
    {
        fourthASource.Play();
    }

    public bool GetSecondSourcePlayingState()
    {
        return secondaryASource.isPlaying;
    }

    public bool GetThirdSourcePlayingState()
    {
        return tertiaryASource.isPlaying;
    }

    public bool GetFourthSourcePlayingState()
    {
        return fourthASource.isPlaying;
    }

    public void OnGameOver()
    {
        secondaryASource.Stop();
        tertiaryASource.Stop();
        fourthASource.Stop();
        secondLerpComplete = false;
        thirdLerpComplete = false;
        fourthLerpComplete = false;
        primaryASource.clip = deathSound;
        primaryASource.Play();
        primaryASource.loop = false;
        secondaryASource.loop = false;
        tertiaryASource.loop = false;
        fourthASource.loop = false;
    }

    public void OnGameReset()
    {
        primaryASource.clip = windSound;
        primaryASource.loop = true;
        secondaryASource.loop = true;
        tertiaryASource.loop = true;
        fourthASource.loop = true;
        primaryASource.Play();
    }
}