- Home /
while coroutine not ending correctly using unityscript
Hi I'm making a door open and close using a transform instead of animation
var speedTime : float;[1]
private var t: float = 0;
function Close() {
stateOpen = false;
stateClosing = true; // so we know its closing
while (t < speedTime) {
t += Time.deltaTime / speedTime; // sweeps from 0 to 1 in time seconds
transform.position = Vector3.Lerp(initialEnd, initialStart, t); // set position proportional to t
yield; // leave the routine and return here in the next frame
}
t = 0;
stateClosing = false; // so we know its not closing
stateClosed = true; // so we now its closed
}
after closing the door i have to wait a prolonged ammount of time before i can open it again only if speedTime is over 1. it seems as if the while coroutine is waiting the length of speedTime until allowing itself to play again.
any help would be great I'm really stumped
[1]: /storage/temp/13481-door_slide.zip
You can use basic debugging, such as putting Debug.Log("closed");
after the loop, to see what's happening. Also the t variable should be local to the Close function, not a global variable.
Thanks i have added speeTime to the open and close function, using the debug message yields the same results it takes a long time after t = speedTime to continue with the rest of the function.
Answer by Bunny83 · Jul 24, 2013 at 07:13 PM
You actually don't wait speedTime seconds but speedTime seconds squared
You divide Time.deltaTime by speedTime, so for example if speedTime is 5 it would take 5 seconds to reach 1.0. However your while condition checks for "t < speedTime". So it doesn't run 5 seconds but 25 seconds 5^2.
You want to use a while like this:
while(t < 1.0f)