Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Unity coroutine movement over time is not consistent/accurate?

Discussion in 'Scripting' started by Jovy83, Dec 12, 2015.

  1. Jovy83

    Jovy83

    Joined:
    Feb 28, 2015
    Posts:
    15
    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:
    Code (CSharp):
    1. private IEnumerator MoveCameraUpCoroutine(Vector3 startPos, Vector3 endPos, float duration)
    2. {
    3. float elapsedTime = 0;
    4. while(elapsedTime < duration)
    5. {
    6. transform.position = Vector3.Lerp(startPos, endPos, (elapsedTime/duration));
    7. elapsedTime += Time.deltaTime;
    8. yield return null;
    9. }
    10. }
    11.  
    12. public void MoveCameraUp(Vector3 startPos, Vector3 endPos, float duration)
    13. {
    14. StartCoroutine(MoveCameraUpCoroutine(startPos, endPos, duration));
    15. }
    In my controller script, I just call my coroutine like this:
    Code (CSharp):
    1. cam.GetComponent<CameraMovement>().MoveCameraUp(cam.transform.position,
    2. new Vector3(cam.transform.position.x, cam.transform.position.y + setupLevel.heightOfBlock, cam.transform.position.z),
    3. 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:
    Code (CSharp):
    1. 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.
     
  2. Sose

    Sose

    Joined:
    Dec 10, 2015
    Posts:
    27
    Well one possible reason is that elapsedTime < duration doesn't necessarily always break on exactly the same elapsedTime value. You could instead split the operation to integer amount of movements.

    Or maybe simply set your camera's position to endPos after the while loop so it'll be set to the "exact" endPos passed after the coroutine finishes.
     
    Jovy83 and Munchy2007 like this.
  3. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    Use Vector3.MoveTowards instead of .Lerp
     
    Jovy83 and Munchy2007 like this.