# simple algorithm for 2D endless runner

Hi everyone, I’m developing a simple 2D endless runner for a thesis where enemies spawn on the right side of the screen and you have to jump over them. Now I have to develop an algorithm or a loop with which the level of difficulty increases and more and more enemies are spawned up to a certain level or the speed of the game is increased. Can someone help me? Thank you for your help

You can use Time.deltaTime to track the time elapsed since the last frame. If you say something like this:

``````private float elapsed = 0f;

private void Update()
{
elapsed += Time.deltaTime;
}
``````

you’ll be able to see how much total time has elapsed since the update started. Then you adjust the speed of the game based on the elapsed time multiplied by some constant

Thank you very much that will help me a lot, but I have a question, what happens if you add a pause menu where the normal time is stopped? Does this time then continue or is it also stopped, or how can I create a meaningful pause menu for this. Best wishes!

You can use Time.TimeSinceLevelLoad to track how long the player’s been playing the current level.

So if you want to ramp up the difficulty to a maximum, you could do something like:

``````    public float m_maxDifficultyTime = 600;    //10 minutes

void Update()
{
//a unit float (0 - 1) value for the current difficulty level:
float difficulty = Mathf.Clamp01(Time.timeSinceLevelLoad / m_maxDifficultyTime);

//Now you can use value to lerp between any two values, e.g.:
float currentSpeed = Mathf.Lerp(MinSpeed, MaxSpeed, difficulty);
float spawnFreq = Mathf.Lerp(MinSpawnFreq, MaxSpawnFreq, difficulty);
}
``````

As long as Time.Timescale is set to 0 while any pause menus are open then timeSinceLevelLoad won’t increase.

Thank you, you help me a lot. If I have any further questions, I’ll let you know!