Probably an easy question but I can’t really find an answer. What I want is to change the pitch and volume of an Audiosource based on two floats.
Say I’ve got 3000 (float A) to 6000 (float B). How can I say that I want the pitch to go from 1 to 1.5 between A and B? And probably much of the same, but how do I do the same for volume? I was looking at MathF.Lerp but that factors in the time, which has nothing to do with what I’m trying to do. I already know how to change pitch, volume and other audio properties with a script, so the question is really about changing certain properties between A and B.
The t parameter in all Lerp functions doesn’t actually represent time, and is probably exactly what you’re looking for.
Rather, the t parameter should be any number between 0 & 1, and it specifies how much of the two provided values mix, where:
A value of 0 = exactly the first parameter.
A value of 1 = exactly the second parameter.
A value of 0.5 = exactly the half-way between the two parameters.
Example:
//value = 3000
float value = Mathf.Lerp(3000f, 6000f, 0f);
//value = 6000
float value = Mathf.Lerp(3000f, 6000f, 1f);
//value = 4500
float value = Mathf.Lerp(3000f, 6000f, 0.5f);
This function will map any input range to any output range, including reversing direction. So it’s basically like Mathf.Lerp, but you don’t need to convert your input range to (0.0-1.0), it does it for you. (Still want it in the Mathf library…)
The key is that once the input range is converted to (0-1), you can multiply that range to get the correct output range, then add the offset (out1).