Infinite runner world creation

Hey people,can someone help me on scripting this.I’m doing this like subway surfer,keeping player stationary and moving the world around him.My question is how can i instantiate the track prefab continuously right after previous one…?

For instance I have track named Track_A as prefab and as soon as this track is instantiated in the world, a script attached to it will make it move.I instantiate 5 prefabs at start-up one after another tailing perfectly.

When the first track is destroyed after reaching a certain point,it will get destroyed and a new track will be instantiated at the end of the chain but will cause FLOATING POINT PRECISION ERRORS leaving small gap between track prefabs.HOW CAN I SOLVE THIS…?

I am attaching the script with this

TrackScript

//The length of the track (will be predefined and same for all track)
public float tracklength=10;
//The speed at which the tracks are moving
private float worldspeed=10.0f;

void Update()
{
    //Move the new track towards the camera
    transform.translate(-Vector3.forward*Time.deltatime*worldspeed);
}

//Function to get the end of this track to create the next track
public Vector3 GetTrackEndPos()
{
    Vector3 Endposition=transform.position;
    Endposition.z+=tracklength;
    return Endposition;
}

WorldScript

//This function will be called when a track at player end is destroyed to create a new track ahead of the last track
public void CreateOneTrackAhead()
{
    if(LastTrackPrefab)
    {Pos_TrackInstantiate=LastTrackPrefab.GetComponent<TrackScript>().GetTrackEndPos();}

    //Instantiate the track at the end of previous track
    LastTrackPrefab=Instantiate(TrackPrefab,Pos_TrackInstantiate,Quaternion.identity);		
}

Use two cubes for each track; one as entry trigger and one as exit trigger "AND TURN OFF MESH RENDERING " to make the cube invisible on scene. Loop the sequence as shown in picture.

The reason exit trigger of Track A is set to the middle of Track B is to avoid the viewing problem. As it may leave a gap on destroyed area which might be annoying to viewer

Credits : MS Paint :smiley: :smiley:

void OnTriggerEnter(collider other)

{

//logic to instantiate the next track

}

void OnTriggerExit(collider other)
{

//logic to destroy the previous track
}

I am surprised you would see floating point precision errors in such a short distance.

If that’s really going on, how about using a double?

Your real problem is probably caused by moving it by a factor of Time.deltaTime every frame, which is a lot more error-prone than moving it a fraction of a set distance relative to the elapsed time since the last reset.