- Home /
HOW TO: Sliding GUI Window?
I have a GUI.Window
that i want to slide on event.
//Message Window Animation
private float fMessageWindowTop = 0;
private float fMessageWindowCurrent = 0;
private float fMessageWindowTarget = 0;
private float fSmoothTime = 0.3F;
private float fVelocity = 0.0F;
private bool bMessageWindowAnimate = false;
public void AddMessage (string msg)
{
fMessageWindowCurrent = fMessageWindowTop;
fMessageWindowTarget = 100;
bMessageWindowAnimate = true;
}
void OnGUI ()
{
if (Time.timeScale > 0)
{ //if the game isn't paused
if(bMessageWindowAnimate) {
fMessageWindowTop = Mathf.SmoothDamp(fMessageWindowCurrent, fMessageWindowTarget, ref fVelocity, fSmoothTime);
if(fMessageWindowTop == fMessageWindowTarget) {
bMessageWindowAnimate = false;
}
}
GUI.Window (1, new Rect (Screen.width / 2 - (300 / 2), fMessageWindowTop, 300, 50), CreateMessagesWindow, "Messages");
}
}
This code doesn't work.. When the AddMessage method is being called, the window start to move.. it moves 6 pixels and then gets stuck and starts being jumpy...
Anyway to make a good sliding window?
Answer by Berenger · May 05, 2012 at 06:16 PM
First, doing anything time related in OnGUI is not a good idea. You see, OnGUI is executed several times per frame for GUI placements and co. Which would make your movement unstable and faster than it should be. Use Update instead.
Now, in your Mathf.SmoothDamp, the current value and the destination value are constant, unless they are updated in a part of the code you didn't write here. Replace fMessageWindowTop by fMessageWindowCurrent in SmoothDamp and in the rect of the window.
Your answer
![](https://koobas.hobune.stream/wayback/20220613071129im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Elements in draggable window do not remain fixed [SOLVED] 2 Answers
Distribute terrain in zones 3 Answers
C# Rows and Columns GUILayout.Window Issues 1 Answer
Multiple Cars not working 1 Answer
How do I tell my code which window the game runs in? 0 Answers