# Calculating where to position instantiated clone

I’m making a game that involves a character moving on a thin piece of ground in one direction until they die. I simulate a piece of “infinite” ground by cloning a piece of ground on one side of the screen, moving it along the Z axis, then destroying it. I have most everything done away with, however I have one rather large issue: speeding up. See, I can calculate where to position the piece of ground along the Z axis when the speed is finite and set in stone, but when the game speeds up, gaps between the pieces of land become visible and grow larger over time. How can I avoid this? This is the script that I have currently:

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

public class MovingGround : MonoBehaviour
{
public GameObject player;
public GameObject obsticle;
public Quaternion q = new Quaternion();

void Start ()
{
q.w = 0;
q.x = 270;
q.y = 0;
q.z = 0;

if (name == "GroundCloned")
{
GameObject clone = Instantiate(gameObject);
clone.transform.position = new Vector3(1.890699f, -1.105775f, 6.46f);
clone.name = "Ground";

clone = Instantiate(gameObject);
clone.transform.position = new Vector3(1.890699f, -1.105775f, -9.82f);
clone.name = "Ground";

clone = Instantiate(gameObject);
clone.transform.position = new Vector3(1.890699f, -1.105775f, -26.2f);
clone.name = "Ground";
}
}

void Update ()
{
if (player.GetComponent<Player>().gamePlaying == true && name == "Ground")
{
gameObject.transform.position = new Vector3(gameObject.transform.position.x,gameObject.transform.position.y,gameObject.transform.position.z + 0.2f + (obsticle.GetComponent<Obsticle>().time / 200f));
if(this.transform.position.z> 22.9f)
{
GameObject clone = Instantiate(gameObject);
clone.transform.position = new Vector3(1.890699f, -1.105775f, -26.03f + (obsticle.GetComponent<Obsticle>().time / 200f));
clone.name = "Ground";
Destroy(this.gameObject);
}
}
}
}
``````

As written in my code, I’m changing the Z position of the clone based on the amount of time has elapsed, and changing the instantiated clone’s position with time accordingly. However, as stated previously, that doesn’t work. Is there a possible algorithm I could use to determine how much I should change the distance apart the clones should be?

Thanks a million

Good day.

If you move the clones based on time, you yill have this problems. Time in not constant, (well, time is… but not the calculation of it :D)

If you want “perfect fit” you should use only coordinates. Detect where the player is, or know where finishes the last ground clone…

But if using time, as you said, you will have “empty spaces” because Maybe the player goes faster, but the time to execute the Update() function is not constant…

Bye!