Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SrBilyon · Apr 15, 2011 at 08:53 AM · randomspawnlevelgenerationalignment

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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); }

Comment
Add comment · Show 12 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SrBilyon · Apr 15, 2011 at 01:34 PM 0
Share

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?

avatar image Kourosh · Apr 15, 2011 at 02:04 PM 1
Share

i'm creating a pseudo. will upload in a $$anonymous$$ute.

avatar image Kourosh · Apr 15, 2011 at 03:08 PM 1
Share

It was more than a $$anonymous$$ute. but it's there.

avatar image SrBilyon · Apr 15, 2011 at 03:25 PM 0
Share

You are awesome. Testing it out.

avatar image Kourosh · Apr 15, 2011 at 03:56 PM 1
Share

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.

Show more comments
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SrBilyon · Apr 15, 2011 at 01:21 PM 0
Share

I like the dictionary part. That was something I was thinking I would have to decide the next block.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges