Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by BlackWaltz · Oct 19, 2011 at 11:51 PM · time

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

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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);
 }
 ...

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BlackWaltz · Oct 20, 2011 at 12:40 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges