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 Joakim · Dec 18, 2012 at 07:20 PM · itweenmoveto

How to make your script wait until function finishes

Greetz, i need a bit of help

I m having a hard time for a few days allready figuring out how to execute multiple iTween.MoveTo functions.

Basically, i need my script to wait for the iTween.MoveTo to finish moving and then proceede to next stuff.

Simple example:

 iTween.MoveTo(tile.gameObject, iTween.Hash("position", destination1, "time", 2f));
 
 //now here script has to wait until upper iTween.MoveTo finishes moving
 
 iTween.MoveTo(tile.gameObject, iTween.Hash("position", destination2, "time", 2f));

In this case, second iTween.MoveTo "kills" first one and only second one is executed.

I tryed to make it with delay property of iTween.MoveTo, but then other kind of problems occur, i.e. MoveTo waits but gets wrong destination point since destination is calculated too early. I tryed with yield but no results, i dont understand that concept quite well.

Anway, i need my script to wait (not completely freeze) until iTween.MoveTo finishes, how could i do it, maybe make a separate thread and then stop one until other finishes or.... ?

Plz help.

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

6 Replies

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

Answer by kurifu · Dec 18, 2012 at 07:51 PM

The easiest way to do this is use launch the movement in a Coroutine, say something like this

 void StartMoving()
     {
         StartCoroutine(DoMoving());
     }
     
     IEnumerator DoMoving()
     {
         iTween.MoveTo(tile.gameObject, iTween.Hash("position", destination1, "time", 2f));
         yield return new WaitForSeconds(2f);
         
         iTween.MoveTo(tile.gameObject, iTween.Hash("position", destination2, "time", 2f));
         yield return new WaitForSeconds(2f);
         
         // ... other sequential actions here?
     }

Now there are ways you can improve this, for instance don't wait on discrete periods of time, I'm not sure offhand if iTween provides some object you can check to see when an action is done but this might be best, or you could even check the distance between the current location and the target location and continuing once you get to approximately 0.

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
0

Answer by justin_iSO · Dec 19, 2012 at 12:13 AM

If you look at the iTween documentation there is a field in the Hash values called "oncomplete". This is a string that is the name of a function to call when the iTween finishes. So just move your code to finish and next iTween call to a separate method and place that method name as the value to "oncomplete". Saves all the extra steps with setting up a coroutine.

~Justin

Comment
Add comment · Show 1 · 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 Joakim · Dec 22, 2012 at 09:59 PM 0
Share

Justin, i think i could not do it like that, i have multiple functions with iTween calls, and those functions have multiple loops with iTween calls in them, + some logic to execute before and after the $$anonymous$$oveTo call. I just cant imagine how could i syncronize it all with onComplete.

Now i made every animation function IEnumerator + i made new IEnumerator function to call those IEnumerator animation functions and i can yield this animation functions (script waits for anim. fun. finish, before proceeding to another animation funciton), and i put a "yield wait for seconds" after every iTween.$$anonymous$$oveTo call... Yeah its not very nice but it works, i could use better solution, probably i should redesign my whole code, but i m fine for now, i m learning : ).

thx

Joakim

avatar image
0

Answer by Piflik · Dec 18, 2012 at 07:29 PM

Have a look at coroutines. I haven't used them myself enough to have more specific advice, but you can make coroutines pause for a certain amount of time, or until another coroutine has completed.

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
0

Answer by TheCheese · Dec 18, 2012 at 07:40 PM

What you're looking for is a coroutine. In c# coroutines look like this:

 void FunctionThatCallsCoroutine(){
     StartCoroutine(ExampleFunction());
 }
 
 IEnumerator ExampleFunction(){
 
     iTween.MoveTo(tile.gameObject, iTween.Hash("position", destination1, "time", 2f));
 
     yield return new WaitForSeconds(2f);
 
     iTween.MoveTo(tile.gameObject, iTween.Hash("position", destination2, "time", 2f));
 
 }
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
0

Answer by Joakim · Dec 18, 2012 at 09:31 PM

Thx guys! i did try coroutines last night and could not get it work.

Now i got it better and it workz!! Thx a lot !

Peace out

Joakim

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 TheCheese · Dec 18, 2012 at 09:33 PM 0
Share

Glad you got it figured out! It would be appreciated if you could check the most useful response as the answer. Thanks.

avatar image Joakim · Dec 22, 2012 at 09:48 PM 0
Share

I did not know i should do that i m new here xD. Ok i did it now, i would like to check 3 answers as correct cos they all are. Thx one more time

  • 1
  • 2
  • ›

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

15 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

Related Questions

iTween: change parameters during the path 1 Answer

Multiple iTween MoveTo's 1 Answer

iTween Help Please MoveTo not working 1 Answer

iTween movetopath 1 Answer

Unknown Identifer 'iTween' 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