aresz
aresz

Reputation: 2649

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.

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.

Upvotes: 0

Views: 1631

Answers (2)

Everts
Everts

Reputation: 10721

    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.

Upvotes: 0

Venkat at Axiom Studios
Venkat at Axiom Studios

Reputation: 2516

The result is actually quite consistent with what's expected. There's absolutely nothing wrong with your code, rather it's because of of differences in frame rates.

The reason you're seeing the minor differences is because there's no guarantee that two frames will take the exact amount of time to render. Take a look at this line

elapsedTime += Time.deltaTime;

What you're doing here is adding an inconsistent value to your elapsed time. (i.e. Time.deltaTime is different every frame).

One partial fix could be to use Time.smoothDeltaTime instead, which is a smoothed out value for deltaTime over several frames. This, however is not going to be perfect.

A second approach, (not entirely an answer per se, but I'm leaving this here for others as well) is to use a tweening engine such as DoTweenor iTween. These engines have methods that essentially do what you're trying to.

Upvotes: 1

Related Questions