- Home /
Wrong placing for background
Hi. I need to make an endless background. I tagged all my backgrounds with the tag 'Background' and created an empty game object, which will, when collides with the background, perform this code:
background_width = ((BoxCollider2D)target).size.x;
// we create a new position for the just touched background
Vector3 newPosition = new Vector3(lastBackgroundX + background_width, target.gameObject.transform.position.y);
// chanes its position to the new one
target.gameObject.transform.position = newPosition;
// change the lastBackgroundX so that we will be able to find a new position for the next background
lastBackgroundX = newPosition.x;
Well, this code simply works not as it should. Instead of putting background one after another, my code, I don't know why, puts next background at a ridiculous distance from the previous one. So there is no 'endless background' effect. What should I do?
P.S: value to the lastBackgroundX is assigned at the start of the game, my scripts finds the farthest object with the tag 'Background' and assigns its transform.position.x to the aforementioned variable.
Why finding the farthest background? shouldn't it be LastBackgroundX = Target.gameObject.transform.position.x;
or more simply Vector3 newPosition = new Vector3(Target.gameObject.transform.position.x + background_width, target.gameObject.transform.position.y);
Your answer
Follow this Question
Related Questions
Large background image, should I cut it? 1 Answer
background GUI texture 2 Answers
Changing camera background color in runtime 3 Answers
Trying to change button background using materials 1 Answer
PNG got green background when imported? 0 Answers