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 chayanvinayak · Mar 27, 2014 at 08:44 PM · coroutineyieldstartcoroutine

JS Wait for a function to complete - no WaitForSeconds

Hello,

There are number of threads on 'wait for function to finish or complete', allot of them either use wait for Seconds or number of frames or they are not appropriate to this problem.

I am look for a Javascript answer without the use of waitForSeconds(), instead it should just wait until all the commands inside a function are executed.

What I am trying to do:

     OnMouseDown(){
          1. rotate an object by 180 ( wait until object is completely rotated).
          2. switch camera view.
     }

My code :

         function OnMouseDown(){
             yield rotate(menuScreen,-180.0,true); // yield StartCoroutine(rotate(menuScreen,-180.0,true));
             camOne.camera.enabled = true;
             cameTwo.camera.enabled = false;
         
         }
         
         function rotate(gObj:GameObject, limit:float) {
             var upperLimit = gObj.transform.rotation.y;
             var lowerLimit = limit;
             for(var i: float = upperLimit; i> lowerLimit; i-=0.1){
                 gObj.transform.rotation.y = i;
                 yield ;
             }
         }
     

Problem with current code : Function waits for object to rotate but it does not proceed further after completely rotating the object i.e. in this case camera does not switch.

Notes :

  • If I remove any 'yield', unity in a blink finishes the rotation and switch to another camera.

  • I believe, yield waits for single frame and that is fine in this case.

Thanks

Comment
Add comment · Show 1
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 highpockets · Mar 27, 2014 at 09:26 PM 0
Share

Try using:

 gObj.transform.eulerAngles.y

Ins$$anonymous$$d of:

 gObj.transform.rotation.y

You may have to define the whole Vector3 though:

 gObj.transform.eulerAngles = Vector3( gObj.transform.eulerAngles.x, i, gObj.transform.eulerAngles.z );

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by chayanvinayak · Mar 28, 2014 at 01:07 AM

thanks highPocket.

To explain what was happening:

When I used :

 for(var i: float = upperLimit; i> lowerLimit; i-=0.1){
      gObj.transform.rotation.y = i;
      yield ;
 }

At i = -1 , rotation of gObj became -180. Beyond -1, the object was not rotating (value of i was increasing slowly), but as it was not giving any visual change it appeared to be frozen.

Working Code :

 function OnMouseDown(){
     yield rotate(menuScreen,-180.0,true); // yield StartCoroutine(rotate(menuScreen,-180.0,true));
     camOne.camera.enabled = true;
     cameTwo.camera.enabled = false;
 }
 
 function rotate(gObj:GameObject, limit:float) {
     var upperLimit = gObj.transform.eulerAngles.y;
     var lowerLimit = limit;
     for(var i: float = upperLimit; i> lowerLimit; i-=0.1){
        gObj.transform.eulerAngles = Vector3( gObj.transform.eulerAngles.x, i, gObj.transform.eulerAngles.z );
        yield ;
     }
 }
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

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

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How To Create Coroutine Delegates? 1 Answer

Yield position within while loop in coroutine 1 Answer

Coroutines and states 1 Answer

StartCoroutine important for using yield? 1 Answer

Fire and forget coroutines 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