- Home /
Hi, Could someone tell me why this is reversed?
Okay, so this is my footsteps script. Whenever the player is walking in any direction the footstep audio clip is meant to be played while they're walking. For some reason the opposite occurs, whenever the player is stationary the audio clip is playing, and while they're walking the clip doesn't play. I'm not sure why this is happening.
Here's my code:
#pragma strict
var Walking : AudioClip;
var Running : AudioClip;
var PlayerIsWalking : boolean;
function Update () {
if (Input.GetAxis("Horizontal") < 0 || Input.GetAxis("Horizontal") > 0 ||
Input.GetAxis("Vertical") < 0 || Input.GetAxis("Vertical") > 0)
{
PlayerIsWalking = true;
}
else
{
PlayerIsWalking = false;
Debug.Log("NOT MOVING");
}
if (PlayerIsWalking)
{
WalkingAudio();
}
}
function WalkingAudio () {
audio.clip = Walking;
audio.Play();
Debug.Log("WALKING");
}
You're not stopping the audio when idle. Also, you should enclose your multiple ORs for readability in lines 9-10 :)
Audio starts playing like in update function when you walking bool is false it continues to play.
Answer by Umresh · Dec 30, 2014 at 07:55 AM
Try this code
if (PlayerIsWalking)
{
WalkingAudio();
}
else
notWalking();
}
function WalkingAudio () {
audio.clip =Walking;
if(!audio.isPlaying)
audio.Play();
Debug.Log("WALKING");
}
function notWalking()
{
audio.Stop();
}
Your answer
Follow this Question
Related Questions
Character Movement and Jumping Script? 4 Answers
how doI add collision to my character ? 1 Answer
Character Controller Script 3 Answers
Changing Head Look Controller Target 1 Answer
Change sound depending on material 1 Answer