Endless 2D background

Hello, I’m creating a flappybird clone and working on creating an endless background.

The way I have done it is I have a ‘Trigger Manager’, which has 2 trigger colliders as its child. One that triggers once the player collides, and this instantiates the background . The next trigger uses waitsforseconds and then deletes the previous prefab.

Is there a better way to have a endless background? My method is running into major issues.

Watch this video, it can help you I think :slight_smile:

if anyone still looking for an answer i’ve my own short script,without instantiating the background,use two backgrounds and attach this BackGrounScript to both backgrounds and set the width of the BG in the inspector panel for both BG(width must be in unity units)


now you can use this for endless y axis back ground

  • for y axis endless BG set the height of your BG in the inspector(set width to 0)
  • for x axis endless BG set the width of your BG in the inspector(set height to 0)

using UnityEngine;

public class BackGroundScript : MonoBehaviour {
	private Vector3 backPos;
	public float width = 14.22f;
    public float height = 0f;
	private float X;
    private float Y;

	void OnBecameInvisible()
		//calculate current position
		backPos = gameObject.transform.position;
		//calculate new position
		print (backPos);
		X = backPos.x + width*2;
            Y = backPos.y + height*2;
		//move to new position when invisible
		gameObject.transform.position = new Vector3 (X, Y, 0f);


Just create 2 background sprites (should be looping, ofcourse) and change the position based on what the camera sees.

Do not instantiate many background sprites. 2 is usually enough. Just recycle them… do not destroy/initialize.

You have seen this tutorial?