Walking Sound Script not working properly (JavaScript)

I’m fairly new to Unity so I have some problems. So I need Help with some things.
I have been hacking around this Grounded variable for a while and finally got it working but now the audio only stops and doesn’t start playing any more when I touch the ground. also all my Keypresses override my old keypress (which is proably what stops the audio from restarting when touching the ground) for example:

  1. I’m holding down the “w” key: audio starts playing (as it should)

  2. while holding down the “w” key I start holding down the “d” key: *audio restarts from beginning

  3. I then let go of the “d” while still holding the “w” key: audio stops

    #pragma strict

    var isGrounded : boolean = true;
    var controller : CharacterController = GetComponent(CharacterController);
    var Sound : AudioClip;

    function Start () {

    }
    function Update () {
    if(!controller.isGrounded){
    {
    audio.clip = Sound;
    audio.Stop();
    }

    } else {
    
    if((Input.GetKeyDown("w")||Input.GetKeyDown("a")||Input.GetKeyDown("s")||Input.GetKeyDown("d")) && isGrounded)
    {
      audio.Play();
    }
    
    if((Input.GetKeyUp("w")||Input.GetKeyUp("a")||Input.GetKeyUp("s")||Input.GetKeyUp("d")) && isGrounded)
    {
       audio.Stop();
    }
    

    }
    }

I hope somebody can help.
Thanks in advance!

First and foremost you need an extra check, make sure that the sound is not ‘already’ playing before you play it:

if (... && !audio.isPlaying) audio.Play();

But that won’t totally fix it, so let’s check out what you said and try to go from there:

1- “while holding down the “w” key I start holding down the “d” key: *audio restarts from beginning” - That’s right, that’s what you told it to here:

if((Input.GetKeyDown("w") || Input.GetKeyDown("a") || Input.GetKeyDown("s") ||   
    Input.GetKeyDown("d")) && isGrounded) {
    audio.Play();
}

Which basically translates to: “any time I press ‘w’, ‘a’, ‘s’ or ‘d’ and I’m grounded” → play the sound from the beginning.

2- “I the let go of the “d” while still holding the “w” key: audio stops”
That’s right again, that’s what you told it to do here:

if((Input.GetKeyUp("w") || Input.GetKeyUp("a") || Input.GetKeyUp("s") ||
   Input.GetKeyUp("d")) && isGrounded) {
   audio.Stop();
}

Which basically says: “If I let go off of ‘w’, or ‘a’, or ‘s’, or ‘d’ and I’m grounded” → stop the sound.

So let’s get your comparison operators right, and knock out your confusion about Input.GetKey/Input.GetButton family.

  1. Input.GetButtonDown/Input.GetKeyDown
    returns true on the first frame you
    press button on, and then as frames
    progress, holding down the button,
    Input.GetButtonDown/Input.GetKeyDown
    will return false.
  2. To say “if I’m holding X button” use
    Input.GetButton/Input.GetKey
    instead, which will return true
    while the button is being pressed.
  3. Input.GetButtonUp/Input.GetKeyUp
    returns true the moment you let go
    of the key (key goes up)

More info on Input.GetKey and Input.GetButton families can be found here.

Armed with the information in your possession now, you should be able to fix the problem and know what went wrong with your approach :slight_smile:

In case you’re lazy here’s how it could look like:

function Update()
{
    var isMoving = Input.GetKey("w") || Input.GetKey("a") || Input.GetKey("s") || Input.GetKey("d");

    // which translates to: "If I'm holding 'w', 'a', 's' or 'd' -> I'm moving"

    if(isMoving && controller.isGrounded)
    {
       if (!audio.isPlaying)
       {
          audio.Play();
       }
    }
    else audio.Stop();
}