I’ve got a scene where when a ball hits the ground it plays a little bounce sound effect.
My code checks for a collision and then uses SendMessage to tell a sound manager object to play the sound. This “technically” works fine but there is a slight delay in the audio. It’s much less than a second but noticeable.
I’ve checked and edited the audio file so there is absolutely no dead space before the sound effect. The only thing I can think of is Unity is causing the delay. I can’t really play the sound just before the collision because I can’t anticipate the collision. It’s triggered off of the collision itself.
How can I remedy this?
The most common cause for this case of very slight audio lag is the compression settings. If the engine has to decompress a sound every time it’s called, then it will lag slightly before it plays. Try using a decompressed .wav file as your sound, and load the sound to ram on your audio asset’s parameter settings. This preps the sound and can be played back instantly with no lag when it is called.
If you are still experiencing a delay - I would suggest you do another 2 quick steps.
Firstly, either look at the waveform using a DAW and see if there is silence within the waveform before the sound starts, or simply try other .wav files to see if they are behaving the same.
Secondly, you can write a quick script to ‘play sound on mouse left click’. Listen for any delay. If you have a delay using this super simple script, and your source .wav files have no initial ‘silence’ in them, then it’s most likely your computers sound card that simply delays any audio coming out of it whatsoever.
I would be happy to look at the waveform if you don’t know how.
Compressing the audio via script was my issue. The GetComponent().Play(); in Unity 5 where the () is the sample rate, the example in the unity.docs. scripting is 44100, this leads to latency as the audio is apparently being compressed on the fly, which creates a delay. I noticed that when I doubled the rate to 96000 it literally doubled the latency. Using no sample rate got rid of my delay. Hope this helps anyone that runs into a similar problem.