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 Gone2Plaid · Jan 23, 2021 at 07:18 AM · coroutinemovetowardswhile loop

[SOLVED] Coroutine while loop exits before condition met

I'm at a loss as to why my coroutine doesn't seem to be working. I'm trying to use MoveTowards to do a smooth translation of my character game object from one hex on the board to the next. I also tried Lerp with the same results. Here's the code:

 private IEnumerator Walk()
     {
         fracDist = Vector3.Distance(transform.position, destination.transform.position);
         while (fracDist > 0.01f)
         {
             Debug.Log("HexUnit.Walk: Before distance = " + fracDist.ToString()
                 + "\n");
             Vector3.MoveTowards(transform.position, destination.transform.position, Time.deltaTime * 1f);
             fracDist = Vector3.Distance(transform.position, destination.transform.position);
             Debug.Log("HexUnit.Walk: After distance = " + fracDist.ToString()
                 + "\n");
             yield return null;
         }
     }

And here's how I call it:

     public void MoveUnit()
         {
             princeAnimator.SetBool("moving", true);
             Debug.Log("HexUnit.MoveUnit: Starting to move\n");
             StartCoroutine(Walk());
             Debug.Log("HexUnit.MoveUnit: Done moving\n");
             HaltUnit();
             Location = destination;
         }

The debug messages are as follows with identical timestamps:

HexUnit.MoveUnit: Starting to move

HexUnit.Walk: Before distance = 17.32051

HexUnit.Walk: After distance = 17.32051

HexUnit.MoveUnit: Done moving

HexUnit.Walk: Before distance = 17.32051

HexUnit.Walk: After distance = 0

HaltUnit() just tells the animator SetBool to go back to false. When I comment out the "Location = destination;" line, I just get non-stop pairs of Before and After distance debug lines reflecting 17.32051. What am I doing wrong either in the while loop or in the MoveUnit() method where I call the coroutine? Thanks in advance.

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
Best Answer

Answer by Llama_w_2Ls · Jan 23, 2021 at 10:11 AM

Coroutines outlive the function that is calling them. This means that any code after the line StartCoroutine(Walk()); immediately runs, regardless of whether the coroutine has finished or not. You should call HaltUnit() in the coroutine, once the while loop has ended, because that's when the player has reached its target. @Gone2Plaid

Comment
Add comment · Show 3 · 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 Bunny83 · Jan 23, 2021 at 02:20 PM 2
Share

Right, in addition I should note that the coroutine will never finish because the $$anonymous$$oveTowards line does nothing. $$anonymous$$oveTowards returns the new position. It can not modify the passed in Vector3. Since the return value is simply ignored the line is pointless. If you want to more the object this scirpt is attached to, you have to do:

 transform.position = Vector3.$$anonymous$$oveTowards(transform.position, destination.transform.position, Time.deltaTime * 1f);
avatar image Gone2Plaid · Jan 23, 2021 at 04:27 PM 0
Share

@Llama_w_2Ls - Thank you so much! The fact that the calling method continues past the coroutine call was what I didn't have my head around.

@Bunny83 - Thanks for the re$$anonymous$$der! I originally had my code exactly as you corrected it, but in my troubleshooting, I had left it incorrect. You saved me a lot of additional frustration.

In addition to Bunny83's correction, I moved the following up to the coroutine after the while loop, and that solved it!

     HaltUnit();
     Location = destination;

Solution accepted and upvotes applied. THANK YOU BOTH!

avatar image Llama_w_2Ls Gone2Plaid · Jan 23, 2021 at 04:28 PM 0
Share

No problem

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

120 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

MoveTowards not moving towards! lol. 1 Answer

IOS Touch movement not exiting coroutine 0 Answers

What is the best method for setting up a variable wait time in a coroutine? 1 Answer

Moving object by path 0 Answers

Problems with While Loops Only repeating Once 1 Answer


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