- Home /
Music play when MOVING..
Hi so,
What i want is music to play whenever the player is moving.. Here is my code.
#pragma strict
var music = false;
function Start () {
}
function Update () {
if(Input.GetKeyDown(KeyCode.W)||Input.GetKeyDown(KeyCode.A)||Input.GetKeyDown(KeyCode.S)||Input.GetKeyDown(KeyCode.D))
{
audio.Play();
}
if(Input.GetKeyUp(KeyCode.W))
{
if(Input.GetKeyUp(KeyCode.A))
{
if(Input.GetKeyUp(KeyCode.S))
{
if(Input.GetKeyUp(KeyCode.D))
{
audio.Stop();
}
}
}
}
The problem i have is that the music restarts everytime the player presses W A S D even when the player is still moving. I wanted it so when any of thos keys are down the music keeps on playing and when they are ALL UP then it stops.
Thanks
Comment
EDIT: Thanks for the replies however none of them seemed to play music atall? O.o
Answer by RedDevil · Aug 12, 2014 at 12:20 PM
if(Input.GetKeyDown(KeyCode.W))
{
audio.Play();
}
else if(Input.GetKeyDown(KeyCode.A))
{
audio.Play();
}
else if(Input.GetKeyDown(KeyCode.S))
{
audio.Play();
}
else if(Input.GetKeyDown(KeyCode.D))
{
audio.Play();
}
else audio.Stop();
Answer by radiKal07 · Aug 12, 2014 at 12:26 PM
Try this:
if(Input.GetKeyDown(KeyCode.W)||Input.GetKeyDown(KeyCode.A)||Input.GetKeyDown(KeyCode.S)||Input.GetKeyDown(KeyCode.D)) { audio.Play(); }
else { audio.Stop(); }
Your answer
Follow this Question
Related Questions
Music On/Off Switch 2 Answers
Singletons in music script? 2 Answers
How do you add player music to your game? 1 Answer
Trying to Play my Particle System OnCollisionEnter Script 1 Answer