- Home /
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)
What is it you want to generate? Prefabs are just blueprints of gameobjects with their components, children and predefined values.
@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.
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
:)
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.
Your answer
Follow this Question
Related Questions
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