- Home /
Footstep Audio Not Working?
Hi, I'm pretty new to scripting and just using Unity in general, and I've actually been pretty fine until now. I'm trying to make a horror game, and as we all know, sounds are key to this genre. I'm trying to insert footsteps when the player is walking, but for some reason it's not working. I have the script set under the player, and then an Audio Source set under the player as well with the sound "ForestFootsteps.mp3" under Audio Clip. Here's what my script looks like:
var AudioFile : AudioClip;
function Update()
{
if (Input.GetKeyDown (KeyCode.W))
{
audio.clip = AudioFile;
audio.Play();
}
else
{
audio.clip = AudioFile;
audio.Stop();
}
}
And for some reason, it's just not working. It's probably something stupid on my part, cause as I said I'm pretty new haha. But if someone could tell me what I'm doing wrong, or maybe even introduce a better way to script this (I know I kept it pretty basic) that'd be great. Thanks in advance!
It is working, but only for one frame.
check what each of the following commands does :
http://docs.unity3d.com/Documentation/ScriptReference/Input.Get$$anonymous$$ey.html
http://docs.unity3d.com/Documentation/ScriptReference/Input.Get$$anonymous$$eyDown.html
http://docs.unity3d.com/Documentation/ScriptReference/Input.Get$$anonymous$$eyUp.html
The same thing goes for GetButton and Get$$anonymous$$ouseButton.
You want Get$$anonymous$$ey. ( Description : Returns true while the user holds down the key identified by name. Think auto fire. )
The problem you'll have after this is the audio will start playing every frame that the key is held.
So you could use a boolean and Invoke to only play the sound if it is not playing :
var audioFile : AudioClip;
var isPlaying : boolean = false;
function Update()
{
if ( Input.Get$$anonymous$$ey( $$anonymous$$eyCode.W ) )
{
if ( !isPlaying )
{
audio.clip = audioFile;
audio.Play();
isPlaying = true;
Invoke( "HasStoppedPlaying", audioFile.Length );
}
}
else
{
audio.clip = audioFile;
audio.Stop();
isPlaying = false;
CancelInvoke( "HasStoppedPlaying" );
}
}
function HasStoppedPlaying()
{
isPlaying = false;
}
You should have a look at a script I wrote, or check out my videos :
Answer by RaptureFace · Jul 20, 2014 at 03:12 PM
The 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();
}
}
I did a modification of my script to include a sprint timer : http://answers.unity3d.com/questions/596645/limited-sprint.html