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 eaj · Mar 30, 2013 at 11:30 AM · movementcoroutines

Coroutine troubles

Hi,

I'm hoping someone can help me out with this problem. I'm trying to get my GameObject to move along a path, which is stored in an array. The GameObject moves along the path correctly but I'm trying to get the game to wait for the GameObject to finish moving before anything else can be done.

In addition, when I query the value of the busy variable after the line StartCoroutine(moveAlongPath()); busy returns as True, when it should be False. I understand that this is because of the nature of coroutines, they run alongside the current process, but I want to get the value of busy after the movement is complete. If I remove the coroutine of moveAlongPath unity crashes.

I'm using coroutines to smoothly move my GameObject.

Here's the code. Thanks in advance.

 #pragma strict
 
 var busy: boolean = false;
 var path: Array;
 
 function moveLinear(goal: Vector3, rateOfMovement: float){
 
     var startPos: Vector3;
     
     while(true)
     {
         busy = true;
         startPos = transform.position;
         
         if (startPos == goal)
         {
             busy = false;
             break;
         }
         
         transform.position = Vector3.MoveTowards(startPos, goal, Time.deltaTime*rateOfMovement);
         
         yield;
     }
 }
 
 function moveAlongPath () {
 
     var i: int = 0;
     
     while (i < path.length)
     {
         if (!busy)
         {
             Debug.Log(i);
             Debug.Log(busy);
             var goal: Vector3 = path[i];
             StartCoroutine(moveLinear(Vector3(goal.x, 0, goal.z), 10));
             i++;
         }
         yield;
     }
 }
 
 function Start () {
 
     path = new Array ();
     path = [Vector3(10,0,0),Vector3(20,20,0),Vector3(30,0,0),Vector3(40,40,0)];
 }
 
 function Update () {
 
     if (Input.GetMouseButtonDown (0))
     {
         StartCoroutine(moveAlongPath());
         Debug.Log('b  '+busy);
     }
 }
 
Comment
Add comment · Show 3
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 Fattie · Mar 30, 2013 at 12:05 PM 0
Share

dude you can't really go "startpos == goal"

you know it's like comparing floats. (search 100s of questions on it here)

more likely you would do something like sqr$$anonymous$$agnitude less than some small value, or use $$anonymous$$ath.Approximately, or find if you're inside a small AABB or whatever

avatar image Fattie · Mar 30, 2013 at 12:06 PM 0
Share

in a general sense your routine "moveLinear" is a coroutine, it does something, it skips frames while it is doing it, and then, it returns.

so in fact you can very simply write code like:

yield moveLinear();

and that will just wait until your "moveLinear" finishes.

avatar image Fattie · Mar 30, 2013 at 12:09 PM 0
Share

FWIW i would write that routine more like this

 function moveLinear
 {
 busy = true
 while ( sqrDistance > 5 centimeters )
     {
     move a little ..
     yield
     }
 set position to exactly the desired position
 busy = false
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Fattie · Mar 30, 2013 at 12:10 PM

"In addition, when I query the value of the busy variable after the line StartCoroutine(moveAlongPath()); busy returns as True, when it should be False"

no, it should be TRUE.

if you YIELDED to moveLinear it would "wait" there, and on the next line it would be FALSE

But you are not yielding to it. You're just telling it to run in the "background". so it is imemdiately TRUE and stays TRUE for a few seconds or however long it will take to run.

Comment
Add comment · Show 2 · 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 eaj · Mar 30, 2013 at 12:32 PM 0
Share

Yes, of course. Thanks for clearing that up.

yield moveLinear() works a treat and I removed the coroutine of moveAlongPath().

I realized something else. I was querying the value of busy from the update function directly after calling moveAlongPath(). This returns the value of busy immediately, as moveAlongPath() is running. The solution of getting the value of busy after moveAlongPath() was to move this query into moveAlongPath(), at the end of the function. Doing so returns false as moveAlongPath() needs to wait for moveLinear() to complete first.

avatar image Fattie · Mar 30, 2013 at 01:01 PM 0
Share

i only wish i could help more!

here's a very handy trick. just use InvokeRepeating for simple timers in Unity. So, try this.

in Awake or whatever ... InvokeRepeating("TESTE", 0.20 );

then add a simple routine

 function TESTE()
 {
 Debug.Log("the boolean is currently ... "+busy);
 }

you will see this is incredibly useful. watch your console and it will give you a good report on what the hell is happening!

it took me years to understand coroutines as little as I do, by asking the experts on this list over and over

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

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

MoveTowards inside Coroutine 2 Answers

Moving only if straight path dijkstra 1 Answer

Making a bubble level (not a game but work tool) 1 Answer

How to make a 2D object move smoothly in a random direction for a set amount of time. 2 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