- Home /
Switch between two different sounds for walking and sprinting?
So I have two audio files, one for walking and one for sprinting, and I am trying to figure out how to switch between the files depending on if you are walking or sprinting. Here is my script so far:
var walk:AudioClip;
var run:AudioClip;
function Update () {
if ((Input.GetButtonDown( "Horizontal" ) || Input.GetButtonDown( "Vertical" )) && ((Input.GetKey("left shift") || Input.GetKey("right shift")))){
audio.clip=run;
audio.Play();
}
else if ((Input.GetButtonDown( "Horizontal" ) || Input.GetButtonDown( "Vertical" ))){
audio.clip=walk;
audio.Play();
}
else if ((!Input.GetButtonDown( "Horizontal" ) || !Input.GetButtonDown( "Vertical" ))){
audio.Stop();
}
}
When I attach this it doesn't play any sound at all, even when I am walking or sprinting. Any ideas why this might be? (By the way I am using the first person character controller)
Answer by AlucardJay · Nov 06, 2012 at 01:16 AM
I think it is playing, just playing from the start at every frame ( audio = foo; audio.Play(); ), therefore you never hear the full sound play.
First problem : Input.GetButtonDown( "Horizontal" ) should be Input.GetAxis( "Horizontal" )
I would break it down into 3 steps : find if the character is walking or running; store that information in booleans; then check if the correct audio is playing (if not, set audio clip) then Play =]
Here is a tested working 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();
}
}
Holy crap you rock. You've helped me solve two problems now, and you've been the only one who has done it right. You are the best. Thank you so much!
I am really new to scripting, like i've been scripting for a week, and it took me like an hour just to write that script, so you saved me a bunch of time. Thanks again!
No worries, happy to help =]
Just to help you along, here are some links I strongly suggest to all new users :
Unity3Dstudent (videos are about 5 $$anonymous$$s each)
Start at the bottom and work up : http://www.unity3dstudent.com/category/modules/essential-skills/
Start at the bottom and work up : http://www.unity3dstudent.com/category/modules/beginner/
this is the YouTube link for the above as one playlist : http://www.youtube.com/watch?v=-oXYHNSmTxg&list=PL27B696FB515608D2&feature=plcp
the Unity Wiki : http://wiki.unity3d.com/index.php/Tutorials
A list of resources : http://answers.unity3d.com/questions/12321/how-can-i-start-learning-unity-fast-list-of-tutori.html
Helpful page with information on using Built-In Arrays and Lists :
The unity wiki link above is very handy with lots of scripts and shaders too (just check out all the links down the left, and the tabs along the top : http://wiki.unity3d.com/index.php/Scripts )
There you go, I'll go do that for your other answer too. Thanks for letting me know how to do that.
Thankyou, your vote is much appreciated =]
just a final tip to using the 'site (before anyone negative votes ya!)
Please don't post comments as answers. Post comments by clicking the [add new comment] button, a window then open for you to type in. Answer fields are for answers only, as this is a knowledge base.
You can convert this answer to a comment (or just edit your original question), you'll also get a better chance of getting an actual answer if the main list shows none or one answer in blue =]
Your answer
![](https://koobas.hobune.stream/wayback/20220613083406im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Footsteps Script for Running and Walking 3 Answers
Audio Settings Menu 2 Answers
Audio files not playing at all 4 Answers
footstep sound 1 Answer