Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to generate an infinite number of levels (NOT INFINITE RUNNER)

Discussion in 'Scripting' started by romain1994, Oct 15, 2019.

  1. romain1994

    romain1994

    Joined:
    Oct 23, 2018
    Posts:
    26
    hello,
    i was wondering how do game developers usually generate an infinite amount of levels, like you finish a first level and the second one is generated with a slight increase in difficulty.
    I am not talking about an endless runner here, i want multiple level generator here, if you can help.
    thank you in advance.
     
    KosukenSan likes this.
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    The answer is going to be wildly different between different genres, different game rules, what you want the game to do, etc. Random level generation is a very "made to order" sort of thing, and realistically, may end up being most of the work of creating your game, at least if you want the levels to be any good.

    If you want some advice on what direction to start, we will need to know a lot more information about your game before we could even begin to give any advice.
     
    Yoreki likes this.
  3. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    As StarManta said, we'd first need to know what genre we are even talking about, what "difficulty" is in that specific game, and what changes are allowed to a level and what changes are not. With these informations in mind you can then try to write an algorithm to fit your criteria. For some usecases there may already be algorithms you can use and adjust.

    There is probably an infinite amount of ways to approach this problem even with the above information known. Starting with doing mostly random changes (but checking that the level is solvable), to training a neural network to create levels of increasing difficulty (a bit exotic, but possible). Most reasonable solutions are somewhere inbetween and depend on what exactly you want, and how well you can abstract the problem and implement that abstraction.
    So there really is no one right answer to your question, especially with how broad the question was formulated.
     
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,616
    Google for Spelunky articles for a good case-study. The author of that game wrote a very robust and effective level generator and has been writing articles about the "making-of" ever since.
     
  5. mbaske

    mbaske

    Joined:
    Dec 31, 2017
    Posts:
    473
  6. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    The general term for this is procedural (level) generation. Usually you don't technically get an infinite number of possible results, just a very large number.
     
  7. romain1994

    romain1994

    Joined:
    Oct 23, 2018
    Posts:
    26
    hello again,
    first of all thank you for your answers, ill add some details about the game.
    it's a hyper casual, 3D top down camera runner game. each round is about a certain distance and the player has to avoid obstacles(you can think of it as a temple run but with several 30s-1min levels). I currently generate obstacle randomly on the road by instantiating a random obstacle prefab from a list of prefabs all along the road. i can increase difficulty by reducing the space between each wall as well as increasing the speed of the player or even add prefabs the the list when reaching a certain number of levels.
    My aim here and the question i am asking for is:
    -is there a way, when the player finishes the 1st level to save the level he did so he can play the same level again, and create a new level with the increased difficulty ?
     
  8. romain1994

    romain1994

    Joined:
    Oct 23, 2018
    Posts:
    26
    let me know if it was unclear or if you need more information,
    thank you
     
  9. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    How do you generate your randomness? If you use a seed for your generator, the same seed should give you the same level if the order you get the numbers are consistent.
     
  10. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    As recommended before, read up on the procedureal Level design.

    You'll soon come across what is called 'Perlin Noise' which is used for much of the random generation stuff. One of the great properties of Unity's implementation (and many others) is that although the numbers in a sequence vary sufficiently to fulfil random requirements, the always produce the same numbers if you enter the same sequence. So, to randomize your Level, you pick a 'seed' or starting Point, and from there on, all numbers are random from the seed's perspective, but will always return the same random value in the sequence.

    Long Story short: you don't really have to save the Level if you are using such a random Generator, just the seed, and you'll be able to recreate the same Level every time by using the same random sequence.
     
  11. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    There are multiple approaches to this possible but the basic idea is common behind all of them. Create blocks of different complexity and combine them to get total complexity of N from M blocks, where N and M increased every K of levels. And if you're using something line genreated terrain or labyringh then you must denote generation parameters what affects final complexity of level and increment them too.
     
  12. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    To save the level, just keep track of where you placed which object and then save that. However, it is kinda weird if different users have different versions of the game, so i'd generate the levels based on a seed. That way you also do not need to save anything, since the same seed will result in the same level again. For the next level, slightly increase one or more of your difficulty settings and generate a new level based on the new values. However, you may eventually run into problems where you create impossible levels, so you will need to check for that.
    To be honest tho, it does not seem (to me) like such a game can possibly make a case for "infinite" levels. Either you create levels of very little difficulty increase, in which case it may get boring, or you run into the problem of "impossible levels" rather fast, meaning you could have just manually created the levels.
     
  13. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    This may not be the case between different platforms. One needs to make sure he using random generation algorithm wich is stable between platforms if planning to allow cross-platform users to play together.
     
    Yoreki likes this.
  14. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Well, if the level generation isn't linked to difficulty, it should be a worthwhile approach to level generation. Let's say you randomize the layout of a village, and then can have an infinite number of different villages etc, so creating an open world exploration game is a candidate as well. Akthough, I general I agree. I yet have to see a good Level-progressed game that entirely relies on procedural Level Generation. The good ones always seem to have some hand-crafted parts to ensure Quality, Balance and fun.
     
  15. romain1994

    romain1994

    Joined:
    Oct 23, 2018
    Posts:
    26
    thank you very much, a seed is definitely what i need, i will look into it! as for the progression ill try to balance it and improve it as i go, this is mostly a test the seed process is all i was trying to reproduce. thx a alot for the help and for the linked docs!