How to move slider same amount with different max values when moving it by a percentage Unity

I have a slider that’s max value changes based off the length of the currently selected audio clip. I am changing the slider’s value when the user right clicks and moves their mouse left/right in a certain area. I want the slider to move faster/slower based on the distance the user has moved the mouse since the last frame. The problem is if my clip is say 135.35 seconds the slider moves extremely slowly. It only works well with a 1 second clip. I think I need to move it a different percentage based off of my movement but am having a lot of trouble getting the math right. I want the slider to move the same amount of distance for each distance value the user moves their mouse regardless of what the max value is. Any and all help is appreciated. Below is my current code but it is moving way too fast when the clip is 1 second and still a bit slow for a 135.35 second clip.

private void UpdateValue()
    {
        float DistanceMoved = FingerXPos - PrevFingerXPos;
        float PercentageOfScreenMoved = Mathf.Abs(DistanceMoved) / Screen.width;
        if (PercentageOfScreenMoved >= 0.005)
        {
            float percentangeChange = ((100 * PercentageOfScreenMoved) / SliderObj.maxValue);
            if (DistanceMoved < 0)//slide to the left
            {
                SliderObj.value -= (percentangeChange*100);
            }
            else
            {
                SliderObj.value += (percentangeChange*100);
            }
        }
    }

You probably want to keep the length of the slider the same, something simple like 0.0 to 1.0

If you want one full finger width swipe to go from start to finish, the math is something like this:

float DistanceMoved = FingerXPos - PrevFingerXPos;

float Fractional = DistanceMoved / Screen.Width;

SliderObj.value += Fractional;

// (you might want to limit slider here from 0.0 to 1.0; I think it might do so automatically however

Then if you are looking scrub the audio to a particular point, it would be:

myAudioSource.time = sliderObject.value * myAudioSource.clip.length;

And you might want to only do the above scrub if the DistanceMoved value above is nonzero, or above a certain threshhold like your original code did.

And this way, if you want to feed the audio current position back into the slider (so it moves as the music plays), you can just assign it:

SliderObj.value = myAudioSource.time / myAudioSource.clip.length;
1 Like