InverseLerp for vector3

Hi

I am currently working on a 3D slider that will be placed inside the game world itself.
I am wondering if there were a method similar to

public static float InverseLerp(float a, float b, float value);

but for vectors specifically for Vector3. I have sort of a work around. The sliders that i currently have placed are all working parallel the world unity axis, Vector.up, right and forward.
The solution that i have right now for world axis sliders output the slider value like so:

        public float SliderOutput{
            get{
                switch (RotationAxisChoice) {
                    case Axis.X:
                        return _sliderOutput = Mathf.InverseLerp(_lowerLimit.x, _opperLimit.x, _currentSlidePos.x);
                    case Axis.Y:
                        return _sliderOutput = Mathf.InverseLerp(_lowerLimit.y, _opperLimit.y, _currentSlidePos.y);
                    case Axis.Z:
                        return _sliderOutput = Mathf.InverseLerp(_lowerLimit.z, _opperLimit.z, _currentSlidePos.z);
                }
                return 0f;
            }
        }

this should work for the special case that the slider is perfectly aligned with the world axis, but i would like to do a generic solution that can have the slider orientated in any direction. And also get rid of the switch statement

I had the same problem and stumbled upon your question.

This works as a generic solution:

    public static float InverseLerp(Vector3 a, Vector3 b, Vector3 value)
    {
        Vector3 AB = b - a;
        Vector3 AV = value - a;
        return Vector3.Dot(AV, AB) / Vector3.Dot(AB, AB);
    }

You can of course Mathf.Clamp01() the output if you want to.

This works in other dimensions too, just change Vector3 to Vector2.