Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

move object with slide

Discussion in 'Scripting' started by Bolt, Jul 18, 2013.

  1. Bolt

    Bolt

    Joined:
    Dec 15, 2012
    Posts:
    296
    I wanted to move an object through a slide. I converted the function to use the slide. Now I'm lost. How can I connect it to the object in order to move it?

    Code (csharp):
    1.  
    2. var hSliderValue : float = 0.0;
    3. var a : float;
    4.    
    5.     public var max : float;
    6.     public var min : float;
    7.     public var slideX : float;
    8.    
    9.    
    10.     function OnGUI () {
    11.        GUI.HorizontalSlider (Rect (25, 25, 100, 30), 0.0, 10.0);
    12.     }
    13.    
    14.    
    15.    
    16.    
    17.     function calculate(){
    18.    
    19.    
    20.    
    21.    
    22.     return ((max-min)*(slideX-0)/(1-0))+min;
    23.    
    24.    
    25.    
    26.    
    27.     }
    28.  
    29.  
     
  2. tonyd

    tonyd

    Joined:
    Jun 2, 2009
    Posts:
    1,224
    Try the following code. You'll need to attach it to the object you want to move.

    If you wish to keep the object on-screen, you'll need to tweak the min and max values, or use some other method.

    Code (csharp):
    1. var min = -10.0;
    2. var max = 10.0;
    3. var sliderPos = Rect(25, 25, 100, 30);
    4.  
    5. function OnGUI (){
    6.     transform.position.x = GUI.HorizontalSlider(sliderPos, transform.position.x, min, max);
    7. }
    8.