Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Bombastrelopas · Aug 25, 2016 at 08:33 PM · variablecoroutine errorsendless loop

Endless while loop in coroutine which uses condition changing in Update() function.

I have been trying to make a series of Lerp move functions work one after the other for an intro to my simple racing game. (Camera starts in some place moves a bit then teleports to some other place moves a bit and so on). The way I have been tackling this problem is with the following code:

 using UnityEngine;
 using System.Collections;
 
 public class PreviewCamera : MonoBehaviour {
     public Transform[] cameraPos;
     public bool finished = false; 
     public float fraction = 0;
     public float speed = 0.2f;
 
     //Fill the array with the caamera positions for the race preview
     void Start () {
         //Fill the array (indexes 0 and 1 will be useless)
         cameraPos = transform.parent.GetComponentsInChildren<Transform>();
         //Set first camera at position 2
         transform.position = cameraPos[2].position;
         transform.rotation = cameraPos[2].rotation;
         StartCoroutine(RunPreviewCamera());
 
     }
     
     // Update is called once per frame
     void Update () {
         if (fraction <= 1)
         {
             fraction += Time.deltaTime * speed;
         }
 
     
     }
 
 
     IEnumerator RunPreviewCamera()
     {
         StartCoroutine(MoveCamera(transform, cameraPos[3]));
         yield return null; 
 
 
     }
 
     //Function to teleport camera from one position to another
     void TeleportCamera( Transform teleportPos)
     {
         transform.position = teleportPos.position;
         transform.rotation = teleportPos.rotation;
 
     }
 
     //Function to move camera from current position to another
     IEnumerator MoveCamera(Transform fromPos, Transform movePos)
     {
         fraction = 0;
         while(fraction < 1) 
         {
             transform.position = Vector3.Slerp(fromPos.position, movePos.position, fraction);
             transform.rotation = Quaternion.Slerp(fromPos.rotation, movePos.rotation, fraction);
         }
         yield return null;
 
     }
 }

What I supposed would happen is that since the fraction variable changes in the Update() eventually it would escape from the loop in the coroutine MoveCamera but it doesn't and it crashes unity every time I run the game. I suppose update() is not being called if MoveCamera doesn't finish? Is my way of thinking wrong? Any way to do it via scripting and not just making 4 different cameras in my scene and calll them to make the moves I want one by one?

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

2 Replies

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

Answer by ScaniX · Aug 25, 2016 at 08:36 PM

You need to yield inside of the loop.

 //Function to move camera from current position to another
  IEnumerator MoveCamera(Transform fromPos, Transform movePos)
  {
      fraction = 0;
      while(fraction < 1) 
      {
          transform.position = Vector3.Slerp(fromPos.position, movePos.position, fraction);
          transform.rotation = Quaternion.Slerp(fromPos.rotation, movePos.rotation, fraction);
          yield return null;
      }
  }
Comment
Add comment · 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
1

Answer by DiegoSLTS · Aug 25, 2016 at 08:38 PM

Move the yield return null inside the while loop. The code inside a coroutine between the start and a yield or between 2 yields will run completelly before returning control to the rest of Unity methods. Your loop won't finish because the coroutine isn't letting the Update run, since fraction is less than one.

Anyway, that code is too complex for something really simple. I don't have much time right now, but later I'll tell you how to clean that A LOT.

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 Bombastrelopas · Aug 28, 2016 at 04:24 PM 0
Share

Thanks for the detailed explanation!

avatar image DiegoSLTS Bombastrelopas · Aug 28, 2016 at 04:39 PM 0
Share

I never wrote the simplified version I said I'd write. Here it is:

 using UnityEngine;
 using System.Collections;
 
 public class PreviewCamera : $$anonymous$$onoBehaviour {
     public Transform[] cameraPos;
     public bool finished = false; 
     public float speed = 0.2f;
 
     //Fill the array with the caamera positions for the race preview
     void Start () {
         //Fill the array (indexes 0 and 1 will be useless)
         cameraPos = transform.parent.GetComponentsInChildren<Transform>();
         //Set first camera at position 2
         transform.position = cameraPos[2].position;
         transform.rotation = cameraPos[2].rotation;
         StartCoroutine($$anonymous$$oveCamera(transform, cameraPos[3]));
     }
 
     //Function to teleport camera from one position to another
     void TeleportCamera( Transform teleportPos)
     {
         transform.position = teleportPos.position;
         transform.rotation = teleportPos.rotation;
     }
 
     //Function to move camera from current position to another
     IEnumerator $$anonymous$$oveCamera(Transform fromPos, Transform movePos)
     {
         float fraction = 0;
         while(fraction < 1) 
         {
             transform.position = Vector3.Slerp(fromPos.position, movePos.position, fraction);
             transform.rotation = Quaternion.Slerp(fromPos.rotation, movePos.rotation, fraction);
             fraction += Time.deltaTime * speed;
             yield return null;
         }
     }
 }

Starting a coroutine that starts a coroutine is useless unles you're waiting for it to end to do something else (with a yield return StartCoroutine). You can keep track of the time inside the coroutine itself ins$$anonymous$$d of Update. This also removes the need of having fraction as a class member. I said it would be "A LOT" cleaner but I was wrong, just simplified it a bit. Hope it helps somehow, anyway.

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

Assigning current color to a variable for fade out (C#) 0 Answers

How to make a var "global" 0 Answers

Custom Inspector - How to add functionality? 1 Answer

Accessing Variables Through Scripts 1 Answer

Adding and Removing to a Inbuild Array 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