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 inglipX · Mar 30, 2013 at 12:06 AM · audio

Stuck on this! Please help me out.

I've been trying to be implement a breathing system in which walking has its own audio clip, sprinting has its own and after sprinting has a tired breath audio clip.

I managed to get Horizontal and Vertical movement to play the walkBreathing audio but when I attempt to make another if statement for sprinting i try to do if(Input.GetButtonDown("Sprint") && Input.GetButtonDown ("Vertical") || ("Horizontal"). This screws the audio up and over lays the walk audio with the run audio.... How can i fix this and make a functioning system here???

 var walkBreathing : AudioClip;
 
 var sprintBreathing : AudioClip;
 
 var aftersprintBreathing : AudioClip;
 
 var isWalking : boolean = false;
 
 var isSprinting : boolean = false;
 
 var isAfterSprint : boolean = false;
 
 var isIdle : boolean = false;
 
  
 
 function Start()
  
 {
 
    
     gameObject.AddComponent(AudioSource);
 
 }
 
  
 
 function Update ()
 {
 
 
 
  
 
     if(Input.GetButtonDown("Vertical") || Input.GetButtonDown( "Horizontal" ))
     {
 
     
 
         audio.clip = walkBreathing;
 
         audio.Play();
 
         yield WaitForSeconds (audio.clip.length);
         
         isWalking = true;
      }    
      else if(Input.GetButtonUp("Vertical") || Input.GetButtonUp( "Horizontal" ))
      {    
         
         isWalking = false;
         isSprinting = false;
         isIdle = true;
         is AfterSprinting = false;
  
 
      }
 
  
 
      if(Input.GetButtonDown("Vertical") && Input.GetButtonDown("Sprint"))
      {
 
         audio.clip = sprintBreathing;                
 
         audio.Play();
 
         yield WaitForSeconds (audio.clip.length);
         
         isSprinting = true;
         
         isIdle = false;
         
         isWalking = false;
         
         isAfterSprinting = false;
     
         
      }
     else if(Input.GetButtonUp("Sprint") && isIdle = true)
     {    
         isSprinting = false;
         
         isAfterSprinting = true;
         
         isWalking = false;
         
         isIdle = true;
         
         audio.clip = aftersprintBreathing;
         
         audio.Play();
         
         yield WaitForSeconds (audio.clip.length);
         
         audio.loop = false;
     
     }
 
 }
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

Answer by iwaldrop · Mar 30, 2013 at 12:33 AM

Within your existing check for walking also check for sprinting and set the sound accordingly.

 if(Input.GetButtonDown("Vertical") || Input.GetButtonDown( "Horizontal" ))
 {
     if (Input.GetButton("Sprinting"))
         audio.clip = sprintBreathing;
     else
         audio.clip = walkBreathing;
 }

Also, you should implement a flag that keeps the moving state so that you only call audio.Play() when the sound changes. You should have as little as possible going on in your Update() loop. Use helper methods to get the work done for you, and then only call them when needed! :)

Comment
Add comment · Show 3 · 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 iwaldrop · Mar 30, 2013 at 12:35 AM 0
Share

Or, since you may be using the isSprinting flag for other things, just do this first:

 isSprinting = Input.GetButton("Sprinting");

Then check isSprinting for everything that you need.

avatar image iwaldrop · Mar 30, 2013 at 12:45 AM 0
Share

Another note:

Ideally you have an InputController that gets your input and makes references for other scripts to use.

Take your audio, for instance. You don't want to check Input.GetButton() for every script that needs to know about it, because that takes longer. Your InputController script will check Input.GetButton() and do clever things with it, like set a Vector2 to store the value of the axis, and set flags like isSprinting and is$$anonymous$$oving for scripts like your AudioController to use.

Then you AudioController just needs a reference to your InputController and check inputController.isSprinting to change between the walking and running clips. At the same time your $$anonymous$$ovementController can check InputController.movementVector for it's input values!

If you want to get super fancy, use delegates and events to let the AudioController and $$anonymous$$ovementController know when they should act.

Note: InputController, AudioController, and $$anonymous$$ovementController are just example names; you may wish to call them something else.

avatar image inglipX · Mar 30, 2013 at 01:01 AM 0
Share

so like this would work?

 var walkBreathing : AudioClip;
  
 var sprintBreathing : AudioClip;
  
 var aftersprintBreathing : AudioClip;
  
 var isWalking : boolean = false;
  
 var isSprinting : boolean = false;
  
 var isAfterSprint : boolean = false;
  
 var isIdle : boolean = false;
  
  
  
 function Start()
  
 {
  
  
     gameObject.AddComponent(AudioSource);
  
 }
  
  
  
 function Update ()
 {
  
  
  
  
  
     if(Input.GetButtonDown("Vertical") || Input.GetButtonDown( "Horizontal" ))
 {
     if (Input.GetButtonDown("Sprint"))
         audio.clip = sprintBreathing;
     else
         audio.clip = walkBreathing;
 }
      else if(Input.GetButtonUp("Vertical") || Input.GetButtonUp( "Horizontal" ))
      { 
  
        isWalking = false;
        isSprinting = false;
        isIdle = true;
        is AfterSprinting = false;
  
  
      }
  
  
  
      if(Input.GetButtonDown("Vertical") && Input.GetButtonDown("Sprint"))
      {
  
         audio.clip = sprintBreathing;                
  
         audio.Play();
  
         yield WaitForSeconds (audio.clip.length);
  
        isSprinting = true;
  
        isIdle = false;
  
        isWalking = false;
  
        isAfterSprinting = false;
  
  
      }
     else if(Input.GetButtonUp("Sprint") && isIdle = true)
     {  
        isSprinting = false;
  
        isAfterSprinting = true;
  
        isWalking = false;
  
        isIdle = true;
  
        audio.clip = aftersprintBreathing;
  
        audio.Play();
  
        yield WaitForSeconds (audio.clip.length);
  
        audio.loop = false;
  
     }
  
 }

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

11 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

Related Questions

Multiple Cars not working 1 Answer

Change Music When GameObject is Grabbed? 1 Answer

Can anyone help me invert this simple script? 2 Answers

Trying to simply play this animation... Help please. 1 Answer

Native visual scripting? 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