0
\$\begingroup\$

In the class PlayerControl I have this

if (Input.GetKey("space")) 
    {           
        soundManager.PlayBleep();
        sword.SetActive(true);
    }

In my soundmanager I have this

   public void PlayBleep()
{
    if (!audio.isPlaying) {
        audio.PlayOneShot(SwordWhip2);          
    }   
}

At the moment I am getting the sound like 10 times each time I press space creating horrible soundeffects. How could I get it not to play until the sound has finished?

\$\endgroup\$
1
\$\begingroup\$

PlayOneShot does not alter isPlaying. See here. So isPlaying will always be false.

A simple fix is to change your input to only trigger once. Instead of Input.GetKey("space") you can use Input.GetKeyDown("space").

You can alternatively add your own flag for when the weapon is active. This additional flag would likely be useful for the animation and other time delays to keep the sword from swinging too fast.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Yeah thats what i did, using a flag. So i guess its a bug? \$\endgroup\$ – Daarwin Oct 12 '13 at 15:22
  • \$\begingroup\$ See my edited answer. \$\endgroup\$ – MichaelHouse Oct 12 '13 at 16:25
  • \$\begingroup\$ You could also use a coroutine. Basically at the start of the coroutine set a local bool (playingSound) to true, then use play one shot, do a yield return wait sound.length, and finally set your bool back to false. This way you only trigger the coroutine once and it will just ignore all other play one shot calls. \$\endgroup\$ – Benjamin Danger Johnson Oct 13 '13 at 6:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.