- Home /
Procedural level generation for a 3D side-scroller
So I was planning on working on a 3D side-scroller game with a character that automatically starts moving at the beginning of the stage. However, I decided to extend the game's playability through procedural level generation which I want to do in 3D graphics (assuming that it isn't to much different from 2D in theory since i'm only using 2 axes).
So, I figure that the best way to go at it by making modular level components (level blocks) that are like, 30 x 30 and spawning them into a series to create a randomly generated level. My problem is I'm not sure how to spawn them in a somewhat controllable fashion to where, say, a slope piece should be spawned after every [x] blocks, and that the next piece should be [y -=30] (assuming 2 axes are being used) lower than that previous piece.
I also would like to randomly spawn objects and obstructions aligned on top of those pieces. How should I go forth creating a script that can pull of the spawning and alignment of my level blocks? And since I plan to have this on a mobile platform, would it be efficient enough to create the level from the beginning or should a create a set amount of blocks at a time?
Lastly, I want to have a block counter that assigns a number to each instanced block. This way, I can track the amount of blocks spawned, identify each spawned block, and set a condition after a certain amount of blocks have been spawned.
Answer by Kourosh · Apr 15, 2011 at 09:18 AM
Technically speaking, the solution i can think of for alignment, is that in each modular component (prefabs) make an empty gameobject as a child and name it "anchor" then in the script where you instantiate new blocks, store the last instantiated gameobject into a variable and before the next block being instantiated look for the last block's "anchor" and make it the position for the new block. This will gives you ability to put the anchor wherever you want and make sure that everything matches seamlessly. And yes, as you also mentioned, I think it's efficient to consider a sight range for your character to limit the amount of process needed.
I've created a sample scene for you to download. I didn't know how to explain here:
http://dl.dropbox.com/u/4659539/ProceduralLevel.unitypackage
When you play use (A-D or Left-right arrow)
Here is the code for everyone:
var blockPrefabs:GameObject[] = new GameObject[2]; //Lets say you have only 2 different blocks. var lastBlock:GameObject; // Store the last/current block. Remember that the first block shoul be in scene and assigned to this variable.
private var nextStop:Vector3;
function Start(){ nextStop = transform.position + Vector3.forward *0.1; }
function Update () { RollCamera(); var currPos:Vector3 =transform.position; print(Vector3.Distance(nextStop,currPos)); if(Vector3.Distance(nextStop,currPos)>5){ CreateNewBlock(); nextStop = transform.position + Vector3.forward *0.1; } }
function CreateNewBlock(){ var insPos:Vector3 = lastBlock.transform.Find("Front_Anchore").transform.position; var newBlock:GameObject = Instantiate(blockPrefabs[Mathf.Floor(Random.Range(0,2))],insPos, lastBlock.transform.rotation); lastBlock = newBlock; }
function RollCamera(){ transform.Translate(Input.GetAxis("Horizontal") Vector3.right 0.2); }
Nice idea. So how exactly do I align the anchor to when it is positioned a whole block length next to the currently spawned block? And what is the best way to assign a block a number so that the system can spawn a 'special' block after a certain number of spawned blocks? Could you supply a pseudo code example?
i'm creating a pseudo. will upload in a $$anonymous$$ute.
You can add more anchors inside your modular components depending on what type of block they. for instance, anchors that you add for downward sloped block is different from anchors available in a cube block. So you may put conditions that if my lastBlock is a sloped block then tell the script to find for the proper anchor which matches the next block. To make it simple, you should put slots as many as block you have and name them in a way that you can condition the connectivity between the last and the next block. I wish I could explain better. Hope you get what i mean.
Answer by efge · Apr 15, 2011 at 11:55 AM
For the random spawn points you could calculate them in 2D and use a raycast to find the height or place transform nodes inside the block tiles and select a random number at runtime.
The "anchor" idea by Kourosh is great.
You could also define a "dictionary" where all possible neighbors and transforms for a given block are listed.
I like the dictionary part. That was something I was thinking I would have to decide the next block.
Your answer
Follow this Question
Related Questions
How can I align instantiated prefabs randomly on runtime #C 0 Answers
how to create random levels. like a golf game 3 Answers
Randomly Generated Levels 3 Answers
Strange random seed issue with level generation 1 Answer
Random Position Script 1 Answer