Search Unity

Audio Volume slider control and output volume

Discussion in 'Audio & Video' started by edufissure, Jan 27, 2019.

  1. edufissure

    edufissure

    Joined:
    Oct 25, 2018
    Posts:
    66
    Hi, ive in menu pause the options to change master volume, sfxvolume and music volume using a slider.

    Ive followed the official unity tutorials, using a AudioMixer, and values of the slider from -80 as min value to 0 max value. The problem im facing is that almost at 50% the sound is so low that i cant hear anything. The sound is working ok as i watch the channels movement.... but seems that a better value would be from -40 to 0... or -20 to 10..... Seems that at -20 is imperceptible. The same happens with other channels...

    Do you change this values ? Or just its the way unity works ?

    Thanks
     
  2. JLF

    JLF

    Joined:
    Feb 25, 2013
    Posts:
    139
  3. edufissure

    edufissure

    Joined:
    Oct 25, 2018
    Posts:
    66
    Thanks id test it later... but its exactly what i was looking for...Half slider its not half volume...
     
  4. Docaroo

    Docaroo

    Joined:
    Nov 7, 2017
    Posts:
    82
    When converting from float to decibels for mixer fader volumes you need to take the float and do this calucation:

    log10(float)*20
     
  5. edufissure

    edufissure

    Joined:
    Oct 25, 2018
    Posts:
    66
    I need help with this i dont know why the sound isnt displayed.. in the editor the audio source is enabled and the correct audio clip is choosed....but im desperate....

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6.  
    7. public class GameOverMenu : MonoBehaviour {
    8.  
    9.     // Use this for initialization
    10.     public static bool isFinished = false;
    11.     public Text infoDisplay;
    12.  
    13.     public GameObject gameOverMenuUI;
    14.     public GameObject audioHolder;
    15.     private PlayerCharacterManager player;
    16.     private string sinfoDisplay;
    17.     private Animator _animator;
    18.  
    19.     private IEnumerator coroutine;
    20.     private AudioSource _audio;
    21.     public AudioClip win;
    22.     public AudioClip loose;
    23.  
    24.  
    25.     void Start () {
    26.         gameOverMenuUI.SetActive(false);
    27.    
    28.         player =GameObject.FindObjectOfType<PlayerCharacterManager>();
    29.         _animator= player.GetComponent<Animator>();
    30.  
    31.         //_audio = audioHolder.GetComponent<AudioSource>();
    32.         _audio = player.GetComponent<AudioSource>();
    33.     }
    34.  
    35.     void Update () {
    36.         if( player.getGameResult() == 3 || player.getGameResult() == 4 || player.getGameResult() == 5 )
    37.         {
    38.             Debug.Log("GAme over in GameOverMenu");
    39.                coroutine = GameOver();
    40.             StartCoroutine(coroutine);
    41.            
    42.         }
    43.  
    44.  
    45.     } //End update
    46.  
    47.     public void Restart () {
    48.         //Resume works inversa as Pause
    49.         //In more than one scene we can select to begin again in the same scene
    50.         Time.timeScale = 1.0f;
    51.         SceneManager.LoadScene (1);
    52.  
    53.     } //End resume
    54.  
    55.     public void QuitGame () {
    56.        
    57.         Debug.Log("Quitting game....");
    58.         Application.Quit();
    59.      }
    60.      public void StopSounds()
    61.     {
    62.         _audio.enabled = false;
    63.         _audio.enabled = true;
    64.     }
    65.     private IEnumerator GameOver()
    66.  
    67.     {
    68.        
    69.         if( player.getGameResult() == 3 )
    70.             {
    71.                 infoDisplay.text = " Congrats !! You won" ;
    72.                 _animator.SetInteger("gameResult",1);
    73.                 _audio.clip = win;
    74.                 _audio.Play();
    75.                 yield return new WaitForSeconds(_audio.clip.length+1.0f);
    76.                 StopSounds();
    77.             }
    78.         else if( player.getGameResult() == 4 )
    79.             {
    80.                 infoDisplay.text = " Enemy killed you !! " ;
    81.                 _animator.SetInteger("gameResult",2);
    82.                 _audio.clip = loose;
    83.                 _audio.Play();
    84.                 yield return new WaitForSeconds(_audio.clip.length+1.0f);
    85.                 StopSounds();
    86.             }
    87.         else if( player.getGameResult() == 5 )
    88.             {
    89.                 infoDisplay.text = " You run out of time" ;
    90.                 _animator.SetInteger("gameResult",2);
    91.                 _audio.clip = loose;
    92.                 _audio.Play();
    93.                 yield return new WaitForSeconds(5.0f);
    94.                 StopSounds();
    95.             }
    96.         //_audio.clip = loose;
    97.         //_audio.Play();
    98.         //yield return new WaitForSeconds(_audio.clip.length+2.0f);
    99.         //StopSounds();
    100.         //AudioSource.PlayClipAtPoint(win,player.transform.position);
    101.         //StopSounds();
    102.        
    103.         //yield return new WaitForSeconds(_audio.clip.length+2.0f);
    104.         float timeAnim = _animator.GetCurrentAnimatorStateInfo(0).length;
    105.         //_animator.SetInteger("gameResult",1);
    106.          yield return new WaitForSeconds(timeAnim);
    107.         //yield return StartCoroutine(Delay(4.0F));
    108.         //StopSounds();
    109.        
    110.         Time.timeScale = 0.0f;
    111.        
    112.         gameOverMenuUI.SetActive(true);
    113.         //player.
    114.         //return null;
    115.        
    116.  
    117.     }  
    118.  
    119.     private IEnumerator Delay(float waitTime)
    120.     {
    121.         yield return new WaitForSeconds(waitTime);
    122.         print("WaitAndPrintGameOverMenu " + Time.time);
    123.     }
    124. } // End Class
    In gameover script i play an animation ( dancing or dying) and want to reproduce a sound ( you win or you loose)....

    This other similar code works perfectly:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. //using UnityEngine.Audio;
    5.  
    6. public class PlayerCharacterElyEnemy : MonoBehaviour {
    7.  
    8.     // Use this for initialization
    9.     private int _health;
    10.     private Animator _animator;
    11.     private AudioSource _audio;
    12.     public AudioClip touch;
    13.     public AudioClip dead;
    14.  
    15.    
    16.     void Start () {
    17.         //Initialize the health value
    18.         _health=3;
    19.         _animator = GetComponent<Animator>();
    20.         _audio = GetComponent<AudioSource>();
    21.     }
    22.    
    23.     // Update is called once per frame
    24.     //era void pero para poder hacer yield necessito inemuretaor
    25.     public void Hurt( int damage)
    26.     {
    27.         _health = _health-damage;
    28.         Debug.Log("Health : " + _health);
    29.         if(_health <= 0)    _animator.SetBool("ElyEnemyDies", true);
    30.         StartCoroutine(TestHurt());  
    31.  
    32.     }
    33.    
    34.     public void StopSounds()
    35.     {
    36.         _audio.enabled = false;
    37.         _audio.enabled = true;
    38.     }
    39.     private IEnumerator  TestHurt () {
    40.        
    41.         //Destroys the element this.
    42.         //this.GameObject refers to the object the script is attached to.
    43.         //this only refers to this script component
    44.         if( _health <= 0)
    45.         {
    46.         //    _animator.SetBool("ElyEnemyDies", true);
    47.             _audio.clip = dead;
    48.             _audio.Play();
    49.             this.gameObject.GetComponent<ElyEnemyMovementAI>().SetAlive(false);
    50.             yield return new WaitForSeconds(5.0f);
    51.             Destroy(this.gameObject);
    52.         }
    53.         else
    54.         {  
    55.             _audio.clip = touch;
    56.             _audio.Play();
    57.             //AudioSource.PlayClipAtPoint(touch,transform.position);
    58.             yield return new WaitForSeconds(_audio.clip.length+2.0f);
    59.             StopSounds();
    60.         //    yield return new WaitForSeconds(_audio.clip.length+2.0f);
    61.             //_audio.Stop();
    62.             //yield return new WaitForSeconds(1.5f);
    63.             //this.gameObject.transform.Rotate(0f, 75, 0);
    64.         }
    65.     }
    66. }
    67.  
    In this case an enemy get touched or die, and produce different sounds depending on it...

    Any help would be apreciated
     
  6. JLF

    JLF

    Joined:
    Feb 25, 2013
    Posts:
    139
    In the first script is the Coroutine firing repeatedly in Update? If AudioSource.Play() gets called in Update, it's unlikely you'll hear anything from it, or just glitching.
     
  7. edufissure

    edufissure

    Joined:
    Oct 25, 2018
    Posts:
    66
    Thanks for your help, this was insane....finally after a hole afternoon trying things....seems this solves the issue....dont know exactly way. Sure there could be a better solution, but this actually works....

    Unity Playoneshot didnt work on the audiosource, so the only solution was to create a "use and throw away" audio source....and assure is only executing once...


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6.  
    7. public class GameOverMenu : MonoBehaviour {
    8.  
    9.     // Use this for initialization
    10.     public static bool isFinished = false;
    11.     public Text infoDisplay;
    12.  
    13.     public GameObject gameOverMenuUI;
    14.     public GameObject audioHolder;
    15.     private PlayerCharacterManager player;
    16.     private string sinfoDisplay;
    17.     private Animator _animator;
    18.  
    19.     private IEnumerator coroutine;
    20.     //private AudioSource _audio;
    21.     public AudioClip win;
    22.     public AudioClip loose;
    23.     private bool isAudioPlaying = false;
    24.  
    25.  
    26.     void Start () {
    27.         gameOverMenuUI.SetActive(false);
    28.    
    29.         player =GameObject.FindObjectOfType<PlayerCharacterManager>();
    30.         _animator= player.GetComponent<Animator>();
    31.  
    32.         //_audio = audioHolder.GetComponent<AudioSource>();
    33.         //_audio = player.GetComponent<AudioSource>();
    34.         //_audio = GetComponent<AudioSource>();
    35.     }
    36.  
    37.     void Update () {
    38.         if( player.getGameResult() == 3 || player.getGameResult() == 4 || player.getGameResult() == 5 )
    39.         {
    40.            
    41.                //coroutine = GameOver();
    42.             //StartCoroutine(coroutine);
    43.             if (!isAudioPlaying) {
    44.                 Debug.Log("GAme over in GameOverMenu");
    45.                 StartCoroutine( GameOver());
    46.             }
    47.         }
    48.  
    49.  
    50.     } //End update
    51.  
    52.     public void Restart () {
    53.         //Resume works inversa as Pause
    54.         //In more than one scene we can select to begin again in the same scene
    55.         Time.timeScale = 1.0f;
    56.         SceneManager.LoadScene (1);
    57.  
    58.     } //End resume
    59.  
    60.     public void QuitGame () {
    61.        
    62.         Debug.Log("Quitting game....");
    63.         Application.Quit();
    64.      }
    65.      public void StopSounds()
    66.     {
    67.         //_audio.enabled = false;
    68.         //_audio.enabled = true;
    69.     }
    70.  
    71.      public void StopAllSounds()
    72.     {
    73.         //_audio.enabled = false;
    74.         //_audio.enabled = true;
    75.     }
    76.     private IEnumerator GameOver()
    77.  
    78.     {        
    79.        
    80.         if( player.getGameResult() == 3 && !isAudioPlaying )
    81.             {
    82.                 infoDisplay.text = " Congrats !! You won" ;
    83.                 _animator.SetInteger("gameResult",1);
    84.                 Debug.Log("Sound 3");
    85.                 AudioSource.PlayClipAtPoint(win,player.transform.position);
    86.                 //yield return new WaitForSeconds(_audio.clip.length);
    87.                 isAudioPlaying = true;
    88.             }
    89.         else if( player.getGameResult() == 4 && !isAudioPlaying)
    90.             {
    91.                 infoDisplay.text = " Enemy killed you !! " ;
    92.                 _animator.SetInteger("gameResult",2);
    93.                
    94.                 Debug.Log("Sound 4");
    95.                 AudioSource.PlayClipAtPoint(loose,player.transform.position);
    96.                 //yield return new WaitForSeconds(_audio.clip.length);
    97.                 isAudioPlaying = true;
    98.             }
    99.         else if( player.getGameResult() == 5 && !isAudioPlaying)
    100.             {
    101.                 infoDisplay.text = " You run out of time" ;
    102.                 _animator.SetInteger("gameResult",2);
    103.                 Debug.Log("Sound 5");
    104.                 AudioSource.PlayClipAtPoint(loose,player.transform.position);
    105.                 //yield return new WaitForSeconds(_audio.clip.length);
    106.                 isAudioPlaying = true;
    107.             }
    108.         //_audio.clip = loose;
    109.         //_audio.Play();
    110.         //yield return new WaitForSeconds(_audio.clip.length+2.0f);
    111.         //StopSounds();
    112.         //AudioSource.PlayClipAtPoint(win,player.transform.position);
    113.         //StopSounds();
    114.         //StopAllSounds();
    115.         //yield return new WaitForSeconds(_audio.clip.length+2.0f);
    116.         float timeAnim = _animator.GetCurrentAnimatorStateInfo(0).length;
    117.         //_animator.SetInteger("gameResult",1);
    118.          yield return new WaitForSeconds(timeAnim+1.0f);
    119.         //yield return StartCoroutine(Delay(4.0F));
    120.         //StopSounds();
    121.        
    122.         Time.timeScale = 0.0f;
    123.        
    124.         gameOverMenuUI.SetActive(true);
    125.         //player.
    126.         //return null;
    127.     //    StopAllSounds();
    128.        
    129.  
    130.     }  
    131.  
    132.     private IEnumerator Delay(float waitTime)
    133.     {
    134.         yield return new WaitForSeconds(waitTime);
    135.         print("WaitAndPrintGameOverMenu " + Time.time);
    136.     }
    137. } // End Class
    138.