Gap between road in endless runner type game

Hi,

I’m trying to make an endless runner and did so by having sort of a treadmill, where you have roadtiles moving on next to the other and when one hits the limit (a cube with collider) it gets teleported to the start and moves forward. Yet my issue is that by doing so, small gaps appear, probably due to the calculation and time beeing amplified. So has anyone a tip on how to fix that ?

{
    public GameObject roadRespawnPlace;

    private void OnTriggerEnter(Collider other)
    {

        if (other.gameObject.tag == "RoadTile")
        {
            other.transform.position = roadRespawnPlace.transform.position;
        }
        else
        {
            Destroy(other.gameObject);
        }
    }

}

Hey there,

in general that behaviour makes sense as you cannot guarantee that the trigger will always exactly trigger when the road element enters the finish line. This way ofc there can be a bit of a gap.

The probably easiest solution would be to keep a reference to the last element that was spawned. Then when you spawn a new element you can set this new elements position in relation to the last element in line. This way it is always dynamically in exact the position that you want it to have.

So basically instead of

  other.transform.position = roadRespawnPlace.transform.position;

write something in the direction of:

  other.transform.position = lastSpawnedTransform.position + Vector3(0, 0, lengthOfRoadElements);
  lastSpawnedTransform = other.transform;

And you should be good to go.

Ofc you have to adjust your initial setup so that the value of lastSpawnedTransform is properly set.