Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question How can I make the background music fade out after timer hits 0 ?

Discussion in 'Audio & Video' started by Ykaru, Nov 14, 2023.

  1. Ykaru

    Ykaru

    Joined:
    Nov 12, 2021
    Posts:
    1
    Hi, I would like to know how can I make the background music fade out after timer is 00:00 ?
    How can I add that to this script ?
    This is my timer script.
    Code (CSharp):
    1. using UnityEngine;
    2. using TMPro;
    3. using UnityEngine.SceneManagement;
    4. using System.Collections;
    5.  
    6. public class TimerManagerScript : MonoBehaviour
    7. {
    8.     public float timeLeft;
    9.     public bool timerOn = false;
    10.     public GameObject player;
    11.     private bool stopMovement;
    12.  
    13.     public TMP_Text timerTxt;
    14.     void Start()
    15.     {
    16.         stopMovement = GameObject.Find("Player").GetComponent<PlayerMovement>().enabled;
    17.         timerOn = true;
    18.     }
    19.  
    20.     void Update()
    21.     {
    22.         timerCheck();
    23.     }
    24.  
    25.     public void timerCheck()
    26.     {
    27.         if (timerOn)
    28.         {
    29.             if (timeLeft > 0)
    30.             {
    31.                 timeLeft -= Time.deltaTime;
    32.                 updateTimer(timeLeft);
    33.             }
    34.             else
    35.             {
    36.                 timeLeft = 0;
    37.                 timerOn = false;
    38.                 timerTxt.color = Color.red;
    39.                 Destroy(player);
    40.                 stopMovement = false;
    41.                 StartCoroutine(ResetLevel());
    42.             }
    43.         }
    44.     }
    45.  
    46.     public void updateTimer(float currentTime)
    47.     {
    48.         currentTime += 1;
    49.  
    50.         float minutes = Mathf.FloorToInt(currentTime / 60);
    51.         float seconds = Mathf.FloorToInt(currentTime % 60);
    52.  
    53.         timerTxt.text = string.Format("{0:00}:{1:00}", minutes, seconds);
    54.     }
    55.  
    56.     IEnumerator ResetLevel()
    57.     {
    58.         yield return new WaitForSeconds(2);
    59.         SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    60.     }
    61. }
    62.  
     
  2. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    430
    I see two simple ways to do that:

    When
    timerCheck()
    could return a bool, and when it's true you could gradually lower the volume of a specific AudioSource. Ideally I would run that in FixedUpdate to have a smoother fade out.

    Code (CSharp):
    1. void FixedUpdate()
    2. {
    3.     if (timerFinished())
    4.     {
    5.         if ((audioSource.volume *= fadeRate) < 0.01)
    6.         {
    7.             audioSource.Stop(); // or disable it
    8.             // any relevant trigger or callback
    9.         }
    10.     }
    11. }
    Also you could leverage the AudioMixer and use Snapshots to dial down your music channel. Here's a good tutorial on the matter: