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.
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.
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.
Your answer
Follow this Question
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