- Home /
Trying to move slider based off percentage of screen
So I am trying to modify a slider's value based off of the percentage of the screen that you have moved across. I want if you are at a x pixel less than 100 the slider value will be 0 and if you are a x pixel greater 1820 you will be at max value. If you are at any other value I want the normalizedValue of the slider to be set to FingerXPos/(Screen.width-200) . At least I think that math is right. I want the slider's touch area to start at 100 and end at 1820. If the user's previous x pos is less than the x pos that represents where the handle currently is which I am trying to calculate by doing float CurrentXSliderPos = ((Screen.width-200) * SliderNormalizedValue)+100 and the current finger x pos is greater than the current x slider pos or the previous x pos is greater than the current x slider pos and the current finger x pos is less than the current x pos I update the normalizedValue of my slider. Now my problem is this seems to work great for increasing the slider's value but when I try to decrease it goes up once instead of going down. I'm not sure how to fix it. Any and all help is appreciated. Below is my current code for updating the value of my slider.
private void TPUpdateValue()
{
float SliderNormalizedValue = SliderObj.normalizedValue;
float CurrentXSliderPos = ((Screen.width-200) * SliderNormalizedValue)+100;
if(FingerXPos>=100 && FingerXPos<=1820 && ( (PrevFingerXPos < CurrentXSliderPos && FingerXPos >= CurrentXSliderPos) || (PrevFingerXPos > CurrentXSliderPos && FingerXPos <=CurrentXSliderPos) ) )
{
SliderObj.normalizedValue = FingerXPos / (Screen.width-200);
}
else if (FingerXPos < 100 && ( (PrevFingerXPos < CurrentXSliderPos && FingerXPos >= CurrentXSliderPos) || (PrevFingerXPos > CurrentXSliderPos && FingerXPos <= CurrentXSliderPos) ) )
{
SliderObj.normalizedValue = 0;
}
else if (FingerXPos >1820 && ((PrevFingerXPos < CurrentXSliderPos && FingerXPos >= CurrentXSliderPos) || (PrevFingerXPos > CurrentXSliderPos && FingerXPos <= CurrentXSliderPos) ) )
{
SliderObj.normalizedValue = 1;
}
}
Answer by OreoSplitter · May 23, 2019 at 06:58 AM
This looks like a mess. Maybe try https://docs.unity3d.com/ScriptReference/Camera.WorldToViewportPoint.html
Your answer
Follow this Question
Related Questions
How to move slider same amount with different max values when moving it by a percentage Unity 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