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 Alismuffin · Nov 06, 2012 at 12:50 PM · cameralerpwaitforsecondscoroutines

Yield WaitForSeconds not working > once in coroutine

Hey guys!

I am having an issue with a bit of code that was working fine before, but decided to not work 2 days before the project it's for is due.

What I wanted was the camera to Lerp from one point to another taking a certain amount of time. It was then to wait there for a certain amount of time, then go back to it's original position.

here is what the coroutine looks like:

 IEnumerator cameraMove (){
         
         duration = durationToEnd;
         fromPos = startAt;
         toPos = endAt;
             
         // Remove action from character
         yield return new WaitForEndOfFrame();
         controller.GetComponent<SoulInfo>().canAct = false;
         
         // Enable Cinematic override to prevent camera following
         // Player
         mainCamera.GetComponent<WorldControls>().cinematicOverride = true;
         
         activated = true;
         
         yield return new WaitForSeconds(duration + durationToWait);
 
         duration = durationToStart;
         fromPos = endAt;
         toPos = startAt;
         
                 
         yield return new WaitForSeconds(duration);
         
         activated = false;
         
         // Re-enable Cinematic override
         mainCamera.GetComponent<WorldControls>().cinematicOverride = false;
         // Return action to character
         controller.GetComponent<SoulInfo>().canAct = true;
     }



And here is how I am calling it in the Update:

 // Update is called once per frame
     void Update () {
         if(activated){
             //Interpolate from one point to another
             mainCamera.transform.position = Vector3.Lerp(fromPos, toPos, Time.time/duration);
         }
     }


It seems to make perfectly logical sense to me, but does not behave how I'd like.

It DOES move from start to end absolutely fine at the right speed and DOES wait the right amount of seconds at that point.

It DOES NOT get past the second WaitForSeconds()

I tested it with Debug.Log and it stops working as soon as add:

  duration = durationToStart;
     fromPos = endAt;
     toPos = startAt;

before the second WaitForSeconds.

Anyone know what's going on?

Comment
Add comment · Show 9
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 Dave-Carlile · Nov 06, 2012 at 12:56 PM 0
Share

What is the value of durationToStart? And have you verified that value with Debug.Log?

avatar image Alismuffin · Nov 06, 2012 at 12:59 PM 0
Share

durationToStart is a public variable that denotes the length of time it takes to get from the end point to the start point

I have Debugged all duration values and even tried putting other values into the 2nd waitForSeconds to no avail

It seems to dislike doing a second WaitForSeconds after I have assigned values to my variables

avatar image Alismuffin · Nov 06, 2012 at 01:00 PM 0
Share

UPDATE:

Sorry I had not actually tried a literal float

Once I truied a literal float it worked fine

Which is extremely odd. It seems you were definitely on to something with the duration to end var

avatar image Dave-Carlile · Nov 06, 2012 at 01:14 PM 1
Share

It shouldn't make a difference if the value of durationToStart is what you expect. Add the Debug.Log right after you set duration and show the value of duration. Since you're dealing with co-routines, maybe something is altering durationToStart unexpectedly.

avatar image fafase · Nov 06, 2012 at 01:52 PM 1
Share

I do not get how you use the Lerp function. You could have a look there for explanation http://unitygems.com/mistakes1/#lerp .Also, why don't I see the function being called in the Update? I do not see camera$$anonymous$$ove in the update. Just for the final, why don't you make an animation? If I got it right you have your camera moving from A to B and then from B to A. You could have one animation and the same backwards.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by astorga · Nov 06, 2012 at 01:21 PM

Why don't you use iTween?

You can do the movement you want in one code line:

 iTween.MoveTo(camera.gameObject, destPos, moveTime);

Comment
Add comment · Show 3 · 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 Alismuffin · Nov 06, 2012 at 01:38 PM 0
Share

I dont want to spend any more money

avatar image astorga · Nov 06, 2012 at 01:46 PM 1
Share

iTween is free to use, search for it in Asset Store :)

avatar image Alismuffin · Nov 06, 2012 at 02:06 PM 0
Share

Thanks for that I would like to continue this method before importing other scripts though, but I know where to go as a last resort now

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

13 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

Related Questions

Easy transition between multiple views? 1 Answer

Single Camera Lerping between three characters? (code provided) 0 Answers

Lerp and recording Transforms/Vector3 1 Answer

Can't figure out While Loop crash? 1 Answer

How you change another script value smoothly on trigger 0 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