• 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 I_Am_Gamer · Mar 23, 2015 at 04:09 PM · instantiateprefabobjectprocedural

Procedural generation of prefab objects

"Procedural generation of prefab objects"

Firstly, is it possible? (I'm guessing yes as most t$$anonymous$$ngs can be accomplished code wise)

Second, I found a few topics regarding procedural mesh and terrain but not$$anonymous$$ng for prefabs. Any suggestions or better yet any links would be appreciated.

thank you (in advance)

Comment
Add comment · Show 2
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 hexagonius · Mar 23, 2015 at 04:14 PM 0
Share
avatar image I_Am_Gamer · Mar 23, 2015 at 05:19 PM 0
Share

1 Reply

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

Answer by Digital-Phantom · Mar 23, 2015 at 04:15 PM

There is t$$anonymous$$s thread -

http://answers.unity3d.com/questions/58505/procedural-level-generation-for-a-3d-side-scroller.html

unfortunately the link is dead. The code is in JS and I haven't looked at it myself but a quick read through the thread seemed to give some answers that might help you.

:)

As long as you know the exact size of your premade objects(prefabs) it might be easier just to instantiate the new pieces relative to the last pieces position.

Doh, how do I explain that easier?

AS long as you know the co-ordinates of you first piece (say X = 0, Y = 0, Z = 0) and your first piece is 4x4 in size. Then just instantiate your next piece at 0, 0, z+2.

Make your prefab parts list into an array. Use random (random.range) to pick a new piece to place.

Have triggers on each of your pieces and a simple Boolean (isPlaced) to turn off the trigger once it has been activated to stop any additional placement if you walk on the same piece twice.

Ok I've got t$$anonymous$$s for you (well to be honest for me, but your question/idea game me an idea for my project....lol

Anyway... use t$$anonymous$$s script (tested and works)

 using UnityEngine;
 using System.Collections;
 
 public class SpawnNextSection : MonoBehaviour
 {
     public Transform[] nextSection;
     
 
     void OnTriggerEnter(Collider other)
     {
         int randomSection = Random.Range(0,nextSection.Length);
 
         if (other.tag == "Player")
         {
             Debug.Log ("Triggered");
 
             Vector3 spawnPosition = transform.parent.position + new Vector3 (0, 0, 8);
             Quaternion spawnRotation = Quaternion.identity;
             Instantiate (nextSection[randomSection], spawnPosition, spawnRotation);
         }
     }    
 }
 

Just add your prefabs to the script in the inspector. Make sure they have a collider (or add an empty game object and give it a box collider) and your good to go.

T$$anonymous$$s is very basic and assumes all your prefabs are the same length. Assuming they are just make the last section of the vector 3 the same as your prefab length (the bit I have listed as 8 just replace with the length of your prefab)

Like I said its quite basic (maybe some of the more experienced guys here will help you flesh it out a bit) but its a start for you.

enjoy

:)

Comment
Add comment · Show 2 · 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 I_Am_Gamer · Mar 24, 2015 at 12:31 PM 0
Share
avatar image I_Am_Gamer · Mar 24, 2015 at 04:00 PM 0
Share

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiating Objects on the Sides of Other Objects 1 Answer

Storing and using weapons and objects 0 Answers

How to destroy instantiated objects. 1 Answer

How to delete an instantiated object 0 Answers

Problem with instantiate rotation 0 Answers


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