Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Audio [SOLVED] Retrigger one sound but let all other run

Discussion in 'Audio & Video' started by Takodan, Apr 3, 2019.

  1. Takodan

    Takodan

    Joined:
    Nov 19, 2017
    Posts:
    6
    Hey,

    I'm making an Arkanoid clone and currently i have two sounds loaded -- one when the ball hits the wall and one when it hits the paddle. If the ball hits a corner wall it sounds like it is playing the same sound twice really quickly since there is a short but noticeable delay and volume increase. This is something i don't want.

    I would like for it to work like different channels.

    If channel 1 is playing a sound and it is triggered again, that channel should stop and re-trigger the sound. Same with all other sounds, that is, a specific sound clip should only be allowed to play if that sound isn't playing. If it is, it should re-trigger.

    I tried doing using the Stop() function, but this seems to stop all sound.

    Code (CSharp):
    1. source = GetComponent<AudioSource>();

    Code (CSharp):
    1.     public static void PlaySound(string clip)
    2.     {
    3.         switch (clip)
    4.         {
    5.             case "start":
    6.                 source.PlayOneShot(startJNGL);
    7.                 break;
    8.             case "startLevel":
    9.                 source.PlayOneShot(levelJNGL);
    10.                 break;
    11.             case "paddle":
    12.                 source.PlayOneShot(paddleSFX);
    13.                 break;
    14.             case "brickLow":
    15.                 if (source.name == "brickLowSFX" && source.isPlaying)
    16.                 {
    17.                     source.Stop();
    18.                     source.PlayOneShot(brickLowSFX);
    19.                 }
    20.                 break;
    21.             case "brickHigh":
    22.                 source.PlayOneShot(brickHighSFX);
    23.                 break;
    24.         }
    25.     }
    Is there a way to start and stop individual sounds instead of all audio?
     
  2. JLF

    JLF

    Joined:
    Feb 25, 2013
    Posts:
    139
    The issue you're having is trying to do it all from one audio source. Stop is an AudioSource function, so stops all of the clips on that AudioSource, not just one clip. The only reason you can even play multiple clips at all from one audio source is because you're using play one shot.

    Try using AudioSource.Play() instead of PlayOneShot. If you call Play on an audio source that's already playing, it cuts it and starts again, which is what you're asking for. It means you'll need to use separate audio sources to play clips simultaneously but will prevent the same clip overlapping when you don't want it to.

    I would move your music clips to different audio sources and then when you trigger the sound effects set the clip on the Audio Source first, then call Play().

    e.g:

    Code (CSharp):
    1. AudioSource source;
    2. AudioClip paddleSFX;
    3.  
    4. source.clip = paddleSFX;
    5. source.Play();
     
  3. Takodan

    Takodan

    Joined:
    Nov 19, 2017
    Posts:
    6
    Superb. I now use two audio sources, each with it's own script, and it works as intended. I have different sounds on both and can stop one without it interfering with the other. Thanks!
     
    Last edited: Apr 4, 2019
    JLF likes this.