Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Audio sounds canceling each other

Discussion in 'Audio & Video' started by aetztztztzzew, May 13, 2019.

  1. aetztztztzzew

    aetztztztzzew

    Joined:
    Apr 12, 2018
    Posts:
    20
    Hi!
    basicly i mostly copied code from tutorial but i have problem with sounds canceling after another sound plays. for example when i shoot and play sound if i hit enemy and explosion sound plays shooting sound gets muted.
    i already have this script on empty gameobject soundmanager and access it through soundmanager.instance.playSingle(sound).
    i dont know what else to do to make sounds not cancel each other!
    any help is appreciated


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class soundManager : MonoBehaviour
    5. {
    6.     public AudioSource efxSource;                   //Drag a reference to the audio source which will play the sound effects.
    7.     public AudioSource musicSource;
    8.     public AudioSource crashSource;
    9.     public AudioSource ShootSource;
    10.     //Drag a reference to the audio source which will play the music.
    11.     public static soundManager instance = null;     //Allows other scripts to call functions from SoundManager.            
    12.     public float lowPitchRange = .95f;              //The lowest a sound effect will be randomly pitched.
    13.     public float highPitchRange = 1.05f;            //The highest a sound effect will be randomly pitched.
    14.  
    15.  
    16.     void Awake()
    17.     {
    18.         //Check if there is already an instance of SoundManager
    19.         if (instance == null)
    20.             //if not, set it to this.
    21.             instance = this;
    22.         //If instance already exists:
    23.         else if (instance != this)
    24.             //Destroy this, this enforces our singleton pattern so there can only be one instance of SoundManager.
    25.             Destroy(gameObject);
    26.  
    27.         //Set SoundManager to DontDestroyOnLoad so that it won't be destroyed when reloading our scene.
    28.         DontDestroyOnLoad(gameObject);
    29.     }
    30.  
    31.  
    32.     //Used to play single sound clips.
    33.     public void PlaySingle(AudioClip clip)
    34.     {
    35.         //Set the clip of our efxSource audio source to the clip passed in as a parameter.
    36.         efxSource.clip = clip;
    37.  
    38.         //Play the clip.
    39.         efxSource.PlayOneShot(efxSource.clip);
    40.     }
    41.     public void PlayCrash(AudioClip clip)
    42.     {
    43.         //Set the clip of our efxSource audio source to the clip passed in as a parameter.
    44.         crashSource.clip = clip;
    45.  
    46.         //Play the clip.
    47.         crashSource.PlayOneShot(crashSource.clip);
    48.     }
    49.  
    50.     public void PlayShoot(AudioClip clip)
    51.     {
    52.         //Set the clip of our efxSource audio source to the clip passed in as a parameter.
    53.         ShootSource.clip = clip;
    54.  
    55.         //Play the clip.
    56.         // ShootSource.PlayOneShot();
    57.         ShootSource.PlayOneShot(ShootSource.clip);
    58.     }
    59.  
    60.     //RandomizeSfx chooses randomly between various audio clips and slightly changes their pitch.
    61.     public void RandomizeSfx(params AudioClip[] clips)
    62.     {
    63.         //Generate a random number between 0 and the length of our array of clips passed in.
    64.         int randomIndex = Random.Range(0, clips.Length);
    65.  
    66.         //Choose a random pitch to play back our clip at between our high and low pitch ranges.
    67.         float randomPitch = Random.Range(lowPitchRange, highPitchRange);
    68.  
    69.         //Set the pitch of the audio source to the randomly chosen pitch.
    70.         efxSource.pitch = randomPitch;
    71.  
    72.         //Set the clip to the clip at our randomly chosen index.
    73.         efxSource.clip = clips[randomIndex];
    74.  
    75.         //Play the clip.
    76.         efxSource.Play();
    77.     }
    78. }
    79.  
     
  2. aetztztztzzew

    aetztztztzzew

    Joined:
    Apr 12, 2018
    Posts:
    20
  3. Docaroo

    Docaroo

    Joined:
    Nov 7, 2017
    Posts:
    82
    If you are calling PlaySingle() all the time then it is using the same AudioSource (efxSource) to play all the sounds you are calling ... I thought that with PlayOneShot() that this didn't matter but maybe not ...

    Use a different AudioSource for the shoot sound and the hit sounds in this case!