I have an audiosource attached to my character and the sound file (AK47_Single) is working, but it is sounding like:
ba-ba-ba-ba-ba-bam
and kind of cut off. is there a way to handle this better?
if (defaultAmmo == 0)
{
//play reload sound
if(resetTime == true)
{
timeSinceLastDecrement = 0;
audio.clip = defaultReloadSound;
audio.Play();
resetTime = false;
}
if(timeSinceLastDecrement >= 2)
{
defaultAmmo = 25;
resetTime = true;
}
}
The sound file is just a quick AK47 shot like BAM.
if (!audio.isPlaying())
{
// play sound here.
}
Prevents the clip from stepping on itself.
Another option could be to use multiple AudioSources and run them on rotation. That way you can have overlapping clips, but not have to worry about constantly creating new Audio Sources.
Sample Code
AudioSource[] sources = new AudioSource[]; //Fill this in an awake or start somewhere
int currentIndex = 0;
void PlayClip(AudioClip clip)
{
sources[currentIndex].clip = clip;
sources[currentIndex].Play();
currentIndex = (currentIndex + 1) % sources.Length;
}
More sources take more resources, but allow for more simultaneous clips, you could use the fire rate and clip length to determine what the ideal length of the array is at runtime.
I guess I was mistaken, I have never used it but thought audio.PlayOneShot(AK47); was for non-repeating / looping sounds. Is that wrong?
PlayOneShot still doesn’t check if the sound is already playing.
you could “WaitForSeconds (AudioClip.length)” before every “PlayOneShot”
That would get the sound out of sync with the rest of the game.
Thanks for the tips guys, i will try them out.