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
0
Question by Christina1 · Aug 26, 2013 at 12:54 PM · audioif

Audio problem? "Or" syntax question?

Hello everyone,

I hope this is an ok question to ask, I am trying to make a footsteps/jumping audio code. The code I've come up with is below. It works pretty well, the footsteps audio pauses to play the jump audio when the jump button is pushed while the run keys are held.

 var Footsteps: AudioClip;
 var Jump: AudioClip;
 
 
 function Update () {
      
     if (Input.GetButton("Horizontal") || Input.GetButton("Vertical")){        //If pressing the move keys
     if(!audio.isPlaying){                                                    //And audio is not playing
         audio.clip=Footsteps;                                                //Find the Footsteps Audio
         audio.Play();                                                        //And play it
             if(Input.GetButtonDown("Jump")){                                //If the jump key is pressed
             audio.Stop();                                                    //Stop playing footsteps. (Jump audio initiated below
             }
         }
     }
     
     else if ( !Input.GetButton( "Horizontal" ) || !Input.GetButton( "Vertical" ) && audio.isPlaying ){ //Else if not pressing move keys
     audio.Stop();                                                                                        //Stop playing audio
     }
     
     if (Input.GetButtonDown("Jump")){                    //If pressing jump button down
     audio.Stop();                                        //Stop any other Audio
     audio.clip=Jump;                                    //Find the Jump audio clip
     audio.Play();                                        //Play the Jump clip
     }
 
 }


My problem is that if the run keys /aren't/ pressed and the jump audio doesn't play. I've been staring at this for a while and I can't seem to figure it out. My question is am I not understanding the function of "Or" ( || ) correctly? To my untrained eyes it looks like whenever the jump button is pushed the audio should play, regardless of other button pushes.

Thanks for any help!

Christina

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
0
Best Answer Wiki

Answer by ATMEthan · Aug 26, 2013 at 01:05 PM

I believe this line right here

  else if ( !Input.GetButton( "Horizontal" ) || !Input.GetButton( "Vertical" ) && audio.isPlaying ){ //Else if not pressing move keys
 audio.Stop(); //Stop playing audio
 }

stops your audio from playing. Even though you play the jump audio a couple lines after. In the next update, which happens nearly instantly, stops the audio from playing. If I'm reading this correctly your second if will stop all audio if you are not moving left or right and audio is playing, hence if you just jump nothing plays.

I think its how you handle your if statements that's messing you up.

I would structure it the following way, see if this helps out at all.

     if(Input.GetButton("Horizontal") || Input.GetButton("Vertical") 
     //do what u gotta do
     else  if (Input.GetButtonDown("Jump"))
     //do what u gotta do
     else
     //audio.Stop()?
Comment
Add comment · Show 2 · 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 Christina1 · Aug 26, 2013 at 01:16 PM 0
Share

You star that's exactly what was happening! Thanks I'd been staring at that for ages! I've done pretty much what you said and added a little if statement in to see if the audio playing is "Footsteps" or "Jump", works brilliantly.

Thanks so much for the help! X

avatar image ATMEthan · Aug 26, 2013 at 01:44 PM 0
Share

:D Glad I could help!

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

16 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 avatar image avatar image avatar image avatar image

Related Questions

Calling an if statement once even when it is calling every frame? 3 Answers

Calling myfunction from update function 1 Answer

Simple && and || question. 1 Answer

play an audio clip in an if statement 2 Answers

Help with a simple conversation script? 0 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