Small Gap between endless cubes
Hi,
I create cubes that are moving right. When the first cube moved his whole x-scale I create another one and so on. Inbetween the cubes is a gap and I can't figure out why. Here's a Video:
Here is the code (it's rather simple):
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MoverScript : MonoBehaviour
 {
     public GameObject cube;
     public float speed = 1.0f;
 
     private List<GameObject> objects = new List<GameObject>();
     // Use this for initialization
     void Start ()
     {
         CreateNewObject();
     }
     
     // Update is called once per frame
     void Update ()
     {
         bool createNew = false;
 
         foreach (var obj in objects)
         {
             obj.transform.position += Vector3.right * Time.deltaTime * speed;
 
             //Cube reaches "end" => create a new one and never do it again for this cube.
             //The tag is used to flag this cube as "handled"
             if(obj.tag != "done" && obj.transform.position.x > obj.transform.localScale.x)
             {
                 createNew = true;
                 obj.tag = "done";
             }
         }
 
         if(createNew)
             CreateNewObject();
     }
 
     void CreateNewObject()
     {
         GameObject instance = Instantiate(cube, new Vector3(0, 0, 0), Quaternion.identity);
         objects.Add(instance);
     }
 }
Answer by Irrgeist · Jun 08, 2018 at 10:48 AM
Okay I found the problem:
The new object has to be instantiated at it's neighbors x-position + Scale. This seems to work perfect, regardless of the speed. Makes sense since:
The check if the cube is > 1 depends on the game's FPS. Sometimes the cube is at 1.2f and creating a new cube at this moment, will create a gap.
Your answer
 
 
             Follow this Question
Related Questions
How to store a position that is moving to create a prefab to spawn at the exact position 1 Answer
Instantiate prefab if no prefab exists at location 1 Answer
Instantiated prefab has a z-axis of 90 0 Answers
What is different when Instantiate set Prefab Position? 1 Answer
Convert Local Position To Global on a Child Object 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                