Search Unity

Audio Audio on Button Press

Discussion in 'Audio & Video' started by nathanhawthorne12, Jan 26, 2019.

  1. nathanhawthorne12

    nathanhawthorne12

    Joined:
    Jan 2, 2019
    Posts:
    29
    Evening folks

    I have a simple 2d scroller. At the moment I have on button press a 1 second sound plays. The problem I have is if someone presses the button many times on keeps there finger on the button, the sound does not get time to play the whole thing, UNTIL after the last button press. Anyone have ideas on per button press that the audio continues its full 1second sound clip? Part of the player input is below Commented out at the moment due to another issue.
    Code (CSharp):
    1. void PcInputs()
    2.     {
    3.  
    4.         if (Input.GetMouseButtonDown(0) || Input.GetButton("Jump"))
    5.         {
    6.             Fart();
    7.             anim.SetBool("1", true);
    8.             anim.SetBool("2", false);
    9.             PlayerFartBar();
    10.  
    11.             //Audio Per touch - NOTE If no sound is recorded then error messages appear TODO
    12.             //AudioSource audioSource = GameObject.Find("EffectAudio").GetComponent<AudioSource>(); //Set the audiosource as EffectAudio
    13.             //audioSource.Play();  //Play what ever sound is on EffectAudio
    14.         }
    15.         else
    16.         {
    17.             anim.SetBool("1", true);
    18.             anim.SetBool("2", false);
    19.  
    20.         }
    21.     }
     
  2. JLF

    JLF

    Joined:
    Feb 25, 2013
    Posts:
    140
  3. nathanhawthorne12

    nathanhawthorne12

    Joined:
    Jan 2, 2019
    Posts:
    29
    Thanks JLF, the problem was the Spacebar code was changed so it is one press rather than constant. Cheers