Search Unity

MiniDungeon - A Free Randomly Generated Dungeon Jump Start

Discussion in 'Made With Unity' started by spg, Jan 2, 2011.

  1. spg

    spg

    Joined:
    Aug 6, 2009
    Posts:
    79
    Hey guys, I spent some time recently messing around with Unity and made a simple randomly generated 2d dungeon maze game skeleton. I thought it might be useful to some people as a jump start to make a similar game, or just something to check out and learn from? Hopefully someone finds it useful! :)

    You can download the full Unity project, and read more about it on my blog, here's the link:

    http://www.stevegargolinski.com/minidungeon-a-free-random-dungeon-jump-start-for-unity/

    -- Steve
    twitter.com/sgargolinski
    stevegargolinski.com
     
    Last edited: Jan 23, 2011
  2. Tastygraph

    Tastygraph

    Joined:
    Nov 12, 2009
    Posts:
    25
    Thanks alot for taking the time to share this.
    I recently tried a similar project, but with a different approach. Sadly I didnt quite have the programming knowledge to finish the project and I ended up with a randomly generated dungeon made of... rocks and lava with no real clear path from Start to Exit... Eventually I decided to put it on hold and focus on different projects with a greater chance of success ;)
    But the idea of using Randomly Generated Maps is something that really appeals to me.
    I love it when games can be replayed with different results everytime. :)
     
  3. AndrewGrayGames

    AndrewGrayGames

    Joined:
    Nov 19, 2009
    Posts:
    3,821
    This...is really awesome. Thanks to this, a little retrofitting and I could make some randomized maps for my FPS game!

    Thank you for this great setup, and my nomination for Post of the Month!
     
  4. spg

    spg

    Joined:
    Aug 6, 2009
    Posts:
    79
    Cool, glad you guys got something out of it!
     
  5. 3Duaun

    3Duaun

    Joined:
    Dec 29, 2009
    Posts:
    600
    very cool, thanks for sharing the script and the detailed blog post. have you considered doing any parametrizing on the script too?

    +1!!!!
     
    Last edited: Jan 21, 2011
  6. 3Duaun

    3Duaun

    Joined:
    Dec 29, 2009
    Posts:
    600
    Do you have any versions of this cool script that always place the hero at the "beginning" of a dungeon(a location the script deems n origin, usually a corner of the map), and the red(stairs to exit) at the "end" of the dungeon?

    Or perhaps some ideas on where in the code I could try to implement such a thing?

    Thanks for any help :)
     
  7. spg

    spg

    Joined:
    Aug 6, 2009
    Posts:
    79
    There are a bunch of different parameters already included (such as dungeon size)... are there any specific ones you'd be interested in?

    As for the "beginning"/"end" stuff, there's really no beginning or end of the maze, it's not something linear that goes from start to finish - there are lots of cycles and lots of different potential beginnings and end. You could try replacing the stairsUp and stairsDown placement code with an alternate function that tries to place them at different corners of the map.
     
  8. rhianu

    rhianu

    Joined:
    Feb 25, 2010
    Posts:
    93
    Hi, I would just like say thank you for putting together this awesome random dungeon generator. I've already incorporated it into a game of mine and have begun making a few minor modifications to it.

    However, because of the way the character movement works in my game, I need to modify the code so that the player goes to the next level simply by touching the red block instead of being inside of it like you originally programmed. I think the best way to accomplish this would be with an OnTriggerEnter command, but I'm having a little trouble figuring how how to incorporate that into your code. Everything I've tried returns a bunch of errors.

    Thanks in advance for any help.
     
  9. spg

    spg

    Joined:
    Aug 6, 2009
    Posts:
    79
    All you need to do is add this code into DungeonMetaState::HandleHeroMovementInput():

    if (moveSuccess DungeonGenerator.IsHeroOnStairsDown())
    {
    MetaStateManager.SetNewMetaState(MetaState.Story);
    return;
    }

    put it right under this:

    if (Input.GetKeyUp(KeyCode.D))
    {
    DungeonGenerator.MoveHero(Direction.East);
    moveSuccess = true;
    }
     
  10. spg

    spg

    Joined:
    Aug 6, 2009
    Posts:
    79
  11. 3Duaun

    3Duaun

    Joined:
    Dec 29, 2009
    Posts:
    600
    !! great round of updates to this already great script!! Thank you Steve! :)
     
  12. rhianu

    rhianu

    Joined:
    Feb 25, 2010
    Posts:
    93
    I appreciate your help, and I don't want to sound ungrateful because you've obviously put a lot of long, hard hours into this project, and you're clearly far more skilled as a programmer than I am, but I'm afraid your proposed solution doesn't work in my project. I tried making that modification in the unaltered version of your project that I downloaded, and it worked just fine. However, in my project the change does nothing, due to some modifications I've made to the way certain things work.

    Perhaps I wasn't clear on this earlier, and for that I apologize, but in my own project I'm using my own custom script to control character movement, and have completely removed the character movement script that you originally programmed. I did this because, in my game, I want my character to walk on the black parts of the maze and treat the gray parts like walls (which is the inverse of how you have it set up). Naturally this would require modifying the building block prefabs so that there is always a clear path when the black part it used as the floor, but I can fix that easily myself by simply building my own prefabs. But first I'd like to get the stairs working properly.

    Anyway, I looked through the code in your DungeonGenerator.cs file to try and figure out how it worked, and looking at the public bool IsHeroOnStairsDown(), I noticed it said return heroLocation.x == stairsDownLocation.x heroLocation.z == stairsDownLocation.z; which is probably what's creating the problem for me, because in my project it simply isn't possible for the hero and the stairsDown object to both have the exact same XZ coordinates as each other. So I was thinking maybe instead of looking having the script check to see if the two objects are both in the same position, it could just use an OnTriggerEnter function to initiate the next phase of the script. But I'm still kinda new to this whole coding thing, so I'm not sure how to make that work.

    Perhaps if you played my game, you could get a clearer idea of what I'm talking about. You can try it here:

    http://finalfreak.greatnow.com/hellsent/game/WebPlayer.html

    It's nowhere near finished, obviously, but it should give you an idea of what I'm trying to do.

    NOTE: my game doesn't start in the maze, but rather in a sort of "beginner area" with the maze being the very next scene after that. When the maze first starts you're stuck inside the green starter block, and you have to jump with the spacebar to get out. Right now the walls are short enough that you can just jump over them, which is something I'll fix later, but I want to get the stairs working first.

    Sorry if that makes things a little complicated.
     
    Last edited: Jan 31, 2011
  13. spg

    spg

    Joined:
    Aug 6, 2009
    Posts:
    79
    Hey man, that makes sense. What part if the OnTriggerEnter approach are you having trouble with? Do you have a rigidbody on the trigger object?

    http://forum.unity3d.com/threads/46861-OnTriggerEnter-not-work
     
  14. rhianu

    rhianu

    Joined:
    Feb 25, 2010
    Posts:
    93
    The object itself responds to the OnTriggerEnter event just fine. I made sure I had a working OnTriggerEnter script first by simply creating a cube and attaching the following code:

    Code (csharp):
    1. function OnTriggerEnter (collision : Collider)
    2. {
    3.    if (collision.CompareTag ("Player"))
    4.    {
    5.      renderer.material.color = Color.red;
    6.    }
    7. }
    As you can see, the above code is very simple, and it works just fine. But then I tried adding in the lines from your script to generate the next level of the dungeon, and Unity gave me errors claiming Unknown identifiers. It's probably a really simple omission on my part, but I don't know what I need to include to make it work.

    Code (csharp):
    1. public DungeonGenerator DungeonGenerator;
    2. public MetaStateManager MetaStateManager;
    3.  
    4.  
    5. function OnTriggerEnter (collision : Collider)
    6. {
    7.    if (collision.CompareTag ("Player"))
    8.    {
    9.      renderer.material.color = Color.red;
    10.      DungeonGenerator.GoToNextLevel();
    11.      MetaStateManager.SetNewMetaState(MetaState.Dungeon);
    12.    }
    13. }
    I'm doing my coding in JavaScript, by the way.
     
    Last edited: Jan 31, 2011
  15. spg

    spg

    Joined:
    Aug 6, 2009
    Posts:
    79
  16. rhianu

    rhianu

    Joined:
    Feb 25, 2010
    Posts:
    93
    Ah, yes, that was it. Thank you. I didn't know that the different languages compiled in different orders. ^_^;;

    So I just rewrote the script in C# and it worked just fine.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. public class clearedLevel : MonoBehaviour
    6. {
    7.    public DungeonGenerator DungeonGenerator;
    8.    public MetaStateManager MetaStateManager;
    9.  
    10.    void OnTriggerEnter(Collider other)
    11.    {
    12.      if (other.gameObject.tag == "Player")
    13.      {
    14.        DungeonGenerator.GoToNextLevel();
    15.        MetaStateManager.SetNewMetaState(MetaState.Dungeon);
    16.      }
    17.    }
    18. }
    However, now that I've got it working, there's the question of how to call the function when the player touches the actual stairsDown game object. I got the code itself to work by simply attaching it to a cube I had placed in the scene manually, but I can't figure out how to call it when the player collides with the stairsDown game object, due to the fact that the stairsDown object functions differently as a prefab. :confused:

    Perhaps instead of attaching the script to the stairsDown game object directly, I should place the code for it somewhere inside of one of your original scripts, rather that giving its own separate script? If so, where would I place the code in order for it to work?

    EDIT: I managed to get the help I needed to solve the problem from the following topics:

    http://forum.unity3d.com/threads/77...-from-an-object-not-involved-in-the-collision
    http://forum.unity3d.com/threads/77...-in-one-script-from-another-script-(using-C-)
    http://answers.unity3d.com/question...ble-in-one-script-from-another-script-using-c

    Thanks again for making such an awesome jump start resource. :D
     
    Last edited: Feb 16, 2011
  17. dontnormally

    dontnormally

    Joined:
    Apr 15, 2009
    Posts:
    8
    Perhaps you could post an updated skeleton project? :)
     
  18. 3Duaun

    3Duaun

    Joined:
    Dec 29, 2009
    Posts:
    600
  19. DrowKiroth

    DrowKiroth

    Joined:
    Oct 31, 2010
    Posts:
    5
    This is great
     
  20. vackup

    vackup

    Joined:
    Jun 26, 2013
    Posts:
    2