Odd twitching when using deltaTime
Was scratching my head over why an object was twitching every second or so and isolated the problem to the use of deltaTime.
rotatingObject.transform.localRotation = Quaternion.AngleAxis(10 * Time.deltaTime, Vector3.back);
Debug.Log(rotatingObject.transform.localRotation.z);
This is a dumbed down version of the code I'm using - I've disabled everything else for the sake of troubleshooting. If I remove Time.deltaTime, the log shows a perfectly consistent rotation angle, but when I include it, the log shows a very minor but constant fluctuation in rotation with occasional jumps to a seemingly random value.
I'm aware that Time.deltaTime is supposed to even out movements between frames, but I can't see why it would cause micro movements in an unmoving object, nor why it would cause the object to freak out. smoothDeltaTime hasn't worked either - am I missing something?
Answer by MagicMissile · Aug 14, 2017 at 03:04 PM
Nevermind - I think I've figured it out.
There's no reason I should be using Time.deltaTime within that particular function because it's setting a destination rotation, not a rate of rotation. By using Time.deltaTime, it's rotating by the total rotation amount divided by the time between frames, which is why the amount is generally consistent and the jumps are because of momentary frame latency.
Can't test - it's past 1am and I'm tired - but in case anyone else finds this, I goofed completely bc I didn't know what I was doing. It happens.