0

This is a part of my script. The GameObjects don't spawn at the time I expect them to. I tried to change the time scale in relationship to the delay value (i. e. timescale = 1 & baseDelay = .1f to timescale = 10 & baseDelay = 1). This how ever works like a charm and I really don't know why. Is something wrong with my code? Has unity problems with the FixedUpdate and small floats?

Images:


using System; using System.Collections; 
using System.Collections.Generic; 
using UnityEngine;

public class GameController : MonoBehaviour { 
internal int xCount = 5; //bricks in x per line 
internal float spacing = .5f; //space between bricks and margin to edges 
internal float baseDelay = .1f; //time that needs to pass until the next movement internal 
float brickMovementPerStep = .05f; //movement distance per step 
int currentLineNumber = 0; //index for current line

 void FixedUpdate()
 {
 //accurate value of space which needs to pass
 float dist = ((screenSize.x - (2 * spacing + ((xCount - 1) * spacing))) / xCount) * .667f + spacing;

 float stepsPerSecond = 1 / baseDelay; //how many steps are there per second?
 float movementPerSecond = stepsPerSecond * brickMovementPerStep; //how far will a line have moved?
 float requiredTime = dist / movementPerSecond; //how long will a line need to travel until the next one can be spawned?

 timeSinceLastSpawn += Time.deltaTime;
 if(timeSinceLastSpawn >= requiredTime)
 {
     timeSinceLastSpawn = 0; //reset time
     currentLineNumber++;
     SpawnAndStartLevel(); //this instantiates and moves the line, it is moved continously
 }

}

Jesse
  • 3,522
  • 6
  • 25
  • 40
Ultraaa
  • 1
  • 2
  • `FixedUpdate` should be used for Physics operation. You may want to use `Update` instead. If you still want to use `FixedUpdate`, then, use `Time.fixedDeltaTime` instead of `Time.deltaTime` – Hellium Apr 17 '18 at 12:17

2 Answers2

1

Time.deltaTime is to be used in functions that update every frame. FixedUpdate() does not run every frame. FixedUpdate() is for physics - since you aren't doing any physics you should rename it to just Update(). I bet this will fix the anomalies in your timing. Read More

Neil
  • 1,613
  • 1
  • 16
  • 18
0

It is certainly possible that rounding errors occur. I'd recommend using a Coroutine with a yield return WaitForSeconds(float) for this, as you seem to do stuff in a fixed interval.

internal int xCount = 5; //bricks in x per line 
internal float spacing = .5f; //space between bricks and margin to edges 
internal float baseDelay = .1f; //time that needs to pass until the next movement internal 
float brickMovementPerStep = .05f; //movement distance per step 
int currentLineNumber = 0; //index for current line

bool run = true;
bool reset = false;

void Start() {
    StartCoroutine(LineRoutine(baseDelay));
}

public IEnumerator LineRoutine(float delay) {

    while (run){
        float dist = ((screenSize.x - (2 * spacing + ((xCount - 1) * spacing))) / xCount) * .667f + spacing;

        float stepsPerSecond = 1 / delay; //how many steps are there per second?
        float movementPerSecond = stepsPerSecond * brickMovementPerStep; //how far will a line have moved?
        float requiredTime = dist / movementPerSecond; //how long will a line need to travel until the next one can be spawned?
        while (!reset) {

            // stuff to happen every [requredTime] seconds
            yield return new WaitForSeconds(requiredTime);

            currentLineNumber++;
            SpawnAndStartLevel();
        }
        reset = false;
    }
}

btw it doesn't matter whether you use Time.deltaTime or Time.fixedDeltaTime in FixedUpdate, as stated in the unity docs

Note that i don't recalculate the required time every iteration, as it seems to me that it won't be constantly changing. however if you set reset to true, it will leave that loop and recalculate the required time.

spfi
  • 103
  • 6
  • I changed the Time.* and FixedUpdate so it is uniform over all scripts in the project. After editing some code (especially deleting some obsolete stuff i did not include in the code above) it worked again. Thank you for taking your time though, I appreciate your help! – Ultraaa Apr 17 '18 at 13:00