Sliding elements across a 2D GUI

I am trying to move elements on my screen over time rather then just making them jump to their locations. After some experimentation I have implemented a function that will take a rect and change it’s position over time. However, I wonder if there isn’t a much simpler methodology for moving 2D GUI Recs over time.

Anyway, this is what I have come up with. TabBarPosition is a global variable.

function MoveTabBarToPos(newPos : Rect)
{
	Slide2DRectOverTime("SetTabBarPosition", TabBarPosition, newPos, TabBarScrollTime);
}

function SetTabBarPosition(newPos : Rect)
{
	TabBarPosition = newPos;
}


// Since javascript (or C#, or Boo for different reasons) can't pass by reference, I'm implementing a very 
// strange hack to make this function work in a generic way. I'm calling a SendMessage to myself to invoke 
// a function that updates the value of the variable in question. 
function Slide2DRectOverTime(modifyFunc : String, currentPos: Rect, endPos : Rect, timeToTake : float)
{
	var StartTime : float = Time.time;
	var EndTime : float = StartTime + timeToTake;
	var StartPos : Rect = currentPos;
	
	SearchStarted = true;
	
	var CurrentTime = Time.time;
	var FractionOfTimeLeft = (EndTime - CurrentTime) / (EndTime - StartTime);
	while(FractionOfTimeLeft > 0.0)
	{
		currentPos = Rect(((StartPos.x - endPos.x) * FractionOfTimeLeft) + endPos.x,
					((StartPos.y - endPos.y) * FractionOfTimeLeft) + endPos.y,
					((StartPos.width - endPos.width) * FractionOfTimeLeft) + endPos.width,
					((StartPos.height - endPos.height) * FractionOfTimeLeft) + endPos.height);
		
		this.SendMessage(modifyFunc, currentPos);
		yield;
		
		CurrentTime = Time.time;
		FractionOfTimeLeft = (EndTime - CurrentTime) / (EndTime - StartTime);
	}
	
	this.SendMessage(modifyFunc, endPos);
}

Any help or advice would be appreciated.

A possible solution could be to use the GUIManager which handles a lot of animations very nicely http://forum.unity3d.com//viewtopic.php?t=23557

Another approach could be to use a Lerp function to move between two positions, the documentation has an example I think.

Thanks for the link, that looks like a useful plugin :slight_smile:

I’m beginning to get the idea that the path of least resistance is more of a state machine methodology then an object oriented one.