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
  3. Dismiss Notice

Audio Audio Cutting Off When the Camera Looks in a Certain Direction

Discussion in 'Audio & Video' started by SomeGuy22, Dec 1, 2019.

  1. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    Hi, I'm having a really weird issue with the AudioSource component and I just can't seem to find the source of the problem. I'm starting to think it's a potential bug with Unity but I just wanted to see if anyone else has encountered this.

    The Problem

    I'm spawning an object that has an AudioSource as a child of it, and I'm calling AudioSource.Play() after setting the AudioClip to one of my assets. Here is the relevant code:

    Code (CSharp):
    1. if(effects.sliceEffectSource)
    2.         {
    3.            
    4.             if(effects.sliceEffectSource.isPlaying)
    5.             {
    6.                 effects.sliceEffectSource.Stop();
    7.             }
    8.            
    9.             effects.sliceEffectSource.clip = effects.startSliceClip;
    10.             effects.sliceEffectSource.Play();
    11.  
    12.         }
    I've ensured that this is the only code that actually effects the AudioSource. I do have another script which pauses it when the game is paused, but the issue persists even with that script removed. The object itself is spawned with Instantiate and then afterwards the function which has the above code is run. I figure it's pretty standard so I don't think the code itself is breaking anything, especially since it behaves as expected in certain conditions. Here's the settings for the component:



    The problem is that the sound plays perfectly fine as expected--only when the Camera (which has the AudioListener) is facing a certain way or close to a certain part of the scene. Otherwise, the audio appears to get cut short or clipped or something. The clip is either being manipulated for some reason with a different pitch or it gets corrupted or something. It seems to be related to the origin of the scene because it's happened across multiple scenes with varying amounts of objects. The problem starts happening when I move far away from the origin, though it's really hard to say because it's somewhat random. It doesn't happen all the time, but it happens fairly consistently when under the conditions I described. I've uploaded a video to show what I'm dealing with:



    Notice how up until 8 seconds the sounds play as expected--that's pretty much how they sound in the Inspector when previewing the asset. But when I teleport far away the sounds start getting corrupted when looking towards the tower. When I look away it appears to stop having that issue.

    What I've Investigated

    • I've checked the settings of the spawned object at runtime to ensure everything is the same as the prefab, and it is.
    • I've tried changing the clip that it sets it to, and the issue still happens with other clips.
    • I've tried adjusting every setting in the AudioSource prefab that I spawn--priority, pitch, spread, reverb zone mix, doppler. None of it helps the issue.
    • I'm not getting any warnings about approaching the voice limit, nor should I since it is practically the only sound playing.
    • I've debug.Logged if the AudioSource is virtual at the time of playing, and determined that it is indeed false. The audio is not being culled.
    • I've tried resetting the AudioSource.time to 0 in case the clip is being skipped for some reason, but that didn't help.
    • I've tried using a Coroutine to wait for .1 seconds before playing in case the spawn lag is causing it to stumble, but that didn't help.
    • I've tried stopping and playing multiple times in succession but it still had the issue.
    • I've checked the Audio Mixer and verified that the target channel is receiving the sound the same way I hear it--when it gets cut off the levels are shorter.
    The only setting which seems to make it stop is if I set it to a 2D source. When I do that, I hear it play the full sound. But the 3D part is still getting cut off if I have a mix between 2D and 3D. But obviously that's not going to work for me since I need it to be in 3D.

    I just have no clue what's going wrong here, I thought I checked everything but I could be wrong. Any suggestions would be appreciated, thanks!
     
    doarp likes this.
  2. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    I'm still unable to find any reason why the clip would play like this...
     
  3. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    I still have no clue what's wrong. I've tried replacing the clip with a longer sound and I still hear an obvious click under the same conditions I described above. I've tried completely replacing the AudioSource with a new one in case it got corrupted or something but it still had the same issue. I've also ensured that the clip is not being played more than once using Debug statements on the isPlaying condition.
     
  4. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    I've confirmed that the issue still happens on a build for Windows, it's not just in the Editor. I tried adjusting the DSP buffer in the audio settings but none of the options made any difference.

    So I did some trial and error with the audio file itself. These were the settings I had it on:


    • Not preloading the data did nothing to help
    • Neither did changing the compression format to ADPCM
    • The sample rate setting also did nothing, optimizing it or manually setting it to 48k does nothing.
    However, when I changed the load type to "Streaming" something changed. I caught onto it due to this question which was asked. When I set it to streaming, I still hear the click/glitch occasionally but it's very faint and only for a brief moment before the rest of the audio plays correctly. It's almost the same as if I set the audio source to half 2D and half 3D--I hear both the correctly and incorrectly played sounds.

    Again, what's odd is that all of this seems to stem from the camera being oriented a certain way. It happens on multiple scenes that I've tested in and only on this specific instantiated object for some reason. I could do more tests in a blank project to narrow down the limits but I really want to avoid wasting even more time on this. My best guess however is that it's just some quirk of the audio system in play here, and something about having it in memory instead of being streamed is causing it to fail the start of playing.

    The streaming trick is at least some sort of workaround for now, but it's not ideal at all. I can still hear the glitch for starters, even if it's hardly noticeable for those not looking for it. I also really don't want this sound to be streamed from disk, I'd like it to be either compressed in memory or decompressed on load. Introducing a streamed audio file would add unnecessary hard drive slowdowns and could cause it to play incorrectly or lag the game on older machines--at least that's my theory having suffered through years of a poor hard drive on laptop and noticing how these kinds of issues pop up. So it's still up in the air what could be happening here, I'm really hoping to find some answers.
     
    Last edited: Jan 10, 2020
  5. georgestpds

    georgestpds

    Joined:
    Jun 29, 2021
    Posts:
    6
    Hi, I know this post is old but I think you might find your solution (if you haven't already) in the Animator component of this GameObject. Just make sure the Culling Mode isn't set to Cull Completely.
    I had the exact same issue and this is what fixed it. My guess is that sound is also something that is culled, not only visuals. I didn't know. Hope this helps!
     
  6. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    Strange, I wouldn't think that it should work like that but I guess you never know. I don't think the object I was spawning had an Animator component either but I could be mistaken. I'll try it if I ever open the project and find that it does have an Animator, thanks!

    On the bright side I haven't really encountered this issue in any newer versions of Unity (2020 LTS+) so a suggestion to anyone stumbling across this is to try updating if you're still on 2019 LTS.
     
  7. ilmarihak

    ilmarihak

    Joined:
    Nov 16, 2020
    Posts:
    1
    i'm having exactly the same isue on 2023.2.18f1, so very weird, only happening when looking towards a certain direction.