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 PaxStyle · Apr 12, 2014 at 03:18 PM · audiofade

Audio fade out problem

Hi everyone, I tried to make an audio fade out to my clip, but i don't understand why this code dosn't work. Thanks in advance.

 #pragma strict
  
 var Allarme : AudioClip;
 var audio1Volume : float = 1.0;
  
 function Start () {
  
 }
  
 function Update () {
  
  
 if (Input.GetKeyDown (KeyCode.H)) {
          audio.clip = Allarme;
          audio.Play();
          audio.loop = true;
          }
  
        else if (Input.GetKeyUp (KeyCode.H)) {
          audio.clip = Allarme;
          audio.Stop();
          fadeOut();
            }
  
 }
  
 function fadeOut() {
     if(audio1Volume > 0.1)
     {
         audio1Volume -= 0.1 * Time.deltaTime;
         audio.volume = audio1Volume;
     }
     }
Comment
Add comment · Show 3
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 Scribe · Apr 12, 2014 at 03:28 PM 0
Share

Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.H) is only true for one frame so your fadeOut method will only be called once, as well as that you call audio.Stop(); which will stop playing the audio immediately, try the following:

 var Allarme : AudioClip;
 var audio1Volume : float = 1.0;
 
 function Update () {
     if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.H)) {
         audio.clip = Allarme;
         audio.Play();
         audio.loop = true;
     }
     if(!Input.Get$$anonymous$$ey($$anonymous$$eyCode.H)) {
         fadeOut();
     }
 }
 
 function fadeOut() {
     if(audio1Volume > 0){
         audio1Volume -= 0.1 * Time.deltaTime;
         audio.volume = audio1Volume;
     }else{
         audio1Volume = 0;
         audio.volume = audio1Volume;
     }
 }

This is untested I'm afraid as I'm away from my main computer, you could also do this by having a boolean variable to define when to start fading out but I'm unsure which would be more efficient.

avatar image PaxStyle · Apr 12, 2014 at 04:13 PM 0
Share

this works only one time that i press H, if after i press H again, the audioclip don't hear anymore.

avatar image Scribe · Apr 12, 2014 at 10:34 PM 0
Share

ahh yeah, forgot to reset the volume again when pressing H, just add in setting the volume back to 1 in the first if statement

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by fafase · Apr 12, 2014 at 03:21 PM

You need to use a coroutine. Your code only calls the method once and then wait for another press and each a tiny bit is happening.

 function fadeOut(){
     while(audio.volume > 0){
          audio.volume -= Time.deltaTime;
          yield;
    }
 }
Comment
Add comment · 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

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

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

Related Questions

Smooth GUI and Audio Volume Fades 5 Answers

Fading Audio in/out with object creation 1 Answer

Unity crashes when it tries to play a sound! 1 Answer

Audio Fade With Toggle 0 Answers

Help with Audio Fade 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