- Home /
How to gradually increase difficulty.
Hey, I wanted to know a way to increase difficulty of game, gradually with time. I am using coroutines to spawn enemy ships, and I decrease the wait for seconds time as the game goes on to ramp up difficulty. But, if someone plays exceptionally well and survives for more than 5 minutes, there are crazy amount of enemies spawning, resulting in crashing of game. So can someone just give me the logic to control the game difficulty. BTW GAME IS ENDLESS RUNNER SORT OF.
Answer by RadonRaph · Jan 29, 2019 at 09:10 AM
Hello, you could use a math fonction like Mathf.sqrt(Time.deltaTime/1000); This number will progress slowly with time.
@WhiteCry should i use the above mentioned func. inside waitforseconds. But, then that means that the no. of enemies spawning decreases(as time interval between each one is increasing) with time, implying difficulty decreases with time.
So, can you please elaborate the above mentioned code a bit.
I don't know how you spawn your enemies so i can't do math without value. I think you can try something like this public float spawnDelay; public float difficultyFactor = 60; // Use this for initialization void Start () { StartCoroutine(spawnEnemies()); } IEnumerator spawnEnemies() { int nbEnemies = $$anonymous$$athf.RoundToInt($$anonymous$$athf.Sqrt(Time.time / difficultyFactor)); for (int i = 0; i < nbEnemies; i++) { //SPAWN YOUR ENE$$anonymous$$IE } yield return new WaitForSeconds(spawnDelay); StartCoroutine(spawnEnemies()); }
By default i have set a factor to 60 that means the curve will be: https://puu.sh/CG6fx/a2b23d673a.png The x axis is the time in seconds and the y axis is the number of enemies. So at 5$$anonymous$$ every spawndelay 2 enemies spawn. You can multiply the nbEnemies to accelerate the spawn Raph
$$anonymous$$ore clearly code: https://pastebin.com/A36qr492