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)

There is this thread -

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.

:slight_smile:

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

:slight_smile: