x


Time.deltaTime not set to 0 if Time.timeScale set to 0 too early!

Can someone confirm this bug exists or suggest where to look for my mistake?

Notice that the problem also happens if you try to set timeScale to 0 in Awake(). (which in this case would be the sensible place to put it.)


    void Update() {
        //if(Time.frameCount < 2)    //deltaTime becomes 0.02, not zero.
                                     //same problem if put in Awake();

        if(Time.frameCount == 2)     //works as expected.
            Pause();
        if(Input.GetKeyDown(KeyCode.Space))
            Pause();
        else if (Input.GetKeyUp(KeyCode.Space))
            Play();
        Debug.Log("Delta"+Time.deltaTime);
    }

    void Pause () {
        Time.timeScale = 0;
        AudioListener.pause = true;
    }
    void Play () {
        Time.timeScale = 1;
        AudioListener.pause = false;
    }

}
more ▼

asked Mar 09 at 11:55 PM

DtBeloBrown\'s gravatar image

DtBeloBrown
323 5 6 18

(comments are locked)
10|3000 characters needed characters left
 moderation talk

4 answers:

Time.deltaTime is the time between previous and the current frame, so setting the timeScale within an Update doesn't change the value for deltaTime for the Updates called in the current frame.

more ▼

answered Mar 10 at 03:25 AM

Jaap Kreijkamp\'s gravatar image

Jaap Kreijkamp
5.1k 16 22 66

While this is true, in subsequent calls to Update many frames later, deltaTime should be 0 and I still see it as a steady 0.02 indefinitely (until timeScale is changed again some time after frame count is >= 2). Let me know if you do run it and get similar results.

Mar 10 at 03:40 PM DtBeloBrown

So if I understan it correctly, it works well after frame 2 (deltaTime == 0) but wrong before frame 2 (deltaTime == 0.02)? Apparently Unity's code does force Time.timeScale to 1 in first frame or so. As I've used timeScale == 0 without problems I would suspect it only occurs on first frames after startup, a cheap hack around would be having an startup scene, that waits 2 frames and then loads the real stuff.

Mar 11 at 03:40 AM Jaap Kreijkamp

That is correct. I just ended up waiting for the two frames and starting my scene with no sounds to prevent any audio noises in those first frames.

Mar 26 at 03:10 PM DtBeloBrown

If it is correct, mark your question as answered (by this answer).

Jan 05 at 02:16 AM Socapex_2K
(comments are locked)
10|3000 characters needed characters left
 moderation talk

I've seen this bug too (Unity 2.6.1f3): if I set Time.timeScale in an Awake() call before the first frame, or in an Update() call in the first frame, then Time.deltaTime is not properly adjusted. (Animations do stop, though: I guess they interrogate Time.timeScale directly).

more ▼

answered Jun 12 at 08:21 PM

Gareth Rees\'s gravatar image

Gareth Rees
61 1 1 7

Thanks for confirming that.

Jun 14 at 11:42 PM DtBeloBrown

This is still the case in Unity 3.5.1

1 day ago starkos
(comments are locked)
10|3000 characters needed characters left
 moderation talk

I've bumped into this too... setting Time.timeScale=0.0f in an Awake in the first scene won't change the deltaTime (always 0.02f). this is really annoying...

more ▼

answered Dec 01 at 11:47 AM

arioch82\'s gravatar image

arioch82
1

(comments are locked)
10|3000 characters needed characters left
 moderation talk

Here's a way to work around it:

function Awake() {
   StartCoroutine("Pause");
}

function Pause() {
   yield WaitForEndOfFrame();
   Time.timeScale = 0.0f;
}
more ▼

answered May 15 at 03:13 PM

starkos\'s gravatar image

Jason Perkins
48 1 1 5

(comments are locked)
10|3000 characters needed characters left
 moderation talk
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x343
x289
x28
x25
x1

Asked: Mar 09 at 11:55 PM

Seen: 1661 times

Last Updated: Sep 09 at 09:32 PM

powered by Qato - Enterprise Q&A