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 icelated · Aug 27, 2012 at 07:08 AM · c#

WaitForSeconds not working at all C#

I have a class file that zooms the camera for aiming. I also have a zoom animation for my gun. What i want to do is wait for the gun to catch up some before i do the camera zoom. It zooms by the right mouse button down event

However, it just doesnt seem to do anything like the function is not being called. I have looked at numerous examples and i dont see what i am doing wrong?

UPDATE: I got it fixed. I will revise my code to reflect that incase anyone else has problems down the road.

 void Update () {
         
         
     StartCoroutine("fieldOfView");
             
     }
 
 
     IEnumerator fieldOfView(){   // Not IEnumerable
         
         if(Input.GetMouseButtonDown(1)){
             
             aim = true;
             
             ready = false;
             
         }
         if(Input.GetMouseButtonUp(1)){
             aim = false;
         }
         
         if(aim){
             
             yield return new WaitForSeconds(2);
              
             cam.camera.fieldOfView = Mathf.Lerp(cam.camera.fieldOfView, distance, speed * Time.deltaTime);
         }
         if(!aim){
     
             cam.camera.fieldOfView = Mathf.Lerp(cam.camera.fieldOfView, original_position, speed * Time.deltaTime);
         }
         
         
         
     }
         

Every example i have tried has not worked.

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
0
Best Answer

Answer by lvictorino · Aug 27, 2012 at 07:28 AM

It seems to actually work perfectly in your code.

Starting a coroutine means "executing another piece of code at the same time that your current piece of code". So you're just "waiting" but this does only pause the coroutine code... not the main "thread".

Even if it doesn't seem to wait 5 seconds it actually does, but not in your main execution thread that's why it seems buggy for you.

I would suggest something using a boolean to control your flow. Something like this:

 void Update () 
 {
   if ( ready == false ) // check if you're no in the 5s delay
   {
     return; 
   }

   if(Input.GetMouseButtonDown(1))
   {
     aim = true;
     ready_to_anim = false; // set the boolean to false == your not allowed to do something for 5s
     StartCoroutine("wait");
     return;
   }
   // rest of your code
 }

 IEnumerator wait() 
 {
    yield return new WaitForSeconds(5);
    ready_to_anim = true; // delay is over now you can do something
 }
Comment
Add comment · Show 5 · 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 icelated · Aug 27, 2012 at 04:44 PM 0
Share

I tried your way and it just doesnt zoom now.

avatar image icelated · Aug 27, 2012 at 04:49 PM 0
Share

I think what i am going to try to do is throw the ifs statements in a function and then just call that function in update. Then, i could pause that function?

avatar image icelated · Aug 27, 2012 at 04:52 PM 0
Share

That did the trick. It works now.

avatar image Bunny83 · Aug 27, 2012 at 04:59 PM 0
Share

Early exits can produce cleaner code, but only if it's possible to use them. In this case an early exit doesn't make any sense. It will ignore all other code in Update as long as "ready" is false.

I guess the trigger condition should be something like:

 if(ready && Input.Get$$anonymous$$ouseButtonDown(1))

I'm not sure what the delay should do since it has no function in the question code. It would help to know what's the desired behaviour.

avatar image icelated · Aug 27, 2012 at 05:02 PM 0
Share

Bunny - i think that would work however i moved the code inside a function and its working now.

avatar image
0

Answer by Ashkan_gc · Aug 27, 2012 at 07:35 AM

I did not read the code carefully to trace it and see what in each state will happen but your coroutine is returning IEnumerable instead of IEnumerator and it should be the reason for it to not work.

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

10 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

Related Questions

A node in a childnode? 1 Answer

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

How do i start a game with Unity, what do i need first? 0 Answers

Not getting component properly 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