Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by DoctorSauce · Nov 05, 2012 at 08:51 PM · audiosoundwalkingrunning

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)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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();
     }
 }
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DoctorSauce · Nov 06, 2012 at 01:54 AM 0
Share

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!

avatar image DoctorSauce · Nov 06, 2012 at 01:56 AM 0
Share

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!

avatar image AlucardJay · Nov 06, 2012 at 02:02 AM 0
Share

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 :

http://www.unifycommunity.com/wiki/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?

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 )

avatar image DoctorSauce · Nov 10, 2012 at 11:39 AM 0
Share

There you go, I'll go do that for your other answer too. Thanks for letting me know how to do that.

avatar image AlucardJay DoctorSauce · Nov 10, 2012 at 11:46 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Making sounds wait for each other to finish 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges