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?

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.