Search Unity

playing sound when certain keys are pressed.

Discussion in 'Scripting' started by ChuckieGreen, Mar 9, 2018.

  1. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    358
    I have a standard code set up for movement, which I am trying to add audio to. This is the code I am using

    Code (CSharp):
    1. void StaminaSystem()
    2.     {
    3.  
    4.         {
    5.             if ((Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0) && (Input.GetKey(KeyCode.LeftShift)))
    6.             {
    7.                 CharacterController.speed = 15;
    8.                 AudioClips[1].Play();
    9.             }
    10.  
    11.             if ((Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0) && !(Input.GetKey(KeyCode.LeftShift)))
    12.             {              
    13.                 CharacterController.speed = 2;
    14.                 AudioClips[0].Play();
    15.             }
    16.             if ((Stamina.CurrentVal == 0) && (Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0))
    17.             {
    18.                 CharacterController.speed = 2;              
    19.             }
    20.             if ((Input.GetAxis("Vertical") == 0 && Input.GetAxis("Horizontal") == 0))
    21.             {
    22.              
    23.             }
    24.  
    25.         }
    26. }
    However the issue I am having is, the sound does not start to play until the shift key is toggled. So for the first line
    Code (CSharp):
    1. if ((Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0) && (Input.GetKey(KeyCode.LeftShift)))
    2.             {
    3.                 CharacterController.speed = 15;
    4.                 AudioClips[1].Play();
    5.             }
    Which is for running, the audio does not play until shift is released, and I think that has to do with, GetKey, I have tried GetKeyDown, but that then stopped the movement from working correctly (running and walking would be the same speed, even when moving all the code into the update function). So I am wondering if anyone possibly has an idea of a way I can get this to work correctly?
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    I assume your AudioClips array actually an array of AudioSources?

    I think the issue is that AudioSource.Play() plays from the beginning of the clip. Essentially, you're constantly restarting the clip from the beginning while the Shift key is down. It's only when you release the key the clip completes playing.

    Just check if the AudioSource is already playing (isPlaying) before you Play() it.
     
  3. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    I was thinking it was from multiple sounds on the same source, but I think dgoyette is right.
     
  4. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
  5. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    358
    Sorry I just re-read the comments above and realised I misunderstood. Was just about to remove and repost. :oops:

    When I try the isPlaying i get a issue that says AudioSource[] does not contain a definition for isPlaying. Is it the array thats causing the issues?
     
  6. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    You didn't show your code, but it sounds like you aren't calling an individual audiosource, but the array.
     
  7. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    358
    Yup i was being stupid. was just calling AudioClips[].isPlaying without indicating what sound. Working now :)