For a long time, I didn’t quite understand what is Time.deltaTime, and why we have to use it when we want to move an object smoothly. I had used it for many times, but never understood its meaning.
Today, after I had a big trouble when writing a script of the player’s operation, I decided to find out what it is. I wrote a coroutine function to detect the input of the players. However, sometime the input may be missed in the function, while being able to be detected in the Update() function. To loop the coroutine function I used the line, which seemed like the core of the problem:
yield return new WaitforSeconds(Time.deltaTime);
This blog: http://www.victsao.com/blog/97-unity/438-unity-script-delta-time explains it well.
Time.deltaTime is the “delta” time (seconds) between each Update() function (usually the last one). It isn’t fixed every frame. Sometimes longer, sometimes shorter. Therefore, if we want to move an object with an fixed distance each FRAME, it may look unsmooth, because each FRAME lasts different SECOND. On the other hand, if we used Time.deltaTime (eg. speed * Time.deltaTime) instead, when the time between 2 frames is relatively long, the distant the object move will be longer as well; On the contrary, if the time between 2 frames is short, the object will move a shorter distant, which could smooth the movement.
Back to the problem I met, what yield return new WaitforSeconds(Time.deltaTime); did was wait until the frame ended before looping. During the waiting the player might already press the key yet the function didn’t enter the other loop to respond in time. Hence, I changed it into:
yield return null;
It works. Also, https://github.com/Gwill/learn-unity3d/issues/5 emphasises that only by using yield return null; will the function to be called each FRAME.