- Home /
Wrong coordinates of setting position of the object
I need to make loop background moving(vertically). I have two bg in me scene, when one collide a collider(created for detecting that bg is not on screen space), then this bg is destroying and in this moment i Instantiate a new bg. When i Instantiate a new bg in right coordinates, i have small gap between my two bgs in scene(one bg is old and one recently spawned) I tried to use prefab or just gameobject anyway i have a gap. There is my collider's script:
`
void OnTriggerEnter2D(Collider2D collider)
{
BgControllerGp.instance.SpawnBg();
Destroy(collider.gameObject);
}
`
And my SpawnBg method
`
void SpawnBg(){
Instantiate(backGp, new Vector2(0f, 120f), Quaternion.identity);
}
`
120f is my background collider.size.y and of course it is right coordinates
I tried to scale my y size from 1 to 1.005 and it is works , but expected after 20 spawns or more(i mean some time) i have gap between bgs. I knew this do not good and some time i'll see this gap causes by scaling Help me please, i need Instantiating for my bgs, cause i wanna change sprite of bgs and with Instantiate it is easy.Thank You
Do not scale. $$anonymous$$ake sure vertical size of your bg gameobjects is some round value (like 100 is better than 107.589 ). $$anonymous$$ake your background gameobject's pivot at the bottom. Before Destroying old BG, use its exact position plus vertical offset (old bg height) to calculate desired position of new BG (and I assume you want to see both, the new bg and old bg at the ame time and destroy old bg only when it goes out of the screen)... Al that being said, you better find a best practice way to achieve what you want, because it seems like not an optimal way to make moving background.
Answer by Timbaluke · May 24, 2018 at 10:45 AM
I had exactly the same problem when I was working with moving procedural floor-tiles. Parent all backgrounds to an empty gameobject and set the localPosition instead of the world position.