Search Unity

Walking sound effect keeps looping

Discussion in 'Scripting' started by Vexer, May 16, 2018.

  1. Vexer

    Vexer

    Joined:
    Feb 24, 2016
    Posts:
    187
    Hey guys for some reason my code doesn't work:
    Code (csharp):
    1.  
    2.  if (Input.GetKeyDown(KeyCode.W))
    3.         {
    4.             isWalking = true;
    5.         }
    6.  
    7.         if (!Input.GetKeyDown(KeyCode.W))
    8.         {
    9.             isWalking = false;
    10.         }
    11.  
    12.         if (isWalking)
    13.         {
    14.             audioSource.clip = walk;
    15.  
    16.             audioSource.Play();
    17.         }
    18.  
    First of all NO! i don't have play on awake on
    second thing yes i do have loop on

    what iam trying to create is that when i press wasd my audioclip called "Walk" plays but if i don't press any of these buttons i want the sound effect to stop but now for some reason my iswalking keeps being true please someone help me!
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,175
    First of all, getkeydown only gets called the moment, you press that button, so you either want to have getkey (and please use else statement then) or you want to change the other if to getkeyup instead. Second of all, if you call audioclip.Play(), in order, to stop it, you need to call audiosource.stop()
    It seems like, you're new to coding. I would advice you to learn coding first and then start with unity or to watch the unity official tutorials, they are great
     
  3. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    2,100
    I wouldn't really recommend your looping approach, but if you want to do it that way you'll need to call audioSource.Stop at some point or it will play forever. The problem there is you might stop the audio source while it's in the middle of playing some audible sound, which would be a weird effect.

    The way I do this is to increment a variable while my movement key is down. When the variable gets large enough, I play the audio source once, and reset the variable. (Looping on the audio source should be disabled.) Something like this:

    Code (CSharp):
    1. private float _timeSinceLastStepPlayed;
    2.  
    3. void Update() {
    4.     if (Input.GetKey(KeyCode.W)) {
    5.         _timeSinceLastStepPlayed += Time.deltaTime;
    6.         if (_timeSinceLastStepPlayed > 1) {
    7.             _timeSinceLastStepPlayed = 0;
    8.            audioSource.Play()
    9.         }
    10.     }
    11. }
    Another advantage is that it make it simple to choose how rapidly to play the sounds. For this to work, your clip (or clips) need to only play a single footstep, not a long series of steps. If you have multiple clips, you can assign a random clip to your audio source each time before you play it.
     
  4. Vexer

    Vexer

    Joined:
    Feb 24, 2016
    Posts:
    187
    Thx for your answer i have this code now:
    Code (csharp):
    1.  
    2.  
    3. if (Input.GetKeyDown(KeyCode.W) && isGrounded)
    4.         {
    5.             audioSource.clip = walk;
    6.             audioSource.Play();
    7.         }
    8.        
    9.         else if (Input.GetKeyUp(KeyCode.W))
    10.         {
    11.             audioSource.Stop();
    12.         }
    13.  
    14.         else if (Input.GetKeyDown(KeyCode.A) && isGrounded)
    15.         {
    16.             audioSource.clip = walk;
    17.             audioSource.Play();
    18.         }
    19.  
    20.         else if (Input.GetKeyUp(KeyCode.A))
    21.         {
    22.             audioSource.Stop();
    23.         }
    24.  
    25.         else if (Input.GetKeyDown(KeyCode.S) && isGrounded)
    26.         {
    27.             audioSource.clip = walk;
    28.             audioSource.Play();
    29.         }
    30.  
    31.         else if (Input.GetKeyUp(KeyCode.S))
    32.         {
    33.             audioSource.Stop();
    34.         }
    35.  
    36.         else if (Input.GetKeyDown(KeyCode.D) && isGrounded)
    37.         {
    38.             audioSource.clip = walk;
    39.             audioSource.Play();
    40.         }
    41.  
    42.         else if (Input.GetKeyUp(KeyCode.D))
    43.         {
    44.             audioSource.Stop();
    45.         }
    46.  
    if have this code now but sometimes when i press w after d for example too fast no sound will be played.. how can i fix this?
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,713
    I liked the idea about creating your own timing. However, I think you could simplify your code:
    Code (csharp):
    1. bool soundPlaying = false;
    2.  
    3. void Update() {
    4.    float h = Input.GetAxisRaw("Horizontal");
    5.    float v = Input.GetAxisRaw("Vertical");
    6.  
    7.    if(!grounded || (h == 0 && v == 0))
    8.    {
    9.       audioSource.Stop();
    10.       soundPlaying = false;
    11.    }
    12.    else if(!soundPlaying)
    13.    {
    14.       audioSource.Play();
    15.       soundPlaying = true;
    16.    }
    17.  }
    Note: there is an 'isPlaying' property on the AudioSource, too, which could have been used in lieu of the bool my script created.