- Home /
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();
}
}
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.
Answer by Goremaster · Mar 02, 2015 at 04:40 PM
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
Your answer
Follow this Question
Related Questions
How can i set audio mixer groups volume to be used with ui slider when 0 is lower and 80 louder ? 1 Answer
Envelope in OnAudioFilterRead method 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Why the audio sound effects of the main menu are not hearing like in mute ? 0 Answers