"Infinite" terrain algorithm for 3D endless runner?

Hi guys, i’ve been following some tutorials online and am trying to expand on a basic 3D endless runner and remove the bounds of the left and right wall; so the character can move freely on the X axis.

My code is kind of working, the issue is that the floor tile that is instantiated is given a gameObject name of currentTile, and when the character moves left or right, the called left or right tile is spawned, but when the player enters into the next forward tile spawn trigger, it still generates on the original forward path.

Sorry its kind of hard to explain, but the code works fine for doing a basic endless runner, with tiles spawning only in front of the character. I think what i need to do is have some kind of a static gameObject as the next spawned floor tile, but I can’t figure out what to do next.

Here’s the current code for TileManaging:

public class TileManager : MonoBehaviour
{
    public GameObject forwardTilePrefab;
    public GameObject leftTilePrefab;
    public GameObject rightTilePrefab;

    public GameObject currentTile;

    public Collider forwardCollisionZone;
    public Collider leftCollisionZone;
    public Collider rightCollisionZone;

    private static TileManager instance;

    public static TileManager Instance
    {
        get
        {
            if (instance == null)
            {
                instance = FindObjectOfType<TileManager>();
            }
            { return instance; }
        }
    }

     public void SpawnForward()
    {
        currentTile = (GameObject)Instantiate(forwardTilePrefab, currentTile.transform.GetChild(0).GetChild(0).transform.GetChild(0).position, Quaternion.identity);
        currentTile.gameObject.tag = "FWtile";
    }

    public void SpawnLeft()
    {
        Instantiate(forwardTilePrefab, currentTile.transform.GetChild(0).GetChild(0).GetChild(1).transform.position, Quaternion.identity);
        currentTile.gameObject.tag = "LFtile";
    }

    public void SpawnRight()
    {
        if (LeftTileSpawner.leftJustUsed == false)
        {
            Instantiate(forwardTilePrefab, currentTile.transform.GetChild(0).GetChild(0).GetChild(2).transform.position, Quaternion.identity);
            currentTile.gameObject.tag = "RTtile";
        }
        else return;
    }
}

And for the colliders/triggers:

public class RightTileSpawner : MonoBehaviour
{
    public Collider RightColBox;
    public bool wasUsed;

    public void OnTriggerEnter(Collider name)
    {
        if (name.tag == "Player")
        {
            print("Right Collider hit");
            wasUsed = true;
            TileManager.Instance.SpawnRight();
       
        }
    }
}

The tag doesn’t get updated in the game either, but that’s not really a concern right now.

Thank you so much for reading all that, if you can come up with anything please let me know!

Usually in endless runner game, character stands at its position (0,0,0) only world moves except of jumps.

You can mimic infinite X axis movement by

  1. Making terrain blocks reasonably big in x-Axis such that a player won’t able to pass through in x-axis until terrain passed back.
  2. When player moves in X-Axis, shift all terrain blocks in x-axis.
  3. keep creating new terrains at 0,0,Z. ()

There could be dozens of ways but if I would be you then would do like it

Thank you fahi, I hadn’t thought of making the player static! I would still like to see if i could fix the terrain spawner though, but if cannot that seems like a good way to go about it!