I have two background sprites. They have a velocity moving down. Both have a single script attached.
Two Issues.
-
The first sprite moves down faster than the second one coming behind creating unwanted space between them.
-
The first one when respawns at above the second. It overlaps the second sprite that do issue with my color gardients. After the second respawns before first one. It respawns at correct place having that velocity created space between.
Script attached to both is:
public class bgScroll : MonoBehaviour
{
private float bgLength;
// Start is called before the first frame update
void Start()
{
Vector2 S = gameObject.GetComponent<SpriteRenderer>().sprite.bounds.size;
bgLength = S.y/10.6f;
Rigidbody2D rigidbody2D = GetComponent<Rigidbody2D>();
rigidbody2D.velocity = new Vector2(0, Time.deltaTime*-Global.speedOfScroll);
}
// Update is called once per frame
void Update()
{
if (transform.position.y < -bgLength)
{
Vector2 groundOffset = new Vector2(0, bgLength * 2f);
transform.position = (Vector2)transform.position + groundOffset;
}
}
}