|
Hey Guys, I could really use your help understanding what is wrong with my code. I'm trying to make an audio fading in out system that can be controlled with these two Booleans; If i start the level with either one selected, it moves as predicted, sliding the value smoothly between 0.00 and 1.00 over a time period, but if I turn on and off the booleans there after, or wait until it is at run time to enable and disable them, it just jumps between 0 and 1. I'm using a similar system for my GUI fades, and I'm having the exact same issue, what is wrong with the way this is written, and how can i fix it? Thanks in advance for your help!
(comments are locked)
|
|
Thanks for all your help, and for taking the time. Sadly I still couldn't get it working with either of those, and things just got so much worse when I started using .deltaTime. So I have reluctantly written an ugly 'ghetto script,' to fit the need. And it works perfectly. Thanks again for your help anyway! I've never heard a script be described as being 'ugly' and 'ghetto' before ... :D
Apr 07 '12 at 07:17 AM
Kleptomaniac
(comments are locked)
|
|
Yes, that's because you're using Time.time as the t parameter in your Mathf.Lerp functions. Because Time.time is the amount of seconds since runtime started and the t parameter is based on a range from 0 to 1, this Mathf.Lerp will only work in the first second of the game. This is further explained (and displays a better way of doing Lerp functions) here. The problem here is that the docs are very misleading, and will probably always remain very misleading. They don't really provide adequate information on the topic, and as such I usually use really roundabout methods to linearly interpolate between values because I still don't understand how to fully get Mathf.Lerp to work. :/ Le sigh ... Hope that helps, Klep
(comments are locked)
|
|
Rather than changing boolean to start the fade In/Out, you could use coroutines. Following code is in C#, but you can easily translate it into JS (or ask someone to do it).
(comments are locked)
|
|
@Kleptomaniac is right: the docs show horrible Lerp examples. You could use something like this:
var minimum = 0.00;
var maximum = 1.00;
private var tFadeIn: float = 1.0;
private var tFadeOut: float = 1.0;
private var duration: float;
function Awake(){
audio.volume = 0.0; // avoid sound glitches during initialization
FadeIn(2.0); // fade in the sound during 2 seconds
}
function Update (){
if (tFadeIn < 1.0){
tFadeIn += Time.deltaTime/duration;
audio.volume = Mathf.Lerp(minimum, maximum, tFadeIn);
}
if (tFadeOut < 1.0){
tFadeOut += Time.deltaTime/duration;
audio.volume = Mathf.Lerp(maximum, minimum, tFadeOut);
}
}
function FadeIn(dur: float){
tFadeIn = 0.0;
duration = dur;
}
function FadeOut(dur: float){
tFadeOut = 0.0;
duration = dur;
}
Just call the function FadeIn(t) to make the sound fade in in t seconds, or FadeOut(t) to make it fade out.
(comments are locked)
|
|
Is there a way to do this so that the fade out is associated with a specific audio file (AudioSource) instead of a single one for all sounds in the object? In other words, if I have multiple sounds playing and I want to trigger one sound to fade out but not the other one, how can that be done? I have an example below:
(comments are locked)
|
