- Home /
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);
}
}
}
Answer by Captain_Pineapple · May 16, 2019 at 08:28 AM
I guess you should remove all multiplications by 100 and change the division by the sliderObj.maxValue to a multiplication.
just a guess though, not sure if i totally understood what you are trying to achive.
float percentangeChange = (PercentageOfScreenMoved * SliderObj.maxValue);
if (DistanceMoved < 0)//slide to the left
{
SliderObj.value -= (percentangeChange);
}
else
{
SliderObj.value += (percentangeChange);
}
Your answer
Follow this Question
Related Questions
Trying to move slider based off percentage of screen 1 Answer
How to get float value based on the element size? 0 Answers
How to make if limit is reached then make sliders only slide back in ui (not forward) 2 Answers
How do I move my train with a slider, and not W & S 1 Answer
Problem with UI slider to make it increment other variables 1 Answer