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

Structuring Procedural Level Generation

Discussion in 'Scripting' started by LiberLogic969, Nov 12, 2014.

  1. LiberLogic969

    LiberLogic969

    Joined:
    Jun 29, 2014
    Posts:
    138
    I'm having trouble figuring out a good way to structure a level that is procedurally generated. I'm going for a simple Binding Of Isaac style 2D Top-Down world which contains multiple Rooms that are connected together. You move from one room to the next so only one is visible at all times. How should I handle structuring a level like this in Unity? Should each room be a separate Scene or should I create a GameObject for each room and place them in a single scene and activate/deactivate as needed? Is it possible to create a new Scene from a script? I'm still relatively new to Unity and I've never done any procedural generation before. I've found a lot of information online about the subject but its all been about various algorithms for generating mazes and dungeons. I'm more interested in the design and setup that best fits Unity. I'm sure there's multiple answers to these questions, and it may just come down to preference, I'm just not sure how to move forward from here.

    Any ideas would be really helpful!
     
  2. Epictickle

    Epictickle

    Joined:
    Aug 12, 2012
    Posts:
    431
    You will firstly have to dynamically generate a Mesh..

    I think you may be in over your head if you are new to Unity, man. Procedural generation, in itself, can be tricky.. But procedural generation with design parameters is another thing entirely..

    Here's a video tutorial I followed awhile back.. Great stuff!

    https://www.youtube.com/playlist?list=PLbghT7MmckI4qGA0Wm_TZS8LVrqS47I9R

    Btw, the genre of your game would be "Dungeon Crawler".
     
    LiberLogic969 and TonyLi like this.
  3. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    That looks like a good tutorial series. You don't necessarily have to generate meshes, however.

    You could create room templates that are prefabs. Add a short script to the prefab's main GameObject that lets you specify where the connection points are. If you want to keep it simple, you can restrict connections to the middle of each edge (top, bottom, left, right) and have a Boolean variable for each. Say you start with a prefab room where left and right are true. For the left connection, you want to find another prefab whose right connection is true, and then instantiate it next to the original room.

    You can't easily nest prefabs in Unity, but you can add a room-content-generation script to the room prefab. This script could contain references to room content prefabs (treasure chests, monsters, etc.). When you instantiate the room prefab, this script can run to randomly instantiate some of those room content prefabs.

    I'd recommend one scene per level. Instantiate all the rooms in the level into that scene.

    To show only one room at a time, you could encompass each room in a trigger collider that fires when the player enters the trigger. It could center the camera over the new room, show its contents, and hide the contents of the other rooms.
     
    LiberLogic969 likes this.
  4. Epictickle

    Epictickle

    Joined:
    Aug 12, 2012
    Posts:
    431
    Ahh, I don't know why I didn't think about that lol.

    I've been working so much with procedural generation of meshes I guess the idea of using prefabs just completely slipped my mind!

    Using TonyLi's suggestion, I doubt it would be over your head. It would be very simple to do it the way he suggests... Man, that makes me want to try out your pseudocode Tony. xP
     
  5. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    Seems like it would be a good game jam project!
     
  6. LiberLogic969

    LiberLogic969

    Joined:
    Jun 29, 2014
    Posts:
    138
    Thanks for the link! Interesting series, although it appears to be WAY overkill for the project I have in mind! I'm trying to start as simple as possible with procedural generation and The Binding Of Isaac follows a very basic Zelda-like design while still being deep (and very fun). I will definitely follow that series more in depth after I get a handle on this beginner stuff.

    Wow, you have no clue how much this has helped. I'm going to throw together a quick little test project and give this a shot. Thank you!
     
  7. Epictickle

    Epictickle

    Joined:
    Aug 12, 2012
    Posts:
    431
    I'd be willing to host it on my website if you'd like to help out. It just seems like too fun an idea to let go to waste. xP
     
  8. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    Let me know when an online game jam is coming up, and we'll do it! :)
     
  9. Mateusak

    Mateusak

    Joined:
    Dec 10, 2014
    Posts:
    6
    Have a way to get a random prefab from a folder? It will help a lot.
     
  10. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    At design time (i.e., in the editor), just find all files in the folder and choose one randomly.

    At runtime (i.e., in a build), have a script with an array of prefab references. Populate the array design time. At runtime, randomly choose one (Random.Range(0, myArray.Length)).