- Home /
Using Mathf.SmoothDamp() in a foreach loop
Can you use Mathf.SmoothDamp()
inside a foreach
loop? As in:
oldSource = GameObject.FindGameObjectWithTag ("backgroundSound").GetComponents<AudioSource> ();
foreach (AudioSource src in oldSource) { src.volume = Mathf.SmoothDamp (src.volume, 0, ref currentVelocity, timeToChange); }
How would one have to deal with the ref currentVelocity
, since all those audio sources might be at a different volumes at that time, and change with a different velocity. Obviously, I can't just take the same variable over and over again but how to do that differently?
Answer by Mike 3 · Jul 31, 2010 at 08:39 PM
You can use the variable over and over if you don't care about the results.
In this case, looks like you don't need them, I'd just leave it as you have it.
Okay, thanks. Just out of curiosity: What exactly is this variable for and what "results" will I get by using the same one over and over again?
It's just an output value telling you the current speed of the values. e.g. if you put in 1 for current and it returns 2, velocity will be 1. You don't need to use it for anything at all really in your case, and reusing it just hides the old unwanted values.
Well I have now the problem that sometimes the src.volume doesn't go to 0, but hovers somewhere around 0.200, even going up again and then staying there, ins$$anonymous$$d of going to 0. This has nothing to do with those re-used values?
Try setting currentVelocity to 0 at the beginning of the loop, see if it fixes it