- Home /
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;
}
}
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;
}
}
sorry C# is not me, i just starte using javascript in february, can you translate it ??
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".
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
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
@fafase you don't need the StartCoroutine in JS. It's done magically.
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:
If that isn't what you are looking for, please let us know.
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
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.
Time.realTimeSinceStartup runs even while timeScale is set to zero.
i just tryed print(Time.realtimeSinceStartup); and it runs, now to find out have to use it :)