Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by Rodlaiz · Dec 14, 2016 at 09:52 AM · coroutinepausecoroutineswhile loopreturn value

Why my Coroutine does unwanted pauses?

I'm using a Coroutine to launch a hook to an enemy and switch places with him on a grid. Everything is working as expected except that I have unwanted pauses between each while inside my coroutine and also at the end of the function. Here's my code:

 IEnumerator HookSmooth(GridPoint2 target, float lineDrawSpeed)
     {
         GameObject enemy = goulsGrid[target].transform.GetChild(0).gameObject;
         line.SetPosition(0, transform.position);
         float counter = new float();
         float dist = Vector3.Distance(transform.position, goulsGrid[target].transform.position);
 
         Vector3 pointA = transform.position;
         Vector3 pointB = goulsGrid[target].transform.position;
 
         while (counter < dist)
         {
             counter += 0.1f / lineDrawSpeed;
             float x = Mathf.Lerp(0, dist, counter);       
             Vector3 pointAlongLine = x * Vector3.Normalize(pointB - pointA) + pointA;
             line.SetPosition(1, pointAlongLine);
 
             yield return 0;
         }    
                  
         counter = 0;
         while (counter < dist)
         {
 
             counter += 0.1f / lineDrawSpeed;
             float x = Mathf.Lerp(0, dist, counter);
             Vector3 pointAlongLine = x * Vector3.Normalize(pointB - pointA) + pointA;
             line.SetPosition(0, pointAlongLine);
             
             //Move Player
             transform.position = pointAlongLine;  
             //Move Enemy
             enemy.transform.position = Vector3.Lerp(pointB, pointA, counter);
             
             yield return null;
         }
 }

The question is why I have a pause between the two while loops and also a pause at the end of my coroutine? I'm guessing that the yield return null lines are somehow producing a pause?

Comment
Add comment · Show 2
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 · Dec 14, 2016 at 01:48 PM 0
Share

ps: Never use yield return 0;. It creates garbage on the heap because "0" is a value type and need to be boxed. If you want to wait one frame, just use yield return null;. That doesn't create any garbage.

avatar image Rodlaiz Bunny83 · Dec 14, 2016 at 02:08 PM 0
Share

Thanks for that, I didn't know that little nugget :)

1 Reply

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

Answer by doublemax · Dec 14, 2016 at 10:08 AM

I don't think the problem is directly related to the coroutine.

The Lerp calls expect an "t" input in the range from 0 to 1. So if "dist" is >1, you won't see any movement from the moment "counter" reaches 1.0.

Comment
Add comment · Show 4 · 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 Rodlaiz · Dec 14, 2016 at 10:39 AM 0
Share

@doublemax Thank you for the answer. But in that case, shouldn't the while loop stop (and with it, the coroutine as well) when "dist" is > 1 ? Is there a way to prevent very small difference numbers on "counter"?

avatar image doublemax Rodlaiz · Dec 14, 2016 at 10:44 AM 1
Share

Just suppose dist = 5. Then counter will run from 0 to 5. But only in the range from 0 to 1 will you see any movement, because Lerp will just return the endvalue for t > 1.

Try something like this (and the same of the other loop):

 while (counter < dist)
 {
    counter += 0.1f / lineDrawSpeed;
    float t = counter / dist;
    float x = $$anonymous$$athf.Lerp(0, dist, t);       
    Vector3 pointAlongLine = x * Vector3.Normalize(pointB - pointA) + pointA;
    line.SetPosition(1, pointAlongLine);
 
    yield return 0;
 }    
avatar image Rodlaiz doublemax · Dec 14, 2016 at 01:09 PM 1
Share

It works like a charm. Thank you very much for your help and explanation. I understood what I was doing wrong in my method.

avatar image Bunny83 · Dec 14, 2016 at 01:43 PM 1
Share

Converted into an answer and moved the other comments as well ^^.

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

6 People are following this question.

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

Related Questions

How to get the Progress from a IEnumerator 0 Answers

Coroutine Not working properly. 1 Answer

Stopping Couroutine 0 Answers

Dialog manager - show full sentence before going to next sentence 0 Answers

c# Coroutines and Waypoints HELP PLS!!!,C# Coroutine and Waypoints Help pls!!! 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