- Home /
sound effect problem for rapid fire weapon.
Update - Thanks to everyone who replied. The problem was with the audio clip itself, adding a fade in and out removed the stutter.
I'm able to hook up a audio file/sound effect to my gun firing but there's this weird audio stutter when the weapon goes full auto. The audio seems to play fine but its like there's a weird side effect to playing a audio file in very quick succession, or playing a new audio file before the first is finished.
function LateUpdate()
{
if(can_shoot)
{
//fire pew pew
if(shot_sound)
{
audio.Stop();
audio.clip = shot_sound;
audio.Play();
}
}
}
I have no experience with audio and the sounds I'm using are the best i could build in Audacity using tones.
Any help or information would be appreciated.
Answer by oliver-jones · Apr 05, 2011 at 01:33 PM
Its because you have placed your audio code within an Update (which is called every frame) - so essentially, your audio is being played every frame.
You want to do something like this:
var audioPlay : boolean = true;
function LateUpdate(){
if(can_shoot && audioPlay == true){
GunAudio();
} }
function GunAudio(){ audioPlay = false; audio.Stop(); audio.clip = shot_sound; audio.Play(); yield WaitForSeconds(shot_sound.length); audioPlay = true; }
Hope this helps
Thanks for the reply. $$anonymous$$y audio file is only played when "can_shoot" is true, like when the player presses the mouse button down or when a set amount of time elapse, so its not playing every frame. The rate of fire on the weapon is 0.1 seconds, and its when the weapon fires at this speed that i get this audio problem.
In that cases its because your audio clip is over lapping each other. I'll update my post later - I'm in a rush at the moment.
Answer by Statement · Apr 05, 2011 at 01:34 PM
You may consider using audio.PlayOneShot.
function LateUpdate()
{
if(can_shoot)
{
//fire pew pew
if(shot_sound)
{
audio.PlayOneShot(shot_sound);
}
}
}
This will cause another sound to play even if another sound is playing. If you're playing sound too often, you might get a very loud experience with so many sounds layered on top of each other - and a performance drop.
If you were to put this in an update function will it still repeat?
yes I know some games that have this problem ( Civilization IV even does with collatoral damage from cavlry on siege unit stacks being resolved to late) Still game of the millinium though ^_^. $$anonymous$$y point is just that you should really avoid that, it ruins game experience being attacked by sound
@Proclyon Civilization IV game of the millinium...try Halo 2 :)
Thanks for the reply even if the topic got a little derailed...There's no game of the millennium because i haven't built it yet.
Your answer
Follow this Question
Related Questions
Making sounds wait for each other to finish 2 Answers
Unity wont play audio in editor, but will when the game is built. 2 Answers
Car engine sound 1 Answer