Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Resolved AudioSource.PlayOneShot sometimes stops when playing it fast?

Discussion in 'Audio & Video' started by iSleepzZz, Apr 25, 2021.

  1. iSleepzZz

    iSleepzZz

    Joined:
    Dec 23, 2012
    Posts:
    206
    Hi there, so I've noticed I have my gun shooting at a rate of once every 0.1s and it calls AudioSource.PlayOneShot() to play the gun shot sound.

    However, what's weird is when I hold down the shooting method, which calls AudioSource.PlayOneShot() every 0.1s, it sometimes stops doing the audio clip?

    Anybody else notice this? What methods should I be using to call so fast such as every 0.1s and have it always play the sound properly?

    P.S. the sound is a .wav format
     
  2. JLF

    JLF

    Joined:
    Feb 25, 2013
    Posts:
    140
    How long is the clip? You might want to double-check that you're not hitting the voice limit.

    The default limit is 32, I believe, so if the clip is anywhere near 3.2s long, and you fire it every 0.1s, you'll hit that limit.

    See my blog post on audio optimisation in Unity for more: https://gamedevbeginner.com/unity-audio-optimisation-tips/#voice_limits

    Hope that helps.
     
  3. iSleepzZz

    iSleepzZz

    Joined:
    Dec 23, 2012
    Posts:
    206
    Wow I have never even heard of these "voice limits" in unity, that's crazy haha
    These audio clips are only 1s long (at the maximum), since they are just gun shots, its a very quick 1s clip, however, if me and my friends are shooting at the same time it happens almost everytime. That makes sense because if the default is 32, then all it takes is 32 shots between us both to hit that limit.
    How can I get around this then? Because obviously I need it to make the audio sound every gun shot (other wise its a really bad player-responsiveness haha)
    Do I just need to simply change these values in the Audio Preferences?
    What issues can arrise from me raising these values? (if any)
    And also, what is the PlayOneShot method: is it "real" or "virtual"?

    Thanks again for this info and link! :)
     
  4. iSleepzZz

    iSleepzZz

    Joined:
    Dec 23, 2012
    Posts:
    206
    I changed it to the maximum of 255, and haven't noticed anything out of the ordinary.
    Do you know of any consequences for having this high? haha
     
  5. JLF

    JLF

    Joined:
    Feb 25, 2013
    Posts:
    140
    As far as I understand it, every Play One Shot is a real voice. Real voices are sounds you can actually hear, while virtual voices keep sounds that can't be played (because of a real voice limit) running in the background so that they are playing back correctly once a real voice is available.

    You can increase the voice limit up to 255, but try to aim for much lower than that depending on the target hardware. I couldn't find published hardware limits for my article but around 40 would be a safe bet with up to 80 being on the higher end. The main issue you'd have is a hardware limit say, on console, or a phone, where it's just not as high as you've set it.

    To reduce the impact of the firing I would either reduce the length of the clip or limit it manually somehow, stopping the audio source playback every nth shot for example might work. Or use multiple audio sources with Play() and toggle between them. There's going to be a more elegant way to do it but those are the basic approaches.

    I'd probably try it with a looping clip instead, and then fade it out quickly when the firing stops.

    Hope that helps.
     
    TPEUnity and iSleepzZz like this.
  6. iSleepzZz

    iSleepzZz

    Joined:
    Dec 23, 2012
    Posts:
    206
    Really appreciate all the information and your help!
    Helps a ton, thanks again!