Audio Glitching on CameraShake

I have a question.

SInce the PlayClipAtPoint is spawning at the camera position, when CameraShake is called the camera rapidly moves around the point where the audio is playing. This makes the audio change from left to right channel every frame.

Is there a way to go around this? Is there a way in Unity to play sounds directly into the audio-output instead of playing them in 2d or 3d space?

Hi Solitar86,

I’m afraid there is no workaround with PlayClipAtPoint. However, you could create a normal AudioSource component and set its Spatial Blend to 2D. And to be able to play multiple audio clips, you use PlayOneShot.

That audio source could be attached to the camera or a new game object. You just have to reference it in your other objects that are supposed to trigger a sound.

Did this help?


See also:

It’s good to know that this cannot be fixed (in this instance) I did notice that when using an AudioSource this did not happen as the music did not glitch out while the camera was shaking.

I did come across another limitation in an earlier course where I attempted to rapidly play the same sound several time (gunfire) and vary the pitch slightly on every play. If a sound is playing and you adjust a random pitch to the audiosource that is playing it it sounds really weird. So you would need severel sources to play sound with random pitch differences.

I solved this (earlier) by manually spawning a new gameobject with an audiosource and parenting it to the camera so the motion of the camera did not affect it. This of course is quite performance heavy.

Well, you don’t have to spawn a new AudioSource object each time you want to play a sound. A common solution for “reusable objects” is “object pooling”. If you know that you need multiple AudioSource components to make your idea work, create multiple AudioSources and keep them. Then write an AudioSourceHandler class which checks which AudioSource is not playing any sound when you want to play a sound, and play that sound with that “free” AudioSource.

That would be a performant solution. A bunch of AudioSource objects in the scene are no problem. Destroying and creating reusable objects over and over again could be a problem because the garbage collector will become fairly busy.

I’ve looked into ObjectPooling. Haven’t tried it since it hasn’t been relevant with any of these lectures that I’ve been taking here on GameDev.TV but it doesn’t seem that difficult.

So far these prototypes have been so light that even spawning new objects hasn’t been that big of an issue but I must get into this sooner or later.

That’s right. Our games are so small that optimising the performance is not that important. Trying to implement an object pool yourself could be a nice challenge, though. :slight_smile:

Privacy & Terms