Hello everyone,
I’m trying to animate or move a switch button in my game scene. So when I hit my “GUI on/off button” it makes the “3d on/off button in orange” change the position to ON.
But I want to hit the same button, the “GUI on/off button” to change to the original position to OFF.
Now I can move the 3d on/off button to the ON position when I hit the GUI on/off button. But I can’t move it back to the position OFF.
Please take a look to my video and my scripts.
Any advice is greatly appreciated
Switch Script
var smooth = 1;
var on : Transform;
function Update () {
if(buttonsGUI.onGo ==1){
transform.position = Vector3.Lerp (transform.position, on.position, Time.deltaTime * smooth);
}
}
GUI On/Off Switch Script
static var onGo : int;
var Skin_power : GUISkin; var beep : AudioClip;
function OnGUI (){
GUI.skin = Skin_power;
if (GUI.Button(Rect(870,227,80,56),"")){
audio.PlayOneShot(beep);
onGo = 1;
}
}
I am not sure if I understood your question correctly, but if I have, could this be a solution?
static var onGo : int;
var Skin_power : GUISkin; var beep : AudioClip;
var switchedOn : boolean = false;
function OnGUI()
{
GUI.skin = Skin_power;
if (GUI.Button(Rect(870,227,80,56),"") && !switchedOn)
`` {
// switch on do your stuff..
audio.PlayOneShot(beep);
onGo = 1;
switchedOn = true;
}
else if (GUI.Button(Rect(870,227,80,56),"") && switchedOn)
`` {
// switch off do your stuff..
switchedOn = false;
}
}
Ok, I looked at this again, and even though I agree with @eXtremeTY, I would do it like this.
Use this for your button code:
var Skin_power : GUISkin;
var beep : AudioClip;
function OnGUI() {
GUI.skin = Skin_power;
if(GUI.Button(Rect(870,227,80,56),"")) {
audio.PlayOneShot(beep);
buttonSlide();
}
}
Then use this for your movement:
var onGo = true;
var smooth = 1;
var on : Transform;
var off : Transform;
function buttonSlide() {
if(onGo) {
transform.position = Vector3.Lerp (transform.position, on.position, Time.deltaTime * smooth);
onGo = !onGo;
}
else {
transform.position = Vector3.Lerp (transform.position, off.position, Time.deltaTime * smooth);
onGo = !onGo;
}
}
Now you will need to pass in another transform with this method, for your initial position, so it knows where to move. I would probably move the audio code into the buttonSlide function as well. Also, if you use a GUIButton, it’s better to create a function, rather than use the update function, as this just wastes CPU cycles checking stuff in the update, when you know you only want it done when the button is pressed.