- Home /
Vertically scrolling 2D background clips
I'm making a vertically scrolling 2d background that loops. Here's the code:
using UnityEngine;
using System.Collections;
public class BackGround : MonoBehaviour {
//the point the background goes back to start
private float reset = 19.2f;
//the point where the background goes after a reset
private float startPoint = -19.2f;
//the speed of movement
private float speed;
void Update () {
//if the background is at reset point, move it to startPoint
if(reset <= transform.position.y) {
transform.position = (new Vector3(0, startPoint, 0));
}
//EnemySpawner.speedPlus is a static float
speed = 0.05f + EnemySpawner.speedPlus;
//Move the backgound up
transform.position += new Vector3(0,speed,0);
}
}
The problem is that there is a space between the backgrounds where is nothing. Background 1 is set to (0,0,0) and background 2 is at (0,-19.2,0). -19.2 seems to be the right coordinate so that the background 2 is just right under the background 1 (they are the same picture).
![alt text][1]
As you can see in the image, there is a white line between the backgrounds. I want that line gone.
It's my first post here so pardon me if i did something wrong. Also feel free to ask anything. [1]: /storage/temp/27046-bug.png
Answer by Asdemt · May 30, 2014 at 06:00 PM
Managed to fix it with my own way as changing reset point didn't help and solution 2 was too time consuming. For anyone with the same problem i just made the background to go to the exact position of the lower backgrounds end.
using UnityEngine;
using System.Collections;
public class BackGround : MonoBehaviour {
//the point the background goes back to start
private float reset = 19.2f;
//the point where the background goes after a reset
private float startPoint;
//the speed of movement
private float speed;
public GameObject otherBackground;
void Update () {
SpriteRenderer sRenderer = otherBackground.GetComponent<SpriteRenderer>();
startPoint = transform.position.y - sRenderer.bounds.size.y*2;
//if the background is at reset point, move it to startPoint
if(reset <= transform.position.y) {
transform.position = (new Vector3(0, startPoint, 0));
}
//EnemySpawner.speedPlus is a static float
speed = 0.05f + EnemySpawner.speedPlus;
//Move the backgound up
transform.position += new Vector3(0,speed,0);
}
}
Thanks for the support anyway.
Answer by jasonkutty · May 29, 2014 at 10:07 PM
It is probably because of the origin of the "reset point". Try moving the reset point a bit closer than before.
Solution : 2 Instead of moving backgrounds by repositioning, try Instantiating the backgrounds and have a constant scrolling of backgrounds. Destroy panes that go out of camera view to improve performance as well.