Dynamically align Platforms of different lengths

I have been working on an endless runner game. The platforms/chunks of the game are set to spawn as the player moves forward. But right now only one platform length (hardcoded) can be used. If I am to see different length platforms overlapping or gaps occur. how to get past this? thanks!

 using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TileManager : MonoBehaviour
{

    public GameObject[] tilePrefabs;
    private Transform playerTransform;
    private float spawnZ = -300.0f;

    private float tileLength = 150.0f;

    private int tilesOnScreen = 7;
    private int lastPrefabIndex = 0;

    void Start()
    {
        playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
     
        for (int i = 0; i < tilesOnScreen; i++)
        {
            if (i < 3)
            {
                SpawnTile(0);
            }else{

            SpawnTile();

            }
        }

    }

    void Update()
    {
        if(playerTransform.position.z > (spawnZ - tilesOnScreen * tileLength))
        {
            SpawnTile();
        }

    }
     void SpawnTile(int prefabIndex = -1)
     {
        GameObject go;
        if(prefabIndex == -1)
        {
            go = Instantiate (tilePrefabs[RandomPrefabIndex()]) as GameObject;
        }
        else
        {
            go = Instantiate (tilePrefabs[prefabIndex]) as GameObject;
          
        }
        go.transform.SetParent(transform);
        go.transform.position = Vector3.forward * spawnZ;
        spawnZ += tileLength;
     }

     private int RandomPrefabIndex()
     {
         if (tilePrefabs.Length <= 1)
            return 0;
        int randomIndex = lastPrefabIndex;
        while(randomIndex == lastPrefabIndex)
        {
            randomIndex = Random.Range(0,tilePrefabs.Length);
        }
        lastPrefabIndex = randomIndex;
        return randomIndex;
     }

}

Some ways:

  • make a script that contains a width and put it on the base of each chunk, read the width, adjust accordingly

  • make invisible markers in the level that define the length and spacing

  • watch Youtube tutorial videos about how other endless runners do it.

1 Like

I changed the Spawning system a little bit. I assigned a variable called lastPlatformLength and set it to β€˜0’.
Every time I spawn a platform/tile I would change it to the length of the platform/tile I just spawned. Since this is done after spawning it does not affect the first spawned platform/tile because the default is β€˜0’. THIS ONE WORKS.
NOW I NEED TO FIND A WAY TO AVOID SPAWNING THE SAME TILE MORE THAN ONCE IN A RAW.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlatformGenerator : MonoBehaviour
{

    public Transform generationPoint;
    private float  platformLength;
    private float [] platformLengths;

    private Vector3 offset = new Vector3(0f, 0f, 200.0f);

    private float distanceBetween;
    public float distanceBetweenMin;
    public float distanceBetweenMax;
    public GameObject[] platforms;
    private int platformSelector;

    private float lastPlatformLength = 0.0f;


    void Start()
    {
        platformLengths = new float [platforms.Length];
        for (int i = 0; i < platforms.Length; i++)
        {
            platformLengths[i] = platforms[i].GetComponent<BoxCollider>().size.z;

        }
    }

    void Update()
    {
       



        if (transform.position.z < generationPoint.position.z)
        {
            distanceBetween = Random.Range(distanceBetweenMin, distanceBetweenMax);

            platformSelector = Random.Range(0,platforms.Length);

            transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z + (platformLengths[platformSelector]/2)+ (lastPlatformLength/2) + distanceBetween);


            GameObject newPlatform =Instantiate (platforms [platformSelector], transform.position - offset, transform.rotation);


//ASSIGN LAST PLATFORM LENGTH
            lastPlatformLength = (platformLengths[platformSelector]);

         

        }

    }

   
   
}

I did go through some Tutorials and managed to Frankenstein some code that works.

WHY ARE WE SHOUTING?!

You can use a shuffle routine (google)

or,

You can keep track of the previous one and make sure you don’t choose it again

or,

Probably many other ways.