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:
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.
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?
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.
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.
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:
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.
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:
function OnTriggerEnter (collision : Collider)
{
if (collision.CompareTag ("Player"))
{
renderer.material.color = Color.red;
}
}
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.
public DungeonGenerator DungeonGenerator;
public MetaStateManager MetaStateManager;
function OnTriggerEnter (collision : Collider)
{
if (collision.CompareTag ("Player"))
{
renderer.material.color = Color.red;
DungeonGenerator.GoToNextLevel();
MetaStateManager.SetNewMetaState(MetaState.Dungeon);
}
}
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.
using UnityEngine;
using System.Collections;
public class clearedLevel : MonoBehaviour
{
public DungeonGenerator DungeonGenerator;
public MetaStateManager MetaStateManager;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
DungeonGenerator.GoToNextLevel();
MetaStateManager.SetNewMetaState(MetaState.Dungeon);
}
}
}
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.
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:
This is such an awesome script!!! I cant get enough of it.
@Rhianu
I’d recommend using a messenger(events) system for such calls, if you’re doing more than a few. Makes it MUCH EASIER to manage on a larger scale
this is a basic one that is pretty good IMO: C Sharp Messenger Extended