- Home /
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.
If your background can be represented by a single tile that repeats (i.e. the left side tiles with the the right side), then using $$anonymous$$aterial.SetTextureOffset() is a simple way to create an endless scrolling background. There is a bit of example code in the reference:
https://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$aterial.SetTextureOffset.html
Answer by aled96 · Jul 23, 2014 at 08:53 AM
Watch this video, it can help you I think :)
Answer by king_ · Apr 01, 2014 at 06:05 PM
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)
UPDATE
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); } }
Tried it, nothing happend. The OnBecameInvisible is not called even when the background is not appearing in the screen anymore :/
Answer by zee_ola05 · Feb 23, 2014 at 10:37 PM
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.
Thanks for the reply,
How could i go about doing this roughly? How do I calculate what the camera sees etc?