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 Poulpc · Apr 24, 2013 at 02:28 PM · audiofadetimescale

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

     if (Input.GetKeyUp("p")|| (Input.GetButtonDown("Fire2"))) {
        if(paused == true){
        paused = false;
           }else{
        paused = true;
                
        }
       
         if(paused == true){
         Time.timeScale = 0.0;
        pausedGUI.enabled = true;
        rigidbody.isKinematic = true;
           audio.volume = 0.05;
         }else{
         
        Time.timeScale = 1.0;
          pausedGUI.enabled = false;
          rigidbody.isKinematic = false;
          audio.volume = 0.2;
          }
          }




Done thx for the help

  if (Input.GetKeyUp("p")|| (Input.GetButtonDown("Fire2"))) {
        if(paused == true){
        paused = false;
           }else{
        paused = true;
 
        }
 
 
         if(paused == true){
         Time.timeScale = 0.0;
        pausedGUI.enabled = true;
        rigidbody.isKinematic = true;
           
         }else{
 
        Time.timeScale = 1.0;
          pausedGUI.enabled = false;
          rigidbody.isKinematic = false;
        
          }
          }
          if(paused == true){
          (VolOut());
          }else{
          (Volin());
          }
 
          function VolOut(){
          while(audio.volume>0.05){
          audio.volume-=0.001;
          yield;
          }
          }
          function Volin(){
          while(audio.volume<0.2){
          audio.volume += 0.001;
          yield;
          }
          }
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
2
Best Answer

Answer by fafase · Apr 24, 2013 at 05:03 PM

Coroutine still runs while timeScale is set to 0. You can then use a variable that is equivalent to deltaTime to be passed to the coroutine.

 void Update(){
    if(pause){
       float t = Time.deltaTime;
       StartCoroutine(VolOut(t));
       Time.timeScale = 0;
    }
 }
 IEnumerator VolOut(float t){
    while(audio.volume>0.2){
      audio.volume-=t;
      yield return null;
    }
 }

EDIT. Js version to be confirmed by someone that really knows about Js

 function Update(){
    if(pause){
       var t = Time.deltaTime;
       StartCoroutine(VolOut(t));
       Time.timeScale = 0;
    }
 }
 function VolOut(t:float){
    while(audio.volume>0.2){
      audio.volume-=t;
      yield;
    }
 }
Comment
Add comment · Show 7 · 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 Poulpc · Apr 24, 2013 at 05:16 PM 0
Share

sorry C# is not me, i just starte using javascript in february, can you translate it ??

avatar image fafase · Apr 24, 2013 at 05:24 PM 0
Share

I can translate it but can you understand it?

I don't mean to be rude or looking down on you but there is no point for you to use code that do not understand yet. You 'd rather use a more simple alternative that you manage but does not do everything you want. Later you get back on it and make it do exactly what you want with a big smile of "I just totally get it".

avatar image Poulpc · Apr 24, 2013 at 05:38 PM 0
Share

oki i no i just starte in this new world, but my game is all most done, http://www.youtube.com/watch?v=NDN2Ul5xT3k newest video of i

avatar image fafase · Apr 24, 2013 at 06:16 PM 0
Share

Ok you seem to learn pretty fast. So the idea there is that t gets the value of deltaTime then VolOut gets called as coroutine and t is passed. Coroutine are not affected by timeScale so even if it 0, it still gets called as usual.

Our t is used to decrease audio.volume each frame by an amount of deltaTime, so it might not be totally accurate but it shuld do the trick.

Each frame the yield return and next frame the function starts again from yield on. As it is in a loop it gos back up until audio.volume is less than 0.2. In this case it gets out of the loop and the function returns.

Note that you will have to figure out a way to set timeScale back to 1 if you want the game to start again. The solution could be OnGUI which is also still called even though timeScale is 0. So you could have a gui button that when pressed sets it back to 1.

This is it

avatar image whydoidoit · Apr 24, 2013 at 06:21 PM 0
Share

@fafase you don't need the StartCoroutine in JS. It's done magically.

Show more comments
avatar image
1

Answer by OpenCoffee · Apr 24, 2013 at 02:34 PM

Hello and welcome to the community!

I searched the forums and found something that you could try:

Unity Answer


If that isn't what you are looking for, please let us know.

Comment
Add comment · Show 4 · 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 Poulpc · Apr 24, 2013 at 02:59 PM 0
Share

no i cant use that, time scale needs to be 1 for that to work i am looking for a way to fade if time scale is 0

avatar image Owen-Reynolds · Apr 24, 2013 at 04:24 PM 0
Share

Right. That method uses "yield WaitForSeconds(..)" and seconds never happen.

(untested) Replacing with "wait one frame" would probably work, which is yield return null;. You'd have to lower the changeBy amount appropriately, since it runs more often.

avatar image Loius · Apr 24, 2013 at 04:31 PM 0
Share

Time.realTimeSinceStartup runs even while timeScale is set to zero.

avatar image Poulpc · Apr 24, 2013 at 05:21 PM 0
Share

i just tryed print(Time.realtimeSinceStartup); and it runs, now to find out have to use it :)

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

Why do AudioMixerSnapshots not crossfade properly when transitioning? 0 Answers

Fading out before load script not functioning correctly? 2 Answers

Activate sound without Pro filters 0 Answers

Adjust audio playback rate with Time.timeScale 2 Answers

Fading Audio in/out with object creation 1 Answer


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