- Home /
Waiting Time Questions
Hello. I new to Unity (and javascript). The problem I am running into is waiting time. As you will see below in my code I essentially move a block in some direction and call that a step. I also want to be able to move the block 10 steps at a time. Currently I can only get it to move the 10 steps instantly. I want it to pause between each step and move the object each time so that the user can see the objects path. I have tried numerous things with Start(), coroutines, and WaitForSeconds, but I can't see to get it to work properly. Any help or suggestions you can give are greatly apprciated.
The Zombie is the block I want to move around.
var Zombie : GameObject; var levelComplete : GUITexture; var leftProb = "0.0"; var rightProb = "0.0"; var upProb = "0.0"; var downProb = "0.0"; var showWarning = false;
function Start() {//Initialize Level Complete Banner to false levelComplete.enabled = false; }
function OnGUI () { GUI.Label (Rect (5, 5, 50, 25), "Left","box"); leftProb = GUI.TextField (Rect (55, 5, 30, 25), leftProb);
GUI.Label (Rect (5, 30, 50, 25), "Right","box");
rightProb = GUI.TextField (Rect (55, 30, 30, 25), rightProb);
GUI.Label (Rect (5, 55, 50, 25), "Up","box");
upProb = GUI.TextField (Rect (55, 55, 30, 25), upProb);
GUI.Label (Rect (5, 80, 50, 25), "Down","box");
downProb = GUI.TextField (Rect (55, 80, 30, 25), downProb);
if (GUI.Button (Rect (90, 5, 100, 30), "Take a Walk!")) {
for(temp = 10; temp >0;temp--){
takeStep(leftProb,rightProb,upProb,downProb);
}
}
if (GUI.Button (Rect (90, 35, 100, 30), "Take a Step!")) {
checkProbs(leftProb,rightProb,upProb,downProb);
if(showWarning == false){
takeStep(leftProb,rightProb,upProb,downProb);
}
}
if (GUI.Button (Rect (90, 65, 100, 30), "Reset!")) {
leftProb = "0.0";
rightProb = "0.0";
upProb = "0.0";
downProb = "0.0";
}
if(showWarning){
if (GUI.Button (Rect (190, 35, 250, 30), "Probabilities don't sum to 1!!!")) {
showWarning = false;
}
}
}
function checkProbs(leftProb,rightProb,upProb,downProb){ //Make sure probs add to one var left: float; var right: float; var up: float; var down: float; var check: float; left = parseFloat(leftProb); right = parseFloat(rightProb); up = parseFloat(upProb); down = parseFloat(downProb); sum = left + right + up + down; if(sum!=1.0){ showWarning = true; } }
function takeStep(leftProb,rightProb,upProb,downProb){ var left: float; var right: float; var up: float; var down: float; var check: float; left = parseFloat(leftProb); right = parseFloat(rightProb); up = parseFloat(upProb); down = parseFloat(downProb);
check = Random.value;
if(check <= left){
Zombie.transform.position.x -= 2.0; //Add if valid move
}
if((check <= right+left) && (check > left)){
Zombie.transform.position.x += 2.0;//Add if valid move
}
if((check <= right+left+up) && (check > left+right)){
Zombie.transform.position.z += 2.0;//Add if valid move
}
if((check <= right+left+up+down) && (check > left+right+up)){
Zombie.transform.position.z -= 2.0;//Add if valid move
}
//Check for Level Complete
}
Answer by aldonaletto · Oct 20, 2011 at 12:27 AM
When you call some function containing yield, the function is executed until yield is found, then the program flow returns to the caller routine - the system continues the coroutine in the next update cycle automatically, but without modifying the main program flow.
In your delay routine, the timer variable is calculated and compared to Time.time, then the yield is encountered and the flow returns to the main routine. The system will return to the instruction after yield in the next Update, and repeat this until the coroutine ends, but the main flow will never know that.
The way to do what you want is to place all the timed code inside the coroutine, so that things will continue happening in the background. You must avoid the coroutine to be called again while it's running, or a new cycle will be started. The best way is to have a boolean flag set at the beginning and reset at the end: if the coroutine is called while this flag is set, it just returns without doing anything.
private var walking = false;
function Walk(steps: int){
if (walking) return; // don't start another cycle before this one ends
walking = true; // signals a cycle is running
for(temp = steps; temp >0; temp--){
takeStep(leftProb,rightProb,upProb,downProb);
yield WaitForSeconds(1.0);
}
walking = false; // cycle ended
}
...
// then call the walking code this way:
if (GUI.Button (Rect (90, 5, 100, 30), "Take a Walk!")) {
Walk(10);
}
...
Thank you very much. I believe I was making it more complicated in my head than it needed to be! I'm glad I asked though because I did not have a proper understanding of how yield works in this situation. Again, thank you!
Your answer
![](https://koobas.hobune.stream/wayback/20220613014310im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How long does review take? 0 Answers
Clamp animation to a certain amount of time? 1 Answer
Framerate Independent Firing Rate (delta time?) 2 Answers
Slow (spell) Enemy unit, problem with duration 0 Answers
Make events happen at once 0 Answers