Hello,
I’m currently working on a project for a Course learning basic Unity. The project has you making and controlling a rocket. My issue is a lack of understanding of how the math is done for an IEnumerator Fade Out function I found on this forum by “Borris1998”( Fade Out Audio Source ). It works great to prevent audible popping when stopping the rocket thrust audio clip and I have worked out exactly how it works except for what happens to a negative value that seamlessly for no reason turns positive before getting set to the audioSource.volume
The included picture shows the line of code doing the operation, where the (startVolume == 1, Time.deltaTime == 0.0125953, and FadeTime == 0.1) with the subtraction of audioSource.volume being 1 I get -0.874047 but the .volume is set to positive 0.874047.
So my question is what happens to the Negative value and want turns it positive?
You mean you want to offer basic Unity course while you don’t understand basic Unity and C#? Or I’m misunderstanding something…
Please, if you post code on the forums, use CODE tags. Details are here: https://discussions.unity.com/t/481379
Reading code from dark, mushy, screenshots is not pleasant.
I’m taking a course not teaching one and this is my first post so I’m not really accustomed on how to use this site yet.
thanks for the heads up.
In my post I state that .volume is somehow getting set to Positive 0.874047, but doing the math after getting the one side of the equation at 0.125953 then subtracting the audioSource.volume that is set at 1 the math gets Negative -0.874047, but the audioSource.volume is being set to the Positive equivalent 0.874047
(P.S. the 0.0125953 for the deltaTime is just the first value I got so I’m just sticking with doing the math with that)
// Note audioSource.volume is set to 1 in unity
// FadeTime is set to 0.1f
// an example of one value from deltaTime would be
// 0.0125953
IEnumerator FadeOut(AudioSource audioSource, float FadeTime)
{
float startVolume = audioSource.volume;
while (audioSource.volume > 0)
{
audioSource.volume -= startVolume * Time.deltaTime / FadeTime;
print(audioSource.volume);
yield return null;
}
audioSource.Stop();
audioSource.volume = startVolume;
}
Thank you for your reply although it didn’t quite hit the mark on what issue I’m currently posting about, I also state the variables and their values above the included picture. I understand and agree with the use of the code tags and will do so for now on.
This means: set the volume to the value: get the actual volume (first it is 1), substract 0.125953. Which is exactly 0.874047. It is not negative. In the next frame it will get the 0.874047 and subtract ~0.125953 (or slightly different) value from it. And so on and so forth.
Thank you for the kind words, It never accord to me to directly test the values I was getting by removing the variables in question. (regardless of not being the cause, simplifying the equation made it easier to see my mistake) I see now that when I did the math and got the 0.125953 by starting on the right of the equation, I continued doing the equation from the right so I was doing 0.125953 - 1 instead of correcting and starting from the left giving 1 - 0.125953 = 0.874047
I will remember this lesson and get to work practicing my math again right away.
Thank you for helping me find this mistake.
-Duncan
P.S. I apologize for any passive aggressive bull I said, bad habit I’m trying to fix and will improve on it. (especially with my amateur expertise on discussed topic)