Decreasing a value as another value increases through script

Hello,

I would like to know how to program a line of code that when my first value (x) decreases, my second value (y) increases in a constant ratio. It should be in the ratios like so:

X : Y
100 : 0.15

50 : 0.3

0 : 0.45

Any help is appreciated.

Thank you.

the easy way to do this is to use Properties when setting a value of first change the value of the other

if x is always going to be between 100 and 0 you could implement Y as a simple property

public float Y
{
get
{
return ((100 - X) * 0.003f + 0.15f);
}
}

One way could be using InverseLerp.

    private float x;
    private float y;

    public float X
    {
        get { return x; }

        set
        {
            float tx = Mathf.InverseLerp(0f, 100f, value);
            y = Mathf.Lerp(0.45f, 0.15f, tx);
            x = value;
        }
    }

    public float Y
    {
        get { return y; }

        set
        {
            float ty = Mathf.InverseLerp(0.45f, 0.15f, value);
            x = Mathf.Lerp(0f, 100f, ty);
            y = value;
        }
    }

You could use an AnimationCurve. It’s by far the easiest way to handle arbitrary value mapping.