Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Jovy83 · Dec 12, 2015 at 07:14 PM · unity5coroutinetime.deltatime

Unity coroutine movement over time is not consistent/accurate?

I have a coroutine that moves my Camera upwards each time the player reaches a certain point in the game. I used a coroutine so that the camera will move smoothly over time.

Here's a snippet of my code:

 private IEnumerator MoveCameraUpCoroutine(Vector3 startPos, Vector3 endPos, float duration)
     {
         float elapsedTime = 0;
         while(elapsedTime < duration)
         {
             transform.position = Vector3.Lerp(startPos, endPos, (elapsedTime/duration));
             elapsedTime += Time.deltaTime;
             yield return null;
         }
     }
 
 public void MoveCameraUp(Vector3 startPos, Vector3 endPos, float duration)
     {
         StartCoroutine(MoveCameraUpCoroutine(startPos, endPos, duration));
     }

In my controller script, I just call my coroutine like this:

     cam.GetComponent<CameraMovement>().MoveCameraUp(cam.transform.position,
                                                     new Vector3(cam.transform.position.x, cam.transform.position.y + setupLevel.heightOfBlock, cam.transform.position.z),
                                                     0.1f);

The problem with this is that the camera's movement is not always consistent in terms of where it's supposed to stop. I did some debugging. On the first run, the camera moved to the 0.7864508 yPos. On the second run, the camera moved to the 0.7789915 yPos. etc. It's not consistent.

But when I simply use Translate instead of my coroutine:

 cam.transform.Translate(0, setupLevel.heightOfBlock, 0);

I get consistent end values for the camera's yPos at 0.7876318, which is what I need. But this code does not move the camera smoothly over time which is not what I want. It makes the camera teleport from pointA to pointB instead of moving it over time.

Does anyone know how to fix this coroutine issue? I don't know but I think there's something wrong with my coroutine code. Any help is greatly appreciated.

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

1 Reply

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

Answer by fafase · Dec 12, 2015 at 07:16 PM

You ask the same question on SO and got two answers...Am I missing something? So here is my answer again:

 float elapsedTime = 0;
 float ratio = elapsedTime / duration;
 while(ratio < 1f)
 {
     elapsedTime += Time.deltaTime;
     ratio = elapsedTime / duration;
     transform.position = Vector3.Lerp(startPos, endPos, ratio);      
     yield return null;
 }

With this setup, your loop will run until ratio is 1. When 1, endPos is returned and the loop exits next round.

I think the issue was that you compare elapsedTime to duration. So on the last run, you move then you increase and you compare. As a result, the last increase is not considered and you end up somewhere near the end but not at the end.

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 Jovy83 · Dec 12, 2015 at 07:25 PM 0
Share

Thanks for the answer. Yes I posted this on SO and here as well to see all the possible options I can take. Still fairly new so I'm curious about all the possible solutions.

With that said, I have tried the snippet of code you wrote and it works great! I get consistent end values for my camera's yPos which is what I need. Thanks for the explanation.

avatar image jmonasterio Jovy83 · Dec 12, 2015 at 07:29 PM 0
Share

Shouldn't you mark it as answered, then?

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

34 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 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

yield return StartCoroutine() doesnt wait for coroutine to finish 5 Answers

Time.deltaTime and difference of Time.time not matching 0 Answers

Time.deltaTime is "frozen" when game object is disabled 2 Answers

Should I use a Coroutine function or a Time.deltaTime equation to add to values overtime? 1 Answer

Unity 2D can't resawn because object is destroyed. 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