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.

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.

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);

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

Thanks everyone for your help with this question and the clarifications on how Unity calculates Time.deltaTime.

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 :expressionless:

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?

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 :expressionless:

I found the following code would have affect by the 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?

Try to put your code in FixedUpdate() and not Update(), and see what happen.