Moving Gameobject and Reseting Issues

Been having trouble where I have the tiles moving in one direction and after it hits a certain point I would like it to reset the position back to 0 and have the movement keep going. Also with this I would like to change the movement speed of the object when needed. I keep coming across the issue of gameobjects creates a huge gap after they gets reseted to 0

Here’s an example.

So if I keep the speed at around 0.1 or 1 it should be fine but if I would like to increase it it always gives me the unwanted spacing. Note that there are 100 gameobjects running the same script.

My Sample Script is
#pragma strict

function Start () 
{

}

function Update () 
{
	Move();
}
function Move()
{
	var speed : float = 0.03;
	transform.position.z = transform.position.z + speed;
	
	if(transform.position.z >= 20)
	{
		transform.position.z = 0;
	}
	
	
}

So I was wondering, what are your suggestion of solving this issue?

maybe u must sync it with your fps . check this :

transform.position.z = transform.position.z + speed*Time.deltaTime;

Whatever speed you are selecting, the chance the last tile will exactly reach 20 is remote. Most of the time it will lap over the 20.0 limit. That means that all rows of tiles behind the front row will be a bit ahead as well, so when you move the tile back to the beginning, there will be a gap between the new rear tiles and the row just in front of them. One solution is to account for the overlap, but placing it forward of 0.0 by the amount of the overlap. In addition, the code should really scale by Time.deltaTime:

function Move()
{
    var speed : float = 1.0;
    transform.position.z = transform.position.z + speed * Time.deltaTime;
 
    if(transform.position.z >= 20)
    {
       transform.position.z = transform.position.z - 20.0;
    }
}

There may still be roundoff errors that cause an opening. I’m not sure without testing, but the situation should be much improved.

The problem with your logic is due to the scale of your movement… if you set speed to “1”, the object is moving 1 world unit forward EACH FRAME.

lets say the object is 3 and a half units long… since it is moving in 1-unit increments, the next guy in front cannot be 3 1/2 units ahead, it must be 4, which leaves a gap of 1/2 a unit… increasing the speed increases the degree of inaccuracy!

to fix, you need to add logic to fill the gap somehow… the trick is to make sure that the speed is always precisely divisible by the objects length…

  var speedIncrement = renderer.bounds.extents / 100;

  if(whatever)
  speed += speedIncrement;

(for example)

you shouldn’t really need to use bounds.extents… just make sure the increment goes exactly into the desired length…

come to think of it, the target distance (instead of 20) may also need to be a multiple of said length in this scenario… or you will need to account for that difference…