- Home /
How Do I make a Walk Sound Repeat?
Hi Guys, I am an Beginner at coding and I have made a script to play a sound when the player presses W, A, S, D But I can't seem to get the sound to repeat if the player holds the button down??? I have tried many ways but just can't do it. Please Help :D
var Walk : AudioClip;
function Update () {
if (Input.GetKeyDown ("w"))
audio.Play();
if (Input.GetKeyDown ("s"))
audio.Play();
if (Input.GetKeyDown ("a"))
audio.Play();
if (Input.GetKeyDown ("d"))
audio.Play();
}
Answer by jimmyismike · Aug 03, 2012 at 10:09 PM
First make sure it knows the key down keyDown = true; then a audio.loop = true; in there then to stop the loop audio.loop = false;
Try something like...
if (Input.GetKeyDown ("What ever key goes here"))
{
audio.Play();
keyDown = true;
audio.loop = true;
}else{
keyDown = false;
audio.loop = false;
}
Note this is just a guess not sure if it will work.
Would I have to stick an "else" statement after audio.loop = true;
function Update () { if (Input.Get$$anonymous$$eyDown ("w")) audio.Play(); audio.loop = true; }
Thank You! I edited it a bit and now it's working!!!
function Update () { if (Input.Get$$anonymous$$eyDown ("w")) { audio.Play(); audio.loop = true; }else{ if (Input.Get$$anonymous$$eyUp ("w")) audio.loop = false; } }
Answer by UnrealIzzy · Aug 03, 2012 at 11:14 PM
The Code Is Done!!!
function Update () {
if (Input.GetKeyDown ("w"))
{
audio.Play();
audio.loop = true;
}else{
if (Input.GetKeyUp ("w"))
audio.loop = false;
}
}
Izzy I voted you up, I hope enough so you can comment without needing a moderator to approve. I can tell your gonna be very useful to community. You actually know how to program.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
BCE0044: expecting }, found 'private'. PLEASE HELP!!!!! 2 Answers
Repeat Function 1 Answer
Unity Web player doesn't finish the install, can you please help me? 0 Answers
change font size help - impossible? 1 Answer