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