- Home /
Question by
daveinpublic · Aug 01, 2014 at 10:12 PM ·
prefabrandomendless
How to instantiate random prefabs of varying size and rotation in a continuous track?
Hi, I've searched various questions but none seem to answer this specific version of this question. I have many pieces of a track of varying size and rotation. Some stair step down, up, left, right, etc.. How do I spawn like 8 track pieces to start, and then more as I move along the path?
I tried this:
public class GeneratePointScript : MonoBehaviour {
public static Vector3 GenerationPoint = new Vector3(0,0,0);
public GameObject[] TrackPieces;
void Start () {
for (int i = 0; i < 8; i++) { addToTrack(); }
}
public void addToTrack () {
Instantiate(TrackPieces[Random.Range(0,2)], GenerationPoint, Quaternion.Euler(0,0,0));
}
}
Plus this on the generated prefab:
public class Track_Long_Script : MonoBehaviour {
void Start () {
GeneratePointScript.GenerationPoint += new Vector3(1,2,1);
}
So that, each time the prefab Track Long is instantiated, the script moves the generation point for the next prefab. Each prefab is a different length and rotation, height, etc.. The way I'm doing it here, all of the track pieces are being generated in the same place, and not continuing to move forward. Maybe the generation point is moving after the function has already generated the 8 pieces?
Thanks!!!
Comment