So what I want is move GUI element from start position to end position in GUI screen coordinate.
I wrote like this but does not work. What’s the problem?
var start : Vector2; // set to 0,0
var end : Vector2; // set to 100, 100
private var pos : Vector2;
function OnGUI(){
GUI.Box(Rect(pos.x, pos.y, 50, 50), "Hello");
pos.Lerp(start, end, Time.deltaTime * 2);
}
Never put movement code inside a OnGUI() - because you don’t know how many times it will be called!
Here’s a sample script:
var start: Vector2; // set to 0,0
var end: Vector2 = Vector2(100, 100); // set to 100, 100
var speed: float = 1;
var currentPos: Vector2;
function Start() {
currentPos = start;
}
function OnGUI() {
GUI.Box(Rect(currentPos.x, currentPos.y, 50, 50), "Hello");
}
function Update() {
currentPos = Vector2.Lerp(currentPos, end, Time.deltaTime * speed);
}