Move Button as soon as its klicked to the bottom Screen
Hey guys,
so i have this task of moving a button to the bottom of the screen as soon as its clicked. i tired different solutions:
var button : Texture;
var x : int = 20;
var bottom : int = (Screen.height - 20);
var time : int = 2;
var startTime : int = Time.time;
var speed : int = 100;
function OnGUI() {
if (GUI.Button (Rect(10,x,80,40), button))
Debug.Log("Es geht los!");
// x += 150*Time.deltaTime; <<-- This way the button never stops to move
}
function Update() { <<-- this way the button stops at the middle of the screen (?)
if(Time.time > startTime + time) {
x += Time.deltaTime*speed;
if (x > bottom) {
x = bottom;
}
}
}
but none of them really worked.
i need the button to go to the bottom of the screen, and then move to the top and back like 5 times.
thanks in advance!
Answer by Brocccoli · Mar 17, 2016 at 03:14 PM
I would set a boolean value on button click
if (GUI.Button (Rect(10,x,80,40), button))
{
bool moveButton = true;
}
Then in your Update method
while(moveButton){
// Increment button towards the bottom of the screen
if (buttonAtDesiredPosition)
{
moveButton = false;
}
}
There's some sudo code to get you started. Get rid of the Time.Time and DeltaTime stuff, I don't think you need it.
Thanks, loos like a simple yet useful structure. :) can you help me further and be a bit more specific about the actual moving and reaching the position?
Seems like the logic you have more moving right now will do. You just need to know what your desired position is and that's when you stop.
yeah but how can i check the position of the button? What would be the script line?