Glitch repeating scenario

I tried to make the scenario in my 2d project repeat, to do it i duplicated the scenario put the second one on the right of the original one, and made an script wich uses the camera x position(camera is following player x position) to check if the scene is out of the field of view of the camera, it works, but i added a movement boost to my player when the screen is double tapped, and if the player movement is boosted, the scene starts to show a glitch, when one of the scenario goes out of the camera field of view and its thrown to the right, if the “boost” happens it starts to show some opening between the scenarios, here is the code i made to move the scenario:

public class SceneControl : MonoBehaviour {

``````public Transform myCamera;
float impulse;
float space ;

void Start () {
impulse = 15.55f;
space = 10;
}

void Update () {
if (transform.position.x < myCamera.position.x - space)
{
transform.position = new Vector3(myCamera.position.x + impulse,transform.position.y, transform.position.z);
}
``````

}
}

Problem Cause:
You are setting the position of the object(scenario) incorrectly. When you base it off the position of the camera, you will not always get the same results if the Camera is dynamic and not guaranteed to be at the same position on separate runs. So in one instance, the distance between transform.position.x and myCamera.position.x - space could be 1 unit. Then on the next instance, the distance between the two could be 4 units. This can happen because the delta time between frames is not constant. So one frame you could move further than another.

Solution:
Instead of moving the object(scenario) based on the position of the camera, move the object based on it’s own width. If the object is positioned at (0,0) in the world and it is 10 units wide, the left side of the object would be at -5, and the right side of the object would be at +5 in world space. Because I know the same object is directly to the right of this one, I can move the object two times it’s own width to move it to the right of the other object.

So you would originally have objects at positions, (0,0) and (10,0).
You then move the first object transform.position = new Vector2(transform.position.x * (width * 2), transform.position.y).
Giving you two new positions of (10,0) and (20,0).

You will need to configure this to suit your specific situation and objects.