Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by paco morales · Jan 17, 2013 at 01:11 AM · guimovementstaticslide

GUI Slider and Movement?

Hello everyone,

I'm trying to fix this script, I want to move one object to another point. the problem is that the movement is continuous and ends without control. I have tested a lot of examples but without success.

I want to control the movement exactly as the movement of my GUI Slider. I'm using a Static Variable from my GUI.Slider script to control the script applied to the object.

Please any advice is more than welcome!

Please see my scripts.

Script Object

 var start : Transform;
 var end : Transform;
 
 function Update () {
 move();
 }   
  
 function move(){ 
     
 amtToMove = buttonsGUI. vSliderValue  * Time.deltaTime;
      
 transform.position = Vector3.Lerp(start.position, end.position, Time.time);
 }
 

Script GUI Slider

 static var triggerGo : int;
 static var vSliderValue : float = 0.0;
 
 var vthumbStyle : GUIStyle;
 var vsliderStyle : GUIStyle;
 var Skin_power : GUISkin; 
 var beep : AudioClip;
 
 function OnGUI (){
 
      vSliderValue = GUI.VerticalSlider (Rect (887.5, 10, 50, 203), vSliderValue, -1500.0, -100.0,vsliderStyle,vthumbStyle);
    
        GUI.skin = Skin_power;
     if (GUI.Button(Rect(870,227,80,56),"")){
      audio.PlayOneShot(beep);
     triggerGo = (triggerGo + 1) % 2;
      
     } 
 }    
 
 @script RequireComponent(AudioSource)

 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer Wiki

Answer by robertbu · Jan 17, 2013 at 03:06 AM

Since you say, "control the movement exactly," here is some code that moves them together. The fraction calculation looks a bit messy because of your unusual values for the bottom and top of the slider. The line calculates the fraction of the way from the bottom to the top the current value places the slider. I've left minus signs in so you can see how your original values are used. Untested:

 function Update() {
     var fraction : float = (vSliderValue - (-1500.0)) / ((-100.0 - (-1500.0));
     transform.position = Vector3.Lerp(start.position, end.position, fraction);
 }
    
Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image iwaldrop · Jan 17, 2013 at 03:18 AM 0
Share

That is quite an elegant solution robertbu!

avatar image paco morales · Jan 17, 2013 at 04:13 AM 0
Share

Thanks Robert, let me try it out :)

avatar image paco morales · Jan 17, 2013 at 04:58 AM 0
Share

Thanks again Robert, works perfectly :)

avatar image paco morales · Jan 17, 2013 at 07:01 PM 0
Share

Hello Robert,

I have a little problem, I hope is something easy to solve. $$anonymous$$y "VariableSpeedAdjuster" the 3d object in red, only works when the GUI Slider is almost to finish the range.

Please take a look to the video. http://www.youtube.com/watch?v=-uq5jdxdni8

Again thanks for your patience and help.

var start : Transform;

var end : Transform;

function Update() { var fraction : float = (buttonsGUI. vSliderValue - (-1500.0) / (-1000.0) - (-1500.0)); transform.position = Vector3.Lerp(start.position, end.position, fraction);

}

avatar image robertbu · Jan 17, 2013 at 08:22 PM 0
Share

Hi paco,

For testing purposes, set topValue and bottomValue in the VerticalSlider() code to 0.0f, 1.0f. With this change you don't need to calculate the fraction. You can just put the value directly in the the Lerp:

Vector3.Lerp(start.position, end.position, vSliderValue);

To get it to work with other values the calculation is:

fraction = (value - topValue) / (bottomValue - topValue);

where value is the current value returned by the slider, and topValue and bottomValue are the values you are passing to the GUI.VerticleSlider() method (I've used the same names as are in the Unity Script Reference for VerticalSlider()).

Show more comments
avatar image
1

Answer by iwaldrop · Jan 17, 2013 at 02:33 AM

I see several problems, actually.

First, while it looks like amtToMove is being set properly, you're never using it to lerp to! Second, in your Lerp function you are using Time.time. The time value should be somewhere between 0 and 1, and after the first second of your game running Time.time will always be greater than 1! Third, you're lerping between two values that never seem to change, so even if you were using a value between 0 and 1 for your time the position of your transform wouldn't move unless the time value changed.

So. You need to set the target position based on the amtToMove value. Are you moving this object on the x, y, or z axis? I'm going to assume the y since you're using a vertical slider.

 function move()
 {
     var time = Time.deltaTime;
     while (time < 1)
     {
         transform.position = Vector3.Lerp(transform.position, new Vector3(transform.position.x, amtToMove, transform.position.z), time);
         time += Time.deltaTime;
         yield;
     }
 }

Now, I don't use unityscript, and this is untested code, but this should get you going in the right direction. Give it a shot, and if it helps you out remember to mark the question as answered! Thanks!

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image iwaldrop · Jan 17, 2013 at 02:36 AM 0
Share

You might try playing with a multiplier on the time value. This will control the speed at which the Lerp completes. Also, again being unfamiliar with unityscript, you might find that if you move the slider very quickly that this code produces some undesierable effects (since it uses a coroutine).

avatar image paco morales · Jan 17, 2013 at 03:00 AM 0
Share

Iwaldrop, Thanks for your help I want to move it horizontally. Please take a look to my screenshots. and my script Updated.

Thanks agaim.

function Update () {

move();

}

function move(){

    amtTo$$anonymous$$ove = buttonsGUI. vSliderValue  * Time.deltaTime;
 
    var time = Time.deltaTime;
     
    while (time < 1){
    
    transform.position = Vector3.Lerp(transform.position, new Vector3(transform.position.x, amtTo$$anonymous$$ove, transform.position.z), time);
    time += Time.deltaTime;
    
    yield;
 }

}

http://www.youtube.com/watch?v=wVoAVjNghts

[I$$anonymous$$G]http://i38.photobucket.com/albums/e102/Paco_$$anonymous$$orales/3DObjector3DSlider_zpsaf42d5fc.jpg[/I$$anonymous$$G]

avatar image iwaldrop · Jan 17, 2013 at 03:15 AM 0
Share

Ok...after seeing your video I realized that, for your use case, there is a simpler approach.

I whipped up a little c# code to do what you need. Line for line, it should integrate into your unityscript just fine.

 using UnityEngine;
 using System.Collections;
 
 public class $$anonymous$$oveGameObjectWithGUISlider : $$anonymous$$onoBehaviour
 {
     private float amountTo$$anonymous$$ove, upper, lower;
     
     void Awake()
     {
         amountTo$$anonymous$$ove = transform.position.x;
         upper = amountTo$$anonymous$$ove + 50;
         lower = amountTo$$anonymous$$ove - 50;
     }
     
     void Update()
     {
         transform.position = new Vector3(amountTo$$anonymous$$ove, transform.position.y, transform.position.z);
     }
     
     void OnGUI()
     {
         amountTo$$anonymous$$ove = GUI.VerticalSlider(new Rect(100, 100, 20, 200), amountTo$$anonymous$$ove, upper, lower);
     }
 }

This should be much more like what you need; just get the amountTo$$anonymous$$ove value from your existing GUI slider and set the extents accordingly.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Making a bubble level (not a game but work tool) 1 Answer

EditorGUI and method create 0 Answers

question with gui 2 Answers

Lock a textarea/textfield for editing 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges