How to instantiate and align sections of level in real-time

Hi,

I am trying to write a procedural level generation script which contains 5 different shaped sections which are placed into a public Transform array (Sections dragged into slots in Inspector, etc.) Each section has its pivot at one end (the front) and an object with tag ‘helper’ at the other end (this is the End-point of each section). The script looks through each section to find its ‘helper’ object (End-point) and then assigns this to the next Transform (Section) in the array (therefore, to its Start-point).

What I would like to be able to do now is to have say one starting section already in the scene, and then have the others generate after it in random order one after the other.
Any help with this would be greatly appreciated! :slight_smile:

The following function is my starting point. It receives a random number (ChosenIndex) and then goes onto select the section corresponding to that index. It will first create a new Transform (nextTrans) to the chosen section and then assign its position and rotation accordingly. This depends on whether we are adding onto the first (already existing) section in the scene, or ones we have added using the script.

However, running this seems to add the new section at the Start of the existing section (ie. on top) in the scene, not at its end-point. Having looked at the new section when the scene is running, it seems that its y-rotation is at 180 degrees, I’m not sure why this is the case.

Note,

  • ParkLotSectionList - the list of sections in the Parking Lot/available to add in the scene (added via Inspector)

  • EndpointPositionsList - the list of End-points of the sections in the Parking Lot, corresponding with their index (0 - 4) - (added via Inspector)

  • StartingSectionEndpoint - the End-point of the existing section in the scene (added via Inspector)

  • firstSection - simply a boolean to check which starting point to use

Thanks in advance.

//----

void SectionSelector (int ChosenIndex)
{    
    // Assign the chosen section to a new Transform, nextTrans
    Transform nextTrans = ParkLotSectionList[ChosenIndex];

    // Adding into the first section in the scene 
    if (firstSection)
    {
        nextTrans.position = StartingSectionEndpoint.position;
    
        nextTrans.rotation = StartingSectionEndpoint.rotation;
    
        Debug.Log("nextTrans.position = " + nextTrans.position);
    
        firstSection = false;
    }
            
    // Otherwise, assign it the End-point values for the following sections
    else
    {
        nextTrans.position = EndpointPositionsList[ChosenIndex].position;

        nextTrans.rotation = EndpointPositionsList[ChosenIndex].rotation;
    }
}

Asimov – can we cal you Isaac! – :slight_smile: – I do not 100% understand the problem you’re having (could you phrase it again more simply), but …

Often this is the approach to take:

set up let’s say 10 each of your different building units. Just have them sitting “in space” offscreen somewhere. We’ll call that the “parking lot”

Each time you need a new one … just roll in the next one that is sitting in the “parking lot”. All you do is change the position.

When the “parking lot” is empty of a particularly type, just suck back the oldest one form the scene.

(Alternately, if you wish, immediately “suck them back” once the plyer has passed or they are otherwise no longer needed.)

Note that you never have to instantiate (copy) anything … you just literally loop through what you have, taking them out of the “parking lot” (or indeed, simply taking them from the “back” of the scene.

For example, I just literally did this this morning for a “stream of fast moving bullets” (I mean it’s only 2 lines of code, no big deal) - and it’s just what you can also do for “large chunks”

(Funnily enough, we had to do exactly what you describe in “the air” for a scene where you fly around endlessly in 3D with stuff.) Again this is I suppose a somewhat standard approach, and very easy.

I hope this helps in some way!

BTW when it is working beautifully, its great to watch the scene from a high meta-view in the editor as your colleague plays!

Here’s a quick drawing to illustrate, as I understand, above descriptions.
If you chose to use different block size, you should also get the “helper” position before runtime because searching in objects takes time : use a new Script to store that variable from the editor for instance and assign this script to each section game object you made.
You’ll then have a list of Section objects, and iterate through that list (or pick a random next one) to create your path.

List<Section> availableSections;

But beware if you move your camera only on the Z-axis as you said, and if your sections are not located one after another on the same axis (left bottom corner on the picture) : you’ll have a path offscreen easily. Consider having a list of “potential candidates” for each section (block A can be followed by block B…)1305-path.jpg

http://forum.unity3d.com/threads/134372-Ruin-Run-Starter-Kit