Search Unity

A slider with min max values

Discussion in 'Scripting' started by theinfomercial, Feb 17, 2011.

  1. theinfomercial

    theinfomercial

    Joined:
    Sep 9, 2008
    Posts:
    1,000
    Hi. I'm having a math problem. Here's what I want:

    I have a 3d slider that I made, and it works properly, apart from making it so that the lefthand side equals the min value and the righthand side equals the max value and have them interpolated based on the slider button position on the slider bar.

    How does one do this? Thanks.
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    I assume that you have a min placement and a max placement. as well as a current placement

    Code (csharp):
    1.  
    2. var minPoint : Vector3;
    3. var maxPoint : Vector3;
    4. var currentPoint : Vector3;
    5.  
    6. var minValue : float = 3.0;
    7. var maxValue : float = 9.0;
    8.  
    9. function Update(){
    10. var modifier = Vector3.Distance(minPoint, currentPoint) / Vector3.Distance(minPoint, maxPoint); // should be a number between 0 and 1
    11. var myValue = (maxValue - minValue) * modifier + minValue;
    12. }
    13.  
    14.  
     
  3. theinfomercial

    theinfomercial

    Joined:
    Sep 9, 2008
    Posts:
    1,000
    I gotta ask. How on earth does one know what to write for the math involved? I never would have guessed to use that equation.

    Also, my min/max points are actually based on the slider bar's scale, which is:

    Code (csharp):
    1.  
    2. var slideScale : float = slider.transform.localScale.x
    3.  
    4. //-slideScale * 0.5 for lefthand side
    5. //slideScale * 0.5 for righthand side
    6.  
    Some more help? :)
     
  4. Mike L

    Mike L

    Joined:
    Sep 14, 2010
    Posts:
    1,035
    its rather simple, it says here: http://unity3d.com/support/documentation/Components/gui-Controls.html

    HorizontalSlider
    The HorizontalSlider Control is a typical horizontal sliding knob that can be dragged to change a value between predetermined min and max values.

    Basic Usage
    The position of the Slider knob is stored as a float. To display the position of the knob, you provide that float as one of the arguments in the function. There are two additional values that determine the minimum and maximum values. If you want the slider knob to be adjustable, assign the slider value float to be the return value of the Slider function.

    Code (csharp):
    1. /* Horizontal Slider example */
    2.  
    3. var hSliderValue : float = 0.0;
    4.  
    5. function OnGUI () {
    6.     hSliderValue = GUI.HorizontalSlider (Rect (25, 25, 100, 30), hSliderValue, 0.0, 10.0);
    7. }
    The Horizontal Slider created by the example code (isnt this what you are looking for?)
     
    Last edited: Feb 17, 2011
  5. theinfomercial

    theinfomercial

    Joined:
    Sep 9, 2008
    Posts:
    1,000
    That's nice. Unfortunately, I'm using my own 3d slider (as implied in my first post), and as such, I can't use Unity GUI to calculate a slider for me.
     
  6. Mike L

    Mike L

    Joined:
    Sep 14, 2010
    Posts:
    1,035
    oh, i didnt see the 3d part. how did you do that?
     
  7. theinfomercial

    theinfomercial

    Joined:
    Sep 9, 2008
    Posts:
    1,000
    Figured it out. BigMisterB's code works as expected and my slider button displays the proper range.

    I honestly have no idea how it works, though.......I F***ing hate math. I need to take a wikipedia algebra course or something. :(

    Thanks for the help everyone! *runs off to wikipedia*
     
  8. theinfomercial

    theinfomercial

    Joined:
    Sep 9, 2008
    Posts:
    1,000
    Ok, another question.

    With the code above, how does one make it so that you can change the position of the slider button based on manually setting the value of the slider?

    For example, when I move the slider button, it gives me the slider value. But how do I make it so that I can set the slider value directly (like in the editor) and have the slider button update it's position accordingly?
     
  9. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Code (csharp):
    1.  
    2. var minPoint : Vector3;
    3. var maxPoint : Vector3;
    4. var currentPoint : Vector3;
    5.  
    6. var minValue : float = 3.0;
    7. var maxValue : float = 9.0;
    8.  
    9. function Update(){
    10. var modifier = Vector3.Distance(minPoint, currentPoint) / Vector3.Distance(minPoint, maxPoint); // should be a number between 0 and 1
    11. var myValue = (maxValue - minValue) * modifier + minValue;
    12. }
    13.  
    14. function SetValue(value : float){
    15. transform.position = Vector3.Lerp(minPoint.position, maxPoint.position, ((value - minValue) / (maxValue - minValue));
    16. }
    17.  
    The math is actually very simple math. It just looks complicated. It all revolves around the difference between the min value and max value. so maxValue - minValue (i.e. 9 - 3 = 6) divided by the number of the value - minValue. This gives us a number between 0 and 1. Precisely what a Lerp uses to calculate the position that it needs.

    So the value of 5 comes out to (5 - 3) / (9 - 3) or 2 / 6 which is 0.333.

    The reverse is true then too. 0.333 = 0.333 * 6 which is ~ 2 when you add the 3 minimum value is 5.
     
    JPressley99 likes this.
  10. theinfomercial

    theinfomercial

    Joined:
    Sep 9, 2008
    Posts:
    1,000
    Big thanks, MisterB! The code works great.

    Here's the (more or less) final version of my 3d slider. Thanks for the help guys!

    Code (csharp):
    1. var sliderName : String = "New Slider";
    2. var slider : GameObject;
    3. var sliderButton : GameObject;
    4. var buttonValue : float = 1;
    5. var minValue : float = 0;
    6. var maxValue : float = 1;
    7. private var cam : Camera;
    8. private var sliderSize : float;
    9. private var minPoint : float;
    10. private var maxPoint : float;
    11. private var zPos : float;
    12. private var lastfingerid : int = -1;
    13.  
    14. function Awake ()
    15. {
    16.     cam = GameObject.Find ("GUI Camera").GetComponent (Camera);
    17.     sliderSize = slider.transform.localScale.x * 0.5;
    18.     minPoint = -sliderSize;
    19.     maxPoint = sliderSize;
    20.     zPos = sliderButton.transform.position.z;
    21. }
    22.  
    23. function ResetFinger ()
    24. {
    25.     lastfingerid = -1;
    26. }
    27.  
    28. function Update ()
    29. {
    30.     if (Input.touchCount == 0)
    31.     {
    32.         ResetFinger ();
    33.     }
    34.    
    35.     for (var i : int = 0; i < Input.touchCount; i++)
    36.     {
    37.         var touch : Touch = Input.GetTouch(i);
    38.         var latchFinger : boolean = false;
    39.        
    40.         if (touch.phase == TouchPhase.Began)
    41.         {
    42.             var ray : Ray = cam.ScreenPointToRay (touch.position);
    43.             var hit : RaycastHit;
    44.            
    45.             if (Physics.Raycast (ray, hit, 2))
    46.             {
    47.                 if (hit.collider == sliderButton.collider)
    48.                 {
    49.                     latchFinger = true;
    50.                 }
    51.             }
    52.         }
    53.        
    54.         if (latchFinger  (lastfingerid == -1 || lastfingerid != touch.fingerId))
    55.         {
    56.             lastfingerid = touch.fingerId;
    57.         }
    58.        
    59.         if (lastfingerid == touch.fingerId)
    60.         {
    61.             var sTW : Vector3 = cam.ScreenToWorldPoint (new Vector3 (touch.position.x, 0, zPos));
    62.             sliderButton.transform.position.x = sTW.x;
    63.             sliderButton.transform.position.z = sTW.z;
    64.             sliderButton.transform.localPosition.x = Mathf.Clamp (sliderButton.transform.localPosition.x, -sliderSize, sliderSize);
    65.            
    66.             var modifier : float = (minPoint - sliderButton.transform.localPosition.x) / (minPoint - maxPoint);
    67.             buttonValue = (maxValue - minValue) * modifier + minValue;
    68.             buttonValue = Mathf.Clamp (buttonValue, minValue, maxValue);
    69.            
    70.             if (touch.phase == (TouchPhase.Ended || TouchPhase.Canceled))
    71.             {
    72.                 ResetFinger ();
    73.             }
    74.         }
    75.     }
    76. }
    77.  
    78. function SetValue (val : float)
    79. {
    80.     sliderButton.transform.localPosition.x = Mathf.Lerp (minPoint, maxPoint, (val - minValue) / (maxValue - minValue));
    81.     sliderButton.transform.localPosition.x = Mathf.Clamp (sliderButton.transform.localPosition.x, -sliderSize, sliderSize);
    82.     buttonValue = val;
    83.     buttonValue = Mathf.Clamp (buttonValue, minValue, maxValue);
    84. }