Hello. I’m trying to create endless runner by moving gameobjects under my player instead of actually moving the player.
Everything works fine except every 4th instantiate creates a gap which size varies depending of the speed I’m moving the objects. I have been trying to fix this for a week now and I’m getting kinda depressed about it. Now, I understand I’m destroying the GO after a specific cordinates, -3,5 on Z axis to be specific, and I understand that those numbers won’t round perfectly. So sometimes its destroyed on -3,5xxxx and I’m sure thats what could be cousing the problem. But I have no idea how to fix it or some other workaround.
Image:

using UnityEngine;
using System.Collections;
public class CubeBehaviour : MonoBehaviour {
public Vector3 speed = new Vector3(0, 0, -20);
public GameObject prefab;
private Vector3 pos; // position of rigidbody
private float length; // length of object
// Use this for initialization
void Start () {
// copy our object's default rigidbody position
pos = rigidbody.position;
// fetch the object's length. we'll use this to determine
// if we've past our camera...
length = 3.5f;
}
// Update is called once per frame
void FixedUpdate () {
// move our z towards the camera
// to do this, we subject our position by the speed
pos.z = rigidbody.position.z;
// let's update our rigidbody's position
// in this exercise, we're just moving the z. however, we
// have cached the X and Y in our start function.
rigidbody.MovePosition(rigidbody.position + speed * Time.deltaTime);
// if our object's length is past our camera (0,0,0), then...
if (pos.z < -length)
{
// delete our object
Instantiate(prefab, rigidbody.position + new Vector3(0,0,28f), transform.rotation);
Destroy(gameObject);
}
}
}