Instantiate leaving a gap [Endless Runner]

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:
alt text

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);
		}
	}
}

I would suggest storing the last object spawned and using it’s length value combined with the object your about to spawns length value to adjust the position of the next object spawned so you get pinpoint accuracy. Something like this:

Instantiate(prefab, LastSpawn.position + new Vector3(0,0,(LastSpawn.Length + AboutToSpawn.Length) /2), transform.rotation);

It’s just a base and you need to access the length variable and what not but hopefully that will put you on the right track and good luck!

I know this tread is one year old, but had the same problem. Fixed with setting Interpolate to Interpolate on rigidbody component. So whoever have same problem try my solution.

I was running into this same issue and found out it had to do with script execution order. My script that determined if it should spawn a road ran before the road movement function. This caused it so have a stale reference to the position by the time Unity actually Instantiates the object.

You can fix this by adding and moving the script that moves your environment to before default time in Project settings > Script Execution Order.