- Home /
How to create an infinite, non-moving background in a 2D game
Hello, I'm very new to Unity. No experience. How could I create an background that when the character moves it goes on forever. Like in Flappy Bird the background stays planted By infinite I mean that by any resolution viewed, the background will be repeated. Please explain in steps.
Thanks!
Answer by robertbu · Jun 15, 2014 at 05:08 PM
Assuming an orthographic camera:
Create a Quad that is sized for a single panel of the background and positioned for the background.
Make the Quad a child of the camera.
Create a background image that wraps. That is if you do an Offset on the horizontal in Photoshop, no seam appears.
Create a material with whatever shader you want and this texture.
Attach the following script to the Quad:
pragma strict
var factor : float;
function Start() { factor = 1.0 / transform.localScale.x; }
function Update() { renderer.material.SetTextureOffset("_MainTex", Vector2(Camera.main.transform.position.x * factor, 0.0)); }
Note this code assumes no 'y' movement of the camera, but this code is easily extended if your camera has 'y' movement.
Your answer
![](https://koobas.hobune.stream/wayback/20220613151612im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Creating pixelperfect backgrounds for a 2d Game 2 Answers
Background Image in 2D 1 Answer
2D Backround Scroller 1 Answer
2d Platformer sprites background 1 Answer
How can I stop my endless runners background sprites from falling out of sync? 1 Answer