简体   繁体   中英

Time.DeltaTime suddenly stops working in Unity

So I've been making a 2D shooter game in unity and I run my game on a global timer using the code timer += Time.deltaTime this usually just gives me a timer that works like a stop watch however suddenly the value of my timer variable just fluctuates at around 0.0003-0.0001 when running the script when it should be going 1 2 3 4 5 etc for every second. It has done it before and without me changing anything to my knowledge it just fixed itself after 30ish mins but now it's happened again. Does anyone know why this is?

float time = 0f;
float timer = 5f;

void Update()
{
    time += Time.deltaTime;
    if(time >= timer)
    {
        // Do something
    }
}

The code is adding 1f to time every second. Using Time.deltaTime makes it so that it will be the same at any framerate (for example if the framerate is 30fps, it won't add a second 30 times in one second ) and if time (the amount of seconds) surpasses timer, we run what is in between the if statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM