- Home /
Time.deltaTime not working correctly
Something very strange appears to be happening with Time.deltaTime when used in the Update loop in C# Scripts. Time.fixedDeltaTime does not have this problem, and I have not tested it in Javascript.
Essentially, it appears that Time.deltaTime is not correctly calculating or that Update is not being called consistently with each frame because when used in any form (including it's simplest) such as:
using UnityEngine; using System.Collections;
public class example : MonoBehaviour { void Update() { float translation = Time.deltaTime * 10; transform.Translate(0, 0, translation); } }
the script results in inconsistent movement of objects. It was a fellow team member who first noted the bug when a few bullets which had been slowed down for testing started to move more quickly when unrelated character controllers received input from an x-box controller.
I soon discovered that although it is most obvious in this context, it appears to be distorting ALL uses of Time.deltaTime. While I was able to remove it from most of the code and convert most movement systems to be based on physics, there are many other places it is used where timing is vital.
Does anyone know what could be causing this problem, or if it is a bug within Unity itself, and what, if anything, could be done in it's place/to fix it?
My biggest concern is that the character controller Move function normally takes in a value multiplied by Time.deltaTime and I can see this becoming a major problem.
Thanks.
For fun, I'd make public float T=0;
and jam T+=Time.deltaTime;
into update. $$anonymous$$aybe do the same thing with fixedTime or just Time.time as a control. Check if it really is moving wrong.
Do you have a s$$anonymous$$dy frame rate? Since Time.deltaTime measure the frame before, I could see that if your FPS is highly inconsistent that you could possibly have some lag issues-this is theoretical though, I've never had any problems with Time.deltaTime.
This could explain it Peter. Thank you for clarifying how the fps is calculated.
Same here, I wonder why no one found this problem. If I manipulate the object position with Time.deltaTime, the movement is not always consistent, the movement could be faster or slower in Unity Editor, depends on what you selected in Hierarchy tab, and standalone full screen seems slower than webplayer, pretty weird.
This problem not so apparent maybe most Unity games only use the physics engine.
Answer by Eric5h5 · Apr 19, 2011 at 03:25 AM
Time.deltaTime is simply the time since the last frame, so there's not really anything that could go wrong with it. It always works, or at least it's never not worked in the 4 years I've been using Unity. The example code you posted is fine, so you have issues elsewhere. You could be changing Time.timeScale or something.
I spoke with my father (he has been coding since punch cards) and apparently it is possible to get time drift with frame checks because occasionally the call to the clock will fail when too many things attempt to check it at the same time. Without knowing exactly how Unity finds Time.deltaTime it is hard to know exactly what is happening. Regardless, the drift is very small and really can only be seen when I slow everything down, so I'm not going to worry about it. I would like to learn more about how Unity calculates the value, but I will live without it xD. Cheers!
You sir, are my hero!!! You have solved something that has been perplexing me for a few hours now, I forgot that my pause menu simply used time.timeScale, and there's a quit level button there... making me scratch my head and wonder why my timer stopped counting before it called for a timecheck, making it impossible to regain any energy xD! Thank you so much for saving me a few more hairs ^^,
Answer by AngryOldMan · Apr 18, 2011 at 04:58 PM
change Time.smoothDeltaTime to get a less jerky reaction. Time.deltaTime is the time between frames so it's always going to be inconsistent even sometimes when not moving.
EDIT
Ah i see I must have misunderstood, my bad. Instead of using tranform.translate use teansform.position and it will appear to be jerky rather than smoothly moving
Yes, it should be inconsistent to correctly calculate the amount of time passed,but that isn't what my problem is.
The problem is it is not calculating this correctly, resulting in inconsistent movement per second.
The entire point of Time.deltaTime is to make movement and other calculations work independently of frame rate, but frame rate continues to affect my calculations (or something is). In other words... I "WANT" it to be jerky and it's not happening lol if that makes sense.
If anything it's as though it is over compensating for the frame rate.
Answer by Joe 24 · May 18, 2011 at 05:05 AM
Hi everyone Did you get a solution to this? I'm basically calculating the position of a character around a circumference by adding to the angle. I want to multiply the angle by deltaTime so it's consistent with the frame rate, but when I do, the animation starts jerking out, basically the character will move forward in the path but it would irregularly jump back and forward a pixel or two. This is the code I'm using:
angle += increment * Time.deltaTime; angle = angle - (Mathf.Floor(angle/360))*360; Debug.Log(angle); float nZ = Mathf.Sin( angle*Mathf.Deg2Rad) * radious; float nX = Mathf.Cos(Mathf.Deg2Rad*angle) * radious; movement = new Vector3(nX-character.transform.position.x, 0, nZ-character.transform.position.z);
character.Move(movement);
Answer by Gizmoa · Apr 20, 2011 at 04:44 PM
Thanks everyone for your help with this question and the clarifications on how Unity calculates Time.deltaTime.
Answer by flim · Apr 05, 2012 at 03:38 AM
The Time.deltaTime return different result if I didn't select anything in Hierarchy tab, or if the "Maximize on Play" is on.
Also the publish target has different result, the rotation works for web player, but not work in standalone full screen.
Very strange :-|
I found the following code would have affect by the Unity editor:
The value movementX is higher when there are something selected in Hierarchy tab, and smallest when run in "Maximize on Play" is selected.
Code:
void Update() {
mouseX = Input.mousePosition.x;
mouseY = Input.mousePosition.y;
cameraScreenXY = Camera.main.WorldToScreenPoint (transform.position);
deltaX = mouseX - cameraScreenXY.x;
deltaY = mouseY - cameraScreenXY.y;
oldX = transform.localPosition.x;
oldY = transform.localPosition.y;
transform.localPosition = new Vector3 (
transform.localPosition.x + (deltaX * Time.deltaTime * moveSpeed / screenWidth),
transform.localPosition.y + (deltaY * Time.deltaTime * moveSpeed / screenHeight),
transform.localPosition.z);
movementX = (transform.localPosition.x - oldX);
movementY = (transform.localPosition.y - oldY);
}
I found that the Time.deltaTime cause the problem, for instance, keep the the Game window resolution unchange, if I select and unselect object in Hierarchy tab I will get different maximum movementX value.
Is the Time.deltaTime has bug?