Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

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:
    1,088
    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,004
    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:
    9
    Try
    Code (CSharp):
    1. AudioSource.PlayOneShot(audioclip);
     
  5. richardgengle

    richardgengle

    Joined:
    Jul 9, 2020
    Posts:
    19
    try GetKey instead of GetKeyDown
     
  6. richardgengle

    richardgengle

    Joined:
    Jul 9, 2020
    Posts:
    19
    try using GetKey, not GetKeyDown
     
  7. richardgengle

    richardgengle

    Joined:
    Jul 9, 2020
    Posts:
    19
    If you can only open one instance of an audio file at a time... you make many copies of the file, and toggle the space bar to select a different copy to play each time
     
unityunity