- Home /
Synchronize a GUI slider with a 3d Object?
Hello everyone,
I'm trying to make a Drill with some interactive buttons as exercise to learn Unity. I made one button to activated the drill so you can see the tool spinning and a slider to control the speed in the GUI.
But I have problems trying to synchronize a GUI slider with a 3d Object "VariableSpeedAdjuster" with animation attached , so when the GUI slider is moving the 3d object too.
I made one script for the GUI buttons with a Slider and other for the 3d Object "VariableSpeedAdjuster" and finally another for the rotor or motor.
I'm trying to get to work my script, but I can't.
Please any advice is more than welcome. Thanks in advance
Script Number 1 GUI & Slider Script.
static var triggerGo : int;
static var cameraGo : 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 Number 2 And this is my other Script for the 3D object to be interact with my GUI Slider.
static var vSliderValue : int;
var myAnimation : AnimationState;
function Start(){
myAnimation = animation["VariableSpeedAdjuster"];
}
function LateUpdate() {
myAnimation.time = vSliderValue;
myAnimation.enabled = true;
animation.Sample();
myAnimation.enabled = false;
}
Script Number 3 Motor or Rotor
function Update () { amtToMove =buttonsGUI. vSliderValue Time.deltaTime; if (buttonsGUI .triggerGo == 1){ transform.Rotate(0,buttonsGUI. vSliderValue Time.deltaTime,0);
} }
Answer by CodeMasterMike · Jan 15, 2013 at 08:28 AM
I have little problem reading the script code, but here is a simple example on how you can do it:
private float m_currentValue = 0.0f;
private float m_topValue = 1.0f;
private float m_bottomValue = 0.0f;
private Vector3 m_offPosition = new Vector(0.0f, 0.0f, 0.0f);
private Vector3 m_onPosition = new Vector(1.0f, 1.0f, 1.0f);
public GameObject m_3dObjectSlider = null;
OnGUI()
{
// Slider goes between 0.0 and 1.0, or you can see it as 0% to 100%
m_currentValue = GUI.VerticalSlider(position, m_currentValue, m_topValue, m_bottomValue);
m_3dObjectSlider.transform.Position = Vector3.Lerp(m_offPosition, m_onPosition, m_currentValue);
}
So, I setup the slider GUI object to slide between 0.0 and 1.0. And I use this value to Lerp the position of the 3d object. Now, I don't know the exact positions for the object, so I just put in some values, that you need to change.
But as you pull the slider in any direction, the 3d object should also slide between these two vectors.
I haven't tested this code, its just a example, but I hope you can see the idea behind it and change the code around until it works for you and your project.
Good luck!
Hi $$anonymous$$ike, Let $$anonymous$$e try it :)
Thanks