How do I make an endless road in Unity?

How do I make an endless road in Unity?

Basically I created 2 quads side by side and I added a road texture. I used transform.translate so that the road moves, but I am trying to make the road endless by making the 2 quads reposition themselves in front of the current quads repeatedly so that when I add a player the road is endless. Any way I could do this? 


Answer 1:

You could have 2 sufficiently long road segments. Place them one after another. When player moves from one road segment to another, remove the segment which is (now) behind the player, to the front of the current segment.

Answer 2:

Many infinite runners scroll the road rather than move the player forwards on the road to make the road truly infinite. You can do this with 2 alternating game objects.

public GameObject[] RoadPieces = new GameObject[2];
const float RoadLength = 100f; //length of roads

const float RoadSpeed = 5f; //speed to scroll roads at
void Update ()
    foreach (GameObject road in RoadPieces)
        Vector3 newRoadPos = road.transform.position;
        newRoadPos.z -= RoadSpeed * Time.deltaTime;
        if (newRoadPos.z < -RoadLength / 2)
            newRoadPos.z += RoadLength;
        road.transform.position = newRoadPos;

Just space the roads out correctly in the beginning, and this’ll scroll them forever.

Answer 3:

Well, the answer of Vinayak Garg is good but since you work with perspective this won’t be perfect. This is basically paralax mapping and works great for 2D games. But here you want to have the road dissapear in the horizon probably. If your road is straight then just extent on this technique and add many pieces until it is looking good. Whenever you leave a piece behind move it to the front.

If you want a curvy rode you have to look in procedural algorithms to generate a path for you can use pieces to correct this path. Perlin noise for example can be used for “infinite” randomization. They use it for minecraft in 3D, you just probably just need a 1D variant that just controls the the curve of the road. ANyway, this is a lot harder to implement.