- Home /
Footstep sounds play when im not moving or if im running, but stop when i move,
#pragma strict
var walk : AudioClip;
var run : AudioClip;
var isWalking : boolean = false;
var isRunning : boolean = false;
function Update()
{
GetState();
PlayAudio();
}
function GetState() //asks is the player running or walking?
{
if ( Input.GetAxis( "Horizontal" ) || Input.GetAxis( "Vertical" ) )
{
if ( Input.GetKey( "left shift" ) || Input.GetKey( "right shift" ) )
{
isWalking = false;
isRunning = true;
}
else
{
isWalking = true;
isRunning = false;
}
}
else
{
isWalking = false;
isRunning = false;
}
}
function PlayAudio()
{
if (isWalking == true)
{
audio.Play();
}
}
i havent put a running sound in yet. But when I start the game, the audio automatically plays. when i start walking, a weird flatline sound plays, but when i run, i hear the walking audio again. help??
Answer by RaptureFace · Jul 20, 2014 at 03:13 PM
That sound that you hear on the begining is you checked the box that sez PlayOnAwake In Audio Source.
The script is easy to make...Just need to know a bit of Scripting.... Heres the script... Its in JS so...
Make a new script call it Footsteps.
Open the script.
Paste the code Bellow.
Put it in the Camera.
Add your sounds. And Enjoy.
Script:
#pragma strict
var walk : AudioClip;
var run : AudioClip;
var isWalking : boolean = false;
var isRunning : boolean = false;
function Update()
{
GetState();
PlayAudio();
}
function GetState()
{
if ( Input.GetAxis( "Horizontal" ) || Input.GetAxis( "Vertical" ) )
{
if ( Input.GetKey( "left shift" ) || Input.GetKey( "right shift" ) )
{
// Running
isWalking = false;
isRunning = true;
}
else
{
// Walking
isWalking = true;
isRunning = false;
}
}
else
{
// Stopped
isWalking = false;
isRunning = false;
}
}
function PlayAudio()
{
if ( isWalking )
{
if ( audio.clip != walk )
{
audio.Stop();
audio.clip = walk;
}
if ( !audio.isPlaying )
{
audio.Play();
}
}
else if ( isRunning )
{
if ( audio.clip != run )
{
audio.Stop();
audio.clip = run;
}
if ( !audio.isPlaying )
{
audio.Play();
}
}
else
{
audio.Stop();
}
}
Enjoy man :) Good Luck.
I did a modification of my script to include a sprint timer : http://answers.unity3d.com/questions/596645/limited-sprint.html
Your answer
![](https://koobas.hobune.stream/wayback/20220613135321im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Random footsteps 5 Answers
Footstep Sound Error toSample > fromSample 1 Answer
Strange Bug or Code Error with footsteps? -1 Answers
Horrible footstep looping problem 2 Answers
Help Please with sound argh 2 Answers