Simple audio script glitch

Hello,

I made this script i’ll explain what it does when you press down wsad it plays the audio file of grass walking when i release it stopped well it works only for the w key on release the asd keeping looping the audio file is there something i’m doing wrong?

using UnityEngine;
using System.Collections;

public class PlayerAudio : MonoBehaviour {
	
	public AudioSource Audio;
	public AudioClip WalkingOnGrass;

	// Update is called once per frame
	void Update () {
		if(Input.GetKeyDown("w") || Input.GetKeyDown("s") || Input.GetKeyDown("a") || Input.GetKeyDown("d"))
			audio.Play();
		else if 
		(Input.GetKeyUp("w") || Input.GetKeyDown("s") || Input.GetKeyDown("a") || Input.GetKeyDown("d"))
			audio.Stop(); 
	}

}

Yes, you are being as blind as a bat; no offense.

–

So, there must be something different about the way you're handling the audio.Stop() for the "w" key than the way you do it for every other key on line 14..... what could that be?

–

I know i have spent all day trying to figure it out i know i made a mistake somewhere just can't find it lol(That's why you don't copy and paste kids xD haha)

–

Go and have a coffee and relax for half an hour. When you come back you will spot it straight away.

–

Oh I can't bear this! Maybe inserting some line breaks would help? Here's your line 14: (Input.GetKeyUp("w") || Input.GetKeyDown("s") || Input.GetKeyDown("a") || Input.GetKeyDown("d")) What's different?

–

1 Answer

1

if(Input.GetKeyDown(“w”) || Input.GetKeyDown(“s”) || Input.GetKeyDown(“a”) || Input.GetKeyDown(“d”))
audio.Play();
else if

         (Input.GetKeyUp("w") || Input.GetKeyDown("s") || Input.GetKeyDown("a") || Input.GetKeyDown("d"))
             audio.Stop(); 
     }

You need Input.GetKeyUp ‘SAD’ lol

Thank use for all your help, went and had a coffee and found the error ill remember not to just copy and paste next time. :p

–