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
0
Question by alexandernst · Sep 01, 2016 at 09:13 AM · coroutinemove an objectbezier

Coroutine for moving lights pauses after each iteration

I have a few lights generated in my Start(). Once all lights are generated and assigned colors, range, intensity, etc... I call a coroutine that moves those lights. The coroutine looks like this:

 Vector3 RandomPoint() {
     float screenX = Random.Range(0.0f, Camera.main.pixelWidth);
     float screenY = Random.Range(0.0f, Camera.main.pixelHeight);
     //float ScreenZ = Random.Range(Camera.main.nearClipPlane, Camera.main.farClipPlane);
     return Camera.main.ScreenToWorldPoint(new Vector3(screenX, screenY, paintHeight));
 }

 Vector3[] BezierPath(Vector3 startPoint, Vector3 endPoint) {
     float distance = Vector3.Distance(startPoint, startPoint);

     Vector3 startTangent, endTangent;
     if (startPoint.x < endPoint.x) {
         startTangent = startPoint + Vector3.right * (distance / 3f);
         endTangent = endPoint + Vector3.left * (distance / 3f);
     } else {
         startTangent = startPoint + Vector3.left * (distance / 3f);
         endTangent = endPoint + Vector3.right * (distance / 3f);
     }

     return Handles.MakeBezierPoints(startPoint, endPoint, startTangent, endTangent, BEZIER_PATH_POINTS);
 }

 IEnumerator Move () {
     print("Moving...");

     Vector3[][] light_points = new Vector3[NUMBER_OF_LIGHTS][];
     Vector3[][] light_paths = new Vector3[NUMBER_OF_LIGHTS][];

     for (int i = 0; i < NUMBER_OF_LIGHTS; i++) {
         light_points[i] = new Vector3[2];
         //light_points[i][0] = RandomPoint();
         light_points[i][1] = RandomPoint();
     }

     while(true) {

         System.DateTime d1 = System.DateTime.Now;
         for (int i = 0; i < NUMBER_OF_LIGHTS; i++) {
             light_points[i][0] = light_points[i][1];
             light_points[i][1] = RandomPoint();
             light_paths[i] = BezierPath(light_points[i][0], light_points[i][1]);
         }
         System.DateTime d2 = System.DateTime.Now;
         print("Calc " + (d2 - d1).TotalMilliseconds + "ms");


         for (int i = 0; i < BEZIER_PATH_POINTS; i++) {
             for (int j = 0; j < NUMBER_OF_LIGHTS; j++) {
                 lights[j].transform.position = light_paths[j][i];
             }
             if (i + 1 < BEZIER_PATH_POINTS) {
                 yield return new WaitForSeconds(0.02f);
             }
         }
         print("End");

     }
 }

The problem is that after each iteration of the while loop the lights stop moving for a noticeable amount of time. I thought that it might be caused by the first for loop inside the while, which calls an expensive function (`MakeBezierPoints`) multiple times, but the time diff shows that that piece of code takes barely 50ms.

What is causing that pause? How could I move my lights without any noticeable pauses?

Comment
Add comment · Show 4
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 doublemax · Sep 01, 2016 at 09:23 AM 2
Share

How much is "noticeable amount of time"? A second? $$anonymous$$ore? Less?

barely 50ms

That's an eternity. You're basically limiting your game to a maximum of 20 fps. I can't tell you exactly where the delay comes from, but blocking your code in a coroutine for that long is not good. Remember that a couroutine does not run in another thread. It only gives back control to the main loop when you call yield.

Also: Are the lights moving in a loop? $$anonymous$$aybe it just looks like they're not moving because their end and start positions overlap?
avatar image alexandernst doublemax · Sep 01, 2016 at 09:37 AM 0
Share

You're making a point here. So, how would I move my lights without blocking and without limiting to 20FPS?

The lights are moving in a path calculated by a Bezier curve. The very first loop I generate 2 random points (start and end), after that I swap the starting point with the ending point, then generate a new ending point and then a new Bezier curve. That way the points move into a random path without jumping around.

avatar image doublemax alexandernst · Sep 01, 2016 at 10:03 AM 0
Share

What's the value for BEZIER_PATH_POINTS? I have a feeling you're creating a huge point array there. I'd try creating much fewer points on the bezier curve and do a linear interpolation between the points myself. It will require some additional effort (= code writing), but it'll probably be much less expensive performance wise.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Can you use a character controller in a coroutine? 0 Answers

Click on object shows menu, but also move it. 0 Answers

how to make the camera move backwards when the game is started? 0 Answers

Can you implement the Scene view in the Game view? 0 Answers

Need help with my code 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