Hi guys.
I have 3 objects with the same script attached. This script has a coroutine. For any reason Time.deltaTime is not working the same way in Update function and inside the coroutine. The visible result of this is Time.deltaTime grows very significantly inside the coroutine, and as i’m using Time.deltaTime to move this objects along a path at constant speed, when the first object reaches a certain point in the path where the coroutine takes control the speed of the object running the coroutine is x20 normal speed (the other objects keep moving at normal speed until they reach the same point). The coroutine calculates the speed the same way than Update function:
Position = Position + Time.deltaTime/TotalTimeOfPath;
When i debug the game i see the values of Position grow faster (and constantly) inside the coroutine. Do you know what is happening?
Since you mentioned you are calling your coroutine from update I’m going to assume that it is not checking if the coroutine has already been started so the update loop will start yet another coroutine on future updates. Eventually you get many coroutines updating the object’s position and it gives you that effect you speak of (where the speed increases dramatically). Wrap your StartCoroutine call with an If condition that makes sure it does not get called while the same coroutine is already running.
You can do this using a flag like such:
private bool m_coroutineStartedFlag = false;
private void Update()
{
if (!m_coroutineStartedFlag)
{
StartCoroutine(MyCoroutine());
}
}
private IEnumerator MyCoroutine()
{
m_coroutineStartedFlag = true;
//do code and yielding until done.
m_coroutineStartedFlag = false;
}
This thread is super old but I found that I had a similar issue that was being caused by a debug statement that I had in a while loop that was in a coroutine I was running. Hopefully this helps some poor lost soul.