Very strange timing issues with time.deltatime

Hey guys,
I have figured out a very strange bug in my game, and I have absolutely no idea how to fix it. The game involved problems sliding down, instantiated at different intervals and moving downwards using Transform.Translate. When I make the game full screen however, the fps drops, and the problems overlap.

This is the code I am using to move the problems

    void Update () {

        if (isMoving)
        {
            this.transform.Translate(Vector2.down * (52 + GameObject.FindGameObjectWithTag("GM").GetComponent<gameMaster>().currentDifficulty * 1.28f) * Time.fixedDeltaTime );
            checkIfDefeated();
        }
       
    }

And this is the code I am using to instantiate them

    public void spawnProblem()
    {
        Debug.Log("Problem spawned.");
        GameObject problem = Instantiate(problemPrefab, GameObject.FindGameObjectWithTag("GameCanvas").transform);
    }
   

    public void setNewInterval()
    {
        nextProblemInterval = 2.25f + Random.Range(0.92f, 1.12f) - GameObject.FindGameObjectWithTag("GM").GetComponent<gameMaster>().currentDifficulty * 0.057f;
        if (nextProblemInterval <= 1.15f)
            nextProblemInterval = 1.15f;
        Debug.Log("Interval :" + nextProblemInterval);
    }
   
    // Update is called once per frame
    void Update () {

        if (hasStarted)
        {
            spawnTimer += Time.deltaTime;

            if (spawnTimer > nextProblemInterval)
            {
                spawnProblem();
                setNewInterval();
                spawnTimer = 0f;
            }
        }
       
    }

Please help me guys, I have absolutely no idea how to solve this.


First:
Do not use this

GameObject.FindGameObjectWithTag("GM").GetComponent<gameMaster>()

in an Update loop, cache it and use the cached version.
I guess you have similar approach in the checkIfDefeated() method as well.
The same with the

GameObject.FindGameObjectWithTag("GameCanvas").transform

Do not need to find it every time. Just cache it in a variable at Start or Awake.

Sorry, thank you so much. What about the timing problem ? Any guess at to what it is ?

Could you please elaborate what the problem is? Because you haven’t said anything other than ‘strange’.

Look at the two screenshots I posted. The problems in my game overlap in fullscreen, but instantiate properly in windowed mode, most likely because of the fps. I am trying to get the problems to not graphically overlap, even when the game is lagging.

I’m not sure, because I have no idea, how it suppose to work, but you probably would like to replace the fixedDeltaTime with deltaTime.

1 Like

The instantiation acts the same regardless of time.deltaTime or time.fixedDeltaTime. It also doesn’t change with fixedUpdate or Update. What confuses me is that I’m using a timer with time.deltaTime to instantiate problems, and a Transform.Translate with time.deltaTime to move them, and yet I feel like one of them is somehow out of sync. I have no idea why the change in fps would modify the movement speed of the objects in my game, and yet would make the instantiation at the normal speed. Basically, while the objects move slower, the instantiation is still the same – but they are both involved through time.deltaTime, so why would they be so different ?

  • you want to use Time.deltaTime all the time, if you put it in the FixedUpdate then it will return with the fixedDeltaTime, so does not matter, but in Update you want to use the deltaTime since you need the last frame time
  • for now, change your Random to a constant (just for test), then check the console and then check the build’s output.txt and check if the numbers are the same
  • if the numbers are the same, you’re experiencing some graphics issue, if different, then your Math is wrong
  • also check the Player log because you may have some problems in the build so you can have some idea, what’s happening

or it’s nothing to do with fps and your just running into a problem with those fixed values in the script not applying to resolutions other than the one you used in the editor… ? those float values seem a little arbitrary