- Home /
how do i make this move slowly over time
i got this problem where i have my GUI labels and i want to as soon as you hit new game, the menu will slide off to the left wich is -600 but i simply cannot find a way to do it :/ any wise heads out there? thanks alot in advance!
void OnGUI() {
GUI.skin = menu_styles;
if (main == true){
GUI.BeginGroup(new Rect(placeing, 0, 800,900));
if (GUI.Button(new Rect(0, 40, 512, 128),"",menu_styles.customStyles[0])){
//main = false;
//placeing = (-600f) * Time.deltaTime;
placeing = BLANK
Debug.Log ("lol");
}
if (GUI.Button(new Rect(0, 170, 512, 128),"",menu_styles.customStyles[1])){
}
if (GUI.Button(new Rect(0, 300, 512, 128),"",menu_styles.customStyles[2])){
}
if (GUI.Button(new Rect(0, 430, 512, 128),"",menu_styles.customStyles[3])){
}
GUI.EndGroup();
}
}
Answer by aldonaletto · Jun 12, 2012 at 12:26 PM
You could have a destX variable holding the desired X coordinate and make placeing moves gradually to it:
public float speed = 800; // speed in pixels/second public float destX = 0; // set the desired X coordinate here
void Update(){ // slide placeing towards destX at Update placeing = Mathf.MoveTowards(placeing, destX, speed * Time.deltaTime); ... }
void OnGUI() { GUI.skin = menu_styles; if (placeing > -600){ // only draw the GUI when menu not completely hidden if (main == true){ GUI.BeginGroup(new Rect(placeing, 0, 800,900)); if (GUI.Button(new Rect(0, 40, 512, 128),"",menu_styles.customStyles[0])){ destX = -600; // sets the new destX to start sliding } // other buttons GUI.EndGroup(); } } } Just set destX to 0 to make the menu slide in, and click the button to make it slide out.
thanks!!! it worked with 1 tweek, how come i cannot use time.deltatime? i have to use time.time?? if i use time.deltatime nothing happens :( know why that is?,thank you! it worked but i cannot use delta time?? i had to use Time.time?? why is that? time.deltatime didnt do anything at all
Have you changed something? Notice that $$anonymous$$oveTowards must be in Update, not in OnGUI.
$$anonymous$$ultiplying speed by Time.deltaTime makes the movement independent of the framerate. If you omit the multiplication, the menu will move speed pixels each frame - and the framerate may vary a lot during the game or among different machines.
NOTE: I forgot to specify the speed type: it should be declared as a float (answer already fixed). If C# decided it's an int (or if you declared it as an int), the function will not work with Time.deltaTime, as you've said. Declare speed as a float, and maybe your problem magically disappears:
public float speed = 800; // speed in pixels/second
Your answer
![](https://koobas.hobune.stream/wayback/20220613072649im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Movement script stops working when time.timescale= 0 1 Answer
How to WaitforSeconds a GuiButton ? 0 Answers
How to create an agenda or timetable system? 1 Answer
Showing Time With GUI.Label 1 Answer
How to Follow By Getting KeyDown? 0 Answers