sound overlap, problems with character controller
I'm using the default first person controller in Unity 5 and now I'm getting problems in my game because when I'm wallkind it plays the "m_FootstepSounds" and I use the reloading action for the gun , that goes like this:
if (Input.GetKey(KeyCode.R) && bullets<20 && ammo>0)
{
GetComponent<AudioSource>().clip = reloadclip;
GetComponent<AudioSource>().Play();
Invoke("Reload", 1.0f);
reloading = true;
}
I can't hear the reloading sound. I tried to search before asking but I couldn't solve the problem yet, I will appreciate any attempt to help me.
Hello, This might happen if your audio clip is a 3D sound and your audioSource is far from the audioListenner (listenner is by default on the mainCamera). Uncheck 3D sound option on your reload clip. Also it is safer to have a statement if(!audio.IsPlaying)
before any`Play()` in a function that might be called several times per frames.
As Nerewar stated, AudioSource.Play()
is usually called only one frame. Otherwise it would start playing 30-60 (approx. depending on framerate) times within a second which can result in overlapping of 30-60 sounds.
I suggest you use Input.Get$$anonymous$$eyDown, because you really only need to execute this code once.
thanks for the last advice, I will use it. And talking about the Audio problem, I found a easy solution thanks to a friend of $$anonymous$$e, it's not very elegant but works very well, I put one AudioSource for the reload and other for the shoot ins$$anonymous$$d of the audioclips I call different audiosource variables, and now I can hear everything without overlap problem. Thanks a lot to both of you.
Of course making a new AudioSource for different AudioClips is the solution that works immediately, but in the future you will find it most annoying to manage all the different sounds you might have. But you should figure it out in you own pace, nothing destroys the learning process more than you forcing yourself to learn things you dont need at the moment ;)
No problem, that's what the community is for. You could wait now if anyone else wants to give a more in-depth explanation or if you are satisfied, tell us to close this question.