- Home /
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)
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);
}
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);
}
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()).
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!
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).
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]
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
Follow this Question
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