Search Unity

Repeat Sound Effect In Unity

Discussion in '2D' started by lauritsbrok, Apr 18, 2018.

  1. lauritsbrok

    lauritsbrok

    Joined:
    Apr 4, 2018
    Posts:
    1
    I have a code that plays a sound every time the player presses space. However, the sound stop and plays a new again if you press the space button twice. I want the sound to be able to overlay each other if the space button is pressed twice.

    This is my code:
    void Update () {
    if (Input.GetKeyDown(KeyCode.Space))
    MusicSource.Play();
    }

    Thanks!
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    This thread confuses me. It's a question about audio that was posted in the 2D forum and given the tag "graphics".

    But anyway, use the IsPlaying AudioSource function. Check if the audio is not playing before calling
    MusicSource.Play();
     
  3. sngdan

    sngdan

    Joined:
    Feb 7, 2014
    Posts:
    1,154
    If I understand you correct you want to start the sfx playback on space and continue any previously started sfx until finished. In this case you need a new Audiosource for each sfx. Likely best to use a pool
     
  4. Kudorado

    Kudorado

    Joined:
    Nov 28, 2015
    Posts:
    10
    Try
    Code (CSharp):
    1. AudioSource.PlayOneShot(audioclip);