Search Unity

Change pitch and volume based on two floats

Discussion in 'Scripting' started by SomerenV, Aug 22, 2020.

  1. SomerenV

    SomerenV

    Joined:
    Dec 20, 2011
    Posts:
    83
    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.

    Thanks!
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    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:
    Code (CSharp):
    1. //value = 3000
    2. float value = Mathf.Lerp(3000f, 6000f, 0f);
    3.  
    4. //value = 6000
    5. float value = Mathf.Lerp(3000f, 6000f, 1f);
    6.  
    7. //value = 4500
    8. float value = Mathf.Lerp(3000f, 6000f, 0.5f);
     
    Kurt-Dekker likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Everything that @Vryken notes above is spot on, but there's also an inverse lerp one that's kinda cool:

    https://docs.unity3d.com/ScriptReference/Mathf.InverseLerp.html

    If you are doing RPM from 3000 to 6000, and the RPM is currently 4500, if you feed those into inverse lerp, you will get 0.5f

    Now you can feed 0.5f into regular Lerp to scale smoothly from 1.0f to 1.5f (your problem space).
     
  4. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    691
    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).

    Code (CSharp):
    1. public float remap(float val, float in1, float in2, float out1, float out2)
    2.     {
    3.         return out1 + (val - in1) * (out2 - out1) / (in2 - in1);
    4.     }
    So for your case, you'd call the function like this, where currentValue is the number between 3000 and 6000 that you want to map:

    Code (CSharp):
    1. currentValue = remap(currentValue, 3000, 6000, 1.0f, 1.5f);