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.
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
}
}
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;
}
}
}