- Home /
Question by
Iceblitzyt · Jun 24, 2013 at 11:52 PM ·
guiboxsliding
How do I move a GUI Box Over Time?
Hey there,
I've been basically trying to figure our how to slide a gui box over a period of time on the screen.
Say I had a box with it's rect (0,0,500,100) and After 2 seconds i wanted it to become rect(0,50,500,100). How would you do that over time? I assume you'd need to times by Time.Deltatime, but i can't figure out how.
Can someone please provide a simple example for me to follow?
Many thanks.
Comment
Best Answer
Answer by CoriolisStudios · Jun 25, 2013 at 01:32 AM
enter code here
You can just add up each delta time like so... (Written in C#)
float cumulativeTime = 0.0f; //Make sure this is a global
//variable (define it at the
//start)
void OnGUI() {
cumulativeTime += Time.deltaTime;
if (cumulativeTime < 2) //The 2 seconds have not passed
GUI.Box(new Rect(0, 0, 500, 100), "1st position");
if (cumulativeTime >= 2) //The 2 seconds has passed
GUI.Box(new Rect(0, 50, 500, 100), "2nd position");
}
Edit: I misread the question (I think). If you meant move slowly over time then do this: (C#)
float cumulativeTime = 0.0f;
float yPosition = 0.0f;
void OnGUI() {
cumulativeTime += Time.deltaTime;
if (cumulativeTime <= 2) {
yPosition = 25 * cumulativeTime; //Because the box is moving
//at 50 pixels per 2
//seconds (or 25 pixels per
//1 second)
GUI.Box(new Rect(0, yPosition, 500, 100));
}
if (cumulativeTime > 2) {
GUI.Box(new Rect(0, 50, 500, 100)); //Because chances are that cumulativeTime won't
//end up at exactly 2, it will probably go over
}
}
Answer by create3dgames · Jun 25, 2013 at 01:44 AM
This will give you a nice result:
var x : int = 0;
var y : int = 0;
var w : int = 500;
var h : int = 100;
var speed : int = 100;
var startTime : int;
var guiBoxTime : int = 2;
function Start ()
{
startTime = Time.time;
}
function OnGUI ()
{
GUI.Box(Rect(x,y,w,h),"GUI Box");
}
function Update ()
{
if (Time.time > startTime + guiBoxTime)
{
y += Time.deltaTime*speed;
if (y > 50)
{
y = 50;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613104825im_/https://answers.unity.com/themes/thub/images/avi.jpg)