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 serenefox · Mar 19, 2013 at 04:42 AM · javascriptaudioontriggerenterfade

Audio Fading too fast

I dont know what's worng with my script but I know where its happening, if anyone can help me out I would be very thankful. I know its happening on the "volume -= 0.01 * Time.time;" line but I don't know what to change, as soon as my "TopCameraCollision" hits the trigger, the float value drops very fast so it's like the music doesn't even fade it just stops. Here is my script:

 #pragma strict
 
 private var music : AudioSource;
 private var mainCamera : GameObject;
 var volume : float;
 
 function Start()
 {
     mainCamera = GameObject.FindWithTag("MainCamera");
     music = mainCamera.GetComponent(AudioSource);
     music.enabled = true;
     music.volume = 0.5;
 }
 
 function OnTriggerEnter(collider : Collider )
 {
     if(collider.gameObject.name == "TopCameraCollision")
     {
         Debug.Log("Hit1");
         if(music.volume > 0)
         {
             Debug.Log("Hit2");
             volume -= 0.01 * Time.time;
             music.volume = volume;
         }
         else if(music.volume <= 0)
         {
             Debug.Log("Hit3");
             volume = 0;
             music.volume = volume;
         }
     }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by whydoidoit · Mar 19, 2013 at 05:46 AM

You are using Time.time which is the time since the game began - so you should probably be using Time.deltaTime which is the time of the current frame.

Now the next problem is that you are only doing this when you enter the trigger and only once. So if you switch to Time.deltaTime you will have to enter the trigger multiple times to get the volume to fade. If that's not the effect you are looking for you probably want to consider a coroutine.

   if(music.volume > 0)
   {
        StartCoroutine(FadeMusic(2));
   }

   ...

   function FadeMusic(seconds : float)
   {
       var initialVolume = volume;
       var t = 0;
       while(t < 1)
       {
           t += Time.deltaTime/seconds;
           volume = Mathf.Lerp(initialVolume, 0, t);
           music.volume = volume;
           yield null;
       }
   }
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 serenefox · Mar 19, 2013 at 06:39 AM 0
Share

Thanks for the reply, I was trying to convert your script to java but I failed miserably. I need to do this in java. If you could translate this script or explain to me how it works so I can do it, it would be much appreciated.

avatar image Fattie · Mar 19, 2013 at 07:28 AM 0
Share

serene, look at your original code

where it says Time.time change it to Time.deltaTime

avatar image serenefox · Mar 19, 2013 at 07:35 AM 0
Share

I tried that first, it does the same thing.

avatar image
0

Answer by r67en · Jul 21, 2013 at 06:53 PM

I had the same issue, so I modified whydoidoit's code to solve the problem. The issue had been that the variable 'volume' was never initialized, and t was not initiaized as a float. I removed volume and just directly used music.volume.

 #pragma strict
 
 private var music : AudioSource;
 private var mainCamera : GameObject;
  
 function Start()
 {
     mainCamera = GameObject.FindWithTag("MainCamera");
     music = mainCamera.GetComponent(AudioSource);
     music.enabled = true;
     music.volume = 1.0;
 }
 
 
 function FadeOut(seconds : float)
 {
     var initialVolume:float = music.volume;
     var t=0.0f;
     while(t < 1.0)
     {
         Debug.Log(""+t);
         t += Time.deltaTime/seconds;
         music.volume = Mathf.Lerp(initialVolume, 0, t);
         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

13 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

Related Questions

How can I individually fade the background (and not the GUI)? 1 Answer

how to fade audio if Time.timeScale = 0.0 ?? 2 Answers

Setting Scroll View Width GUILayout 1 Answer

OnTriggerEnter Problem 3 Answers

Help! OnTriggerEnter isn't working. 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