• 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 things can be accomplished code wise)

Second, I found a few topics regarding procedural mesh and terrain but nothing 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

What is it you want to generate? Prefabs are just blueprints of gameobjects with their components, children and predefined values.

avatar image I_Am_Gamer · Mar 23, 2015 at 05:19 PM 0
Share

@hexagonius maybe generation was not the right word then. Procedural instantiation maybe?

Trying to make a tunnel/corridor that will be different on each play-thru. I was going to make just a straight forward scene, mapped out beforehand for each scene but I thought I'd look into making it procedural.

At the moment I have about 9 premade sections that I'm using to build my scenes so basically I was thinking of using these pieces.

1 Reply

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

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

There is this 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 this for you (well to be honest for me, but your question/idea game me an idea for my project....lol

Anyway... use this 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.

This 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

thanks guys. Think this is probably harder than my skill level atm lol. Still I think its something I want to experiment with later though. I like the idea of making a game that will (at least in part) be different each play thru.

avatar image I_Am_Gamer · Mar 24, 2015 at 04:00 PM 0
Share

thanks man

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

How to Reference Player when Instantiating Prefab 1 Answer

inserting random gun into player/enemies hands 3 Answers

Instantiating Objects on the Sides of Other Objects 1 Answer

Unity not Instantiating Prefabs Properly 0 Answers

How to destroy instantiated objects. 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges