infinite platform

I have to simulate an infinite terrain (platform). Doing so though, I’ve seen it go. Do you have other tips?

``````using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;

public class copy : MonoBehaviour
{
public Transform inst;
private Transform tmp;
private float sizeScene;
private int sizeMax = 2;
private List<Transform> queue;
private Vector3 maxSize, lastPosition;
private bool flag;
public float margin;
void Start()
{
flag = false;
queue = new List<Transform>();
maxSize = inst.localScale;
sizeScene = Camera.main.aspect * Camera.main.orthographicSize;
for (int i = 0; i < sizeMax; i++)
{
Vector3 pos = Vector3.zero + inst.right * (maxSize.x * i);
Transform ins = Instantiate(inst, pos, inst.rotation) as Transform;
ins.name = i.ToString();
if (i != 0) lastPosition = pos;
}
flag = true;

}

void Update()
{
if (flag != false)
{

for (int i = 0; i < queue.Count; i++)
{
queue[i].position -= queue[i].right * Time.deltaTime * 20.0f;
if (queue[0].position.x < -sizeScene - (maxSize.x/2))
{

tmp = queue[0];
queue[0] = queue[queue.Count - 1];
queue[queue.Count - 1] = tmp;
queue[queue.Count - 1].position = queue[0].position + queue[queue.Count - 1].right * (maxSize.x + margin);

}
}
}

}

}
``````

The obvious one is create an inexplicably massive plane, and another one would be always positioning a plane/quad underneath the player at some y height.

But if you wanted to fully simulate it in code, you could perform a downward raycast onto a virtual plane using Plane.Raycast. This would return a float of the distance along the ray, effectively being the height from the floor. Ultimately you’d have to rewrite your character controller to make use of this method.

(I’m on my phone right now, but when I get the chance I’ll see if I can make an example!)

The forums wouldn’t let me edit my post, as it cried foul of "spam’, but…
I just realized the Plane.Raycast was completely unnecessary as a simple difference from current height and plane height would suffice-

Move the platforms, not the player. Recycle a platform back to the end of the line once it moves beyond the camera’s minimum view.

Is it good as a solution?

Assuming it’s an endless runner, absolutely. If you continuously move the player forward, eventually you’ll be far enough from the origin (0,0,0) that floating point errors begin to accumulate and your character begins acting strangely.