x


yield Vector3.Lerp()?

Is there a way to yield until a Lerp has completed?

I am trying to lerp a camera's target position to a new object, when a new object is detected via an external raycast hit.

more ▼

asked May 07 '11 at 08:08 AM

ina gravatar image

ina
3.3k 492 547 596

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Not like yield animationState;. Yield isn't a Unity specific keyword and it requires that you return an IEnumerator. Vector3.Lerp() returns a Vector3, but you could run the loop until its completed and wait for the result, but then you would have to move some stuff out of Update(). It would be easier if you were using a finite state machine, but it isn't necessary.

var isLerping : boolean = false;

function Update () {
    if(/*conditions arise that call for your camera to loop*/) {
         LerpCamera(startPos, endPos);
    }

    if(!isLerping) {
    }

}

function LerpCamera (start : Vector3, end : Vector3) : IEnumerator {
    var i : float = 0;
    isLerping = true;
    while(i < 1) {
         camera.transform.position = Vector3.Lerp(start, end, i);
         i += Time.deltaTime;
         yield;
    }
    isLerping = false;

    //You can put more commands here that will be execute after the Lerp is finished.  Like you yielded the completion.
}

You can continue your code from wherever you want, I would either go after you finish the loop, or you can put it in Update somewhere. Start() with an infinity loop made into an FSM would probably be easiest because then you can yield the whole function then you won't even need a bool check.

more ▼

answered May 07 '11 at 11:11 AM

Peter G gravatar image

Peter G
15k 16 44 136

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2979
x333
x261
x161

asked: May 07 '11 at 08:08 AM

Seen: 1661 times

Last Updated: May 07 '11 at 08:08 AM