- Home /
Vector3 Lerp, no smooth movement
i want my camera to smoothly move to the new position. unfortunatly this is not working. the script waits until the time is over and then it suddenly changes position, but doesnt interpolate between my 2 values. what am i doing wrong? also when i put a yield command in there my gui dissapears... heres my script, hope someone can help me..
if (GUI.Button (Rect (6,60,100,20), "Set as Center")){
var i = 0.0;
var rate = 1.0/9;
meineCam.GetComponent(Mouse).target = GameObject.Find(this.name +"target").transform;
var oldPosition = Vector3(meineCam.transform.position.x, meineCam.transform.position.y, meineCam.transform.position.z);
var newPosition = Vector3(GameObject.Find(this.name +"target").transform.position.x , GameObject.Find(this.name +"target").transform.position.y, meineCam.transform.position.z);
while (i < 1.0) {
i += Time.deltaTime * rate;
meineCam.transform.position = Vector3.Lerp(oldPosition, newPosition, i);
}
}
thanks a lot!
Answer by Berenger · Mar 23, 2012 at 03:56 PM
That's because no frame is drawn while your while is running. The engine just freezes until it's done. Try that instead :
function OnGUI(){
if( GUI.Button(...) ){
....
MoveCamera(oldPosition, newPosition);
}
}
function MoveCamera( oldPos : Vector3, newPos : Vector3 ){
var t : float = 0.0;
while (t < 1.0) {
meineCam.transform.position = Vector3.Lerp(oldPos, newPos, t);
yield null; // Here the while is paused until the next frame, so you actually see the camera moving.
t += Time.deltaTime * rate;
}
}
PS 1 : Be carefull with my js syntax, i'm more of a C# guy.
PS 2 : Try not to do anything not-Event-related in OnGUI, like using Time.deltaTime. That function is called several times per frame and that will causes performances losses and unwanted behaviors.