Search Unity

generate random 2d level

Discussion in '2D' started by SamVorst, Dec 3, 2019.

  1. SamVorst

    SamVorst

    Joined:
    Nov 28, 2019
    Posts:
    19
    Hi Guys, I'm very new to coding with C#.
    I was building a game and would like to generate levels existing out of 6 different colors prefab (circles).
    The problem is I can't find a solution that's works for me...
    Anybody know how to do this?

    Thanks, Sam
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    589
    Hi @SamVorst,

    First figure out the logic how you want to build your level, then think about how you would code the logic and create the required scene items etc.

    I have no idea how you would want to place your objects as your description really doesn't offer much details.
    But in general terms the procedural level generation might work like this:

    1. Generate random value.
    2. Instantiate the prefabs to the scene according to this random value.
    3. When some threshold is crossed, repeat from 1.

    You might also need to remove objects from time to time, so that you don't get too many in the scene. Depends on your game etc.

    The setup you need might be very simple, or a bit more complex depending what you plan to do.

    If you for example just want to randomize some platforms in front of the player and you advance in your game to one direction, there's not much trouble there. You could just instantiate platforms at randomized distance within a range and offset them sideways. That would already keep on building randomized level. However you might need to consider moving the level so that it's close to the origin if your game would be such where the player can move long distances. This would be to avoid floating point number inaccuracies. But if your player moves just moderate distances in the scene, you don't need to worry about that.

    Levels that go here and there like some dungeon crawlers are much more complex to setup, but there's many tutorials and articles out there on the web.

    But it would be better if you start to build your level generator system and try figure out every problem at a time, then ask for more help.
     
    MisterSkitz likes this.
  3. SamVorst

    SamVorst

    Joined:
    Nov 28, 2019
    Posts:
    19
    Thanks i'l gonna try out!
     
  4. motivision

    motivision

    Joined:
    Dec 3, 2019
    Posts:
    12
    hi
    that`s not a completed solution, but maybe you make customization in your game and that help you with your question

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.SceneManagement;
    4.  
    5. public class GameLevel5 : MonoBehaviour {
    6.  
    7. //load level manual from another level
    8.  
    9. public void LoadSomeLevel()
    10.     {
    11.         SceneManager.LoadScene ("Scene 20");      
    12.     }
    13.  
    14.  
    15. //your custom random level
    16.  
    17.     public void MyRandomLevel()
    18.     {      
    19.         //your customization for random level
    20.     }
    21.  
    22.  
    23. void Start() {
    24.  
    25.     //LoadSomeLevel();
    26.     MyRandomLevel() ;
    27. }
    28.  
    29.  
    30. }