- Home /
Walking sound effect on grass
This is very simple, I just need the game to play a sound file when the character is moving then the audio will stop when the player stops. I have this script but the result is not what I expected. It keeps on looping until I release the W key on the first time then it won't play anymore on the second time.
var AudioFile : AudioClip;
function Update()
{
if (Input.GetKeyDown (KeyCode.W))
{
audio.clip = AudioFile;
audio.Playloop();
}
else if (Input.GetKeyUp (KeyCode.W))
{
audio.Stop;
}
}
Any idea what's wrong and what I should do? The code is in JavaScript.
Answer by ganesh.pingale · Apr 16, 2013 at 02:47 PM
var AudioFile : AudioClip;
function Update()
{
if (Input.GetKey (KeyCode.W))
{
audio.clip = AudioFile;
audio.Playloop();
}
else
{
audio.Stop;
}
}
Answer by Iceblitzyt · Apr 16, 2013 at 10:45 AM
I think (Sorry just restarted coding after along while) you need to make:
else if (Input.GetKeyUp (KeyCode.W))
{
audio.clip = AudioFile;
audio.Stop();
}
Sorry for such a late reply. I'm trying it now, and I will reply the results soon enough. Thanks in advance
Okay, here's what happened:
It starts to play at first then it wont play the second time I press W.
Update: Compiler Error says:
$$anonymous$$issing$$anonymous$$ethodException: UnityEngine.AudioSource.Playloop
Answer by Griffo · Apr 16, 2013 at 10:50 AM
Try -
#pragma strict
var AudioFile : AudioClip;
function Update()
{
if (Input.GetKeyDown (KeyCode.W))
{
audio.clip = AudioFile;
audio.Play();
}
else if (Input.GetKeyUp (KeyCode.W))
{
audio.Stop();
}
}
I tried this but it only plays once: every time I press (not hold) W. $$anonymous$$y audio file is only one footstep so as to save me some memory.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
AngryBots MaterialImpactManager Script and FootstepsAudio 1 Answer
Random Footsteps within CollisionSoundEffects Script 1 Answer
Help with Simple Footsteps? 2 Answers
How do I stop footstep sound from playing when I release 'w'? 1 Answer