- Home /
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:
I'm holding down the "w" key: audio starts playing (as it should)
while holding down the "w" key I start holding down the "d" key: *audio restarts from beginning
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!
You could try something like...
var walking : boolean = false;
if(Input.Get$$anonymous$$eyDown("w")||Input.Get$$anonymous$$eyDown("a")||Input.Get$$anonymous$$eyDown("s")||Input.Get$$anonymous$$eyDown("d"))
{
walking = true;
}
if(Input.Get$$anonymous$$eyUp("w")||Input.Get$$anonymous$$eyUp("a")||Input.Get$$anonymous$$eyUp("s")||Input.Get$$anonymous$$eyUp("d"))
{
walking = false;
}
if(walking){ // checks if walking == true just a shorter form of it
audio.clip = Sound;
audio.Play();
} else {
audio.Stop();
}
[edit] Sorry, just realized that you would still get the same problem with this code so please ignore this and just use the Answer from Vexe
Answer by vexe · Sep 12, 2013 at 07:58 AM
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.
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.To say "if I'm holding X button" use
Input.GetButton
/Input.GetKey
instead, which will return true while the button is being pressed.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 :)
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();
}
Let me know how it goes :) - Did that solve the problem? - If so, please mark the answer as correct to close this question so that others don't think it's still active and we could move on :)
So basicly I make a new variable preventing Audio playing whe it already is, so that's good to know and the I delete all the "Down"s and leave the "Up"s am I getting this right ? I'm going to try it now. Thank you very much.
edit: I'm confused about you having is$$anonymous$$oving and isWalking how does the script know that is$$anonymous$$oving corresponds to isWalking? or is that just a mistake?
No you don't need to create a boolean to deter$$anonymous$$e if the audio is playing or not, it's already a member declared inside the AudioSource class, named isPlaying
And forget about the "Up"s - just try the code I gave you.
I'm sorry if I sound stupid asking this stuff, but I don't only want to get it to work i want to know hw it works since I'm looking forward to keep working with Unity and scriping in general. Thanks!
No need to think you sound stupid :) - We all exist to learn, best learning comes from simple questions and deep understanding :) - But what is it that you're not quite getting? - what is it that you want to know how it works?