Help with Walking Audio :)

Hey Guys, quick one. I want this walking audio to play continuously if 1 or more keys (w, a, s, d) is being held down without it stopping.

lets say you hold down W (Audio Plays), and then also hold D (Audio is still playing) but then let go of D so only W is being held down but I want the audio to carry on until all keys are lifted.

How can i get it so it continues the audio until all keys are lifted?




public class TwoDimensionalStateController : MonoBehaviour
{

    Animator animator;
    public float isMoving = 0f; 
    
    

    public AudioSource WalkingSource;
    public AudioClip WalkingClip;

    void Start()
    {
        WalkingSource = GetComponent<AudioSource>();
    }


    void Update()
    {

        if (Input.GetKeyDown(KeyCode.W) || (Input.GetKeyDown(KeyCode.S) || (Input.GetKeyDown(KeyCode.A) || (Input.GetKeyDown(KeyCode.D)))))
        {
            isMoving = 1;

            if (isMoving == 1)
            {
                WalkingSource.PlayOneShot(WalkingClip);
            }
        }
        else if (Input.GetKeyUp(KeyCode.W) || (Input.GetKeyUp(KeyCode.S) || (Input.GetKeyUp(KeyCode.A) || (Input.GetKeyUp(KeyCode.D)))))
        {
            isMoving = 0;

            if (isMoving == 0)
            {
                WalkingSource.Stop();
            }
        }

If it helps, you can just play the clip then instantly pause the audio source in start. Then you’re just pausing and unpausing it.

1 Like

Hey dude,

So with this im guessing id have to do the code for each key on a new line then reference if the other keys are being pressed so they dont overlap each other if that makes sense?

Not at all. Just move this to start, and put a pause after it.

WalkingSource.PlayOneShot(WalkingClip);

Add a ‘play’ where it was. and replace your ‘stop’ with ‘pause’

I appreciate the help on this, I’ll give it a shot!

No problems! You actually, you don’t need a lot of the code you have there.

public class TwoDimensionalStateController : MonoBehaviour
{

    Animator animator;
    public float isMoving = 0f; 
    
    

    public AudioSource WalkingSource;
    public AudioClip WalkingClip;

    void Start()
    {
        WalkingSource = GetComponent<AudioSource>();
        WalkingSource.PlayOneShot(WalkingClip);
        WalkingSource.Pause();
    }


    void Update()
    {

        if (Input.GetKeyDown(KeyCode.W) || (Input.GetKeyDown(KeyCode.S) || (Input.GetKeyDown(KeyCode.A) || (Input.GetKeyDown(KeyCode.D)))))
        {
            WalkingSource.Play();
        }
        else
        {
           WalkingSource.Pause();
        }

It may sound a little strange if you abruptly stop the audio mid footstep. Instead you can play a sound of a single footstep and then you don’t need to worry about stopping it. You also can just check the character’s velocity instead of all the keys.

void Update()
{
	if (isGrounded && !WalkinSource.isPlaying && velocity.magnitude>0.5f)
		WalkingSource.PlayOneShot(footStep);
}

Unfortunately this stops the audio all together :frowning:

This method should work as ive done it previously, I’ll watch another tutorial as a refresher. Cheers bud

It shouldn’t do. But either way you don’t need to add an extra check to the code there. The ‘isMoving’ is being set to one, then being instantly checked by the code to see if it is one. It will never not be one in that situation.

Just realised the reason it’s stopping for you is probably that you unticked ‘looping’ on the Audio Source.

Of course, this method may result in half of one step being played, then it being finished the next time you move. It’s up to you if this is distracting or not. If it is, I can talk you through a more complex method involving the single step approach - which is superior and can accomadate multiple surfaces.