尝试根据跨屏幕移动的百分比来移动滑块

时间:2019-05-22 23:16:38

标签: c# unity3d math slider

因此,我试图根据滑过屏幕的百分比来修改滑块的值。我想如果x像素小于100,则滑块值将为0,如果x像素大于1820,则滑块值将为最大值。如果您处于其他任何值,我希望将滑块的normalizedValue设置为FingerXPos /(Screen.width-200)。至少我认为数学是正确的。我希望滑块的触摸区域从100开始并在1820结束。如果用户的上一个x pos小于表示当前手柄位置的x pos,我想通过进行float CurrentXSliderPos =(((Screen.width -200)* SliderNormalizedValue)+100并且当前手指x pos大于当前x滑块pos或先前的x pos大于当前x滑块pos并且当前手指x pos小于当前x pos我更新我的滑块的normalizedValue。现在我的问题是,这似乎对增加滑块的值很有用,但是当我尝试减小滑块时,它一次上升而不是下降。我不确定如何解决。任何和所有帮助表示赞赏。下面是我当前用于更新滑块值的代码。

 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;
         }  
     }

0 个答案:

没有答案