Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Hsmblada · Aug 12, 2015 at 01:47 PM · c#transformcoroutineloopdelegate

Executing coroutines consecutively

So I have a coroutine which rotates and moves an object. First it should rotate and after it finished doing that, then it should move to its target position. I have two coroutines for handling the rotating and moving, which looks like this:

 public IEnumerator CoRotateAndMove(Vector3 targetPosition, Quaternion newRotation) {        
         yield return StartCoroutine(Rotate(newRotation));
         yield return StartCoroutine(Move(targetPosition));
 }
 
 public IEnumerator Move(Vector3 targetPosition) {
         while(Vector3.Distance(transform.position, targetPosition) > 0.05f) {
             transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * moveSpeed);
             yield return null;
         }
     }
 
 public IEnumerator Rotate(Quaternion newRotation) {
         for(float t = 0.0f; t < 1; t += Time.deltaTime) {
             transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, t);
             yield return null;
         }
     }

Additionally I've built a system for queueing commands which uses delegates to store the function to call and a custom class for handling the parameters. I add these commands to my queue List during the course of a player's turn and when it's time to execute I call the functions in a loop like this:

 void EndTurn() {
     foreach(Tuple t in actionQueue) {
         t.T1.Invoke(t.T2);
     }
     actionQueue.Clear();
 }

So to finally get to the problem: When I queue two move commands with a unit, let's say first to point A and then to point B, the unit should first move to point A and then to point B. What it actually seems to do though is, it tries to execute both commands simultaneously which results in it moving what looks to be somewhere in the middle of A and B where it gets stuck (doesn't take any more commands and visibly shakes around like crazy).

Does anyone have any idea what might cause this behaviour?

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 Scribe · Aug 12, 2015 at 03:51 PM 1
Share

Am I right in thinking t.T1 is your monobehaviour instance, and t.T2 is your coroutine then? Don't you need to be yielding in the foreach loop, I would have expected a rotation and movement to also occur simultaneously with how they look to be being called. I'm calling this pseudo code as I have no way of testing it, but it might work:

 IEnumerator EndTurn() {
     foreach(Tuple t in actionQueue){
         yield t.T1.Invoke(t.T2);
     }
     actionQueue.Clear();
 }

avatar image Hsmblada · Aug 12, 2015 at 06:36 PM 1
Share

Sorry, I forgot to include this bit for the custom Tuple class:

 public class Tuple {
 
     public Func T1;
     public DataObject T2;
 
     public Tuple(Func T1, DataObject T2) {
         this.T1 = T1;
         this.T2 = T2;
         }
     }

Func refers here to the delegate which references the function, the DataObject holds the parameters for this function. I then want to call each function in my List with its respective parameter. How can I yield in the EndTurn function/coroutine? Because the functions I call in the loop aren't coroutines, they are just normal methods that themselves start the coroutines. Do I have to make these coroutines as well then?

Edit: I found a solution! Just as I said, by transfor$$anonymous$$g all methods to coroutines and then yielding in the foreach loop, the coroutines will wait until the previous one has finished, so I can chain function calls after each other. Thanks for the tip!

avatar image Scribe · Aug 13, 2015 at 08:09 AM 0
Share

Good job on getting it solved. Alternatively you could have had some other callback to know when you were finished on one and could start the next, but the coroutine idea sounds simpler!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

function behaves unexpectedly inside a coroutine. 2 Answers

make events occur only at specific times 1 Answer

Loop Coroutine For A Demo Mode 1 Answer

Code working on wrong axis. 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