Create a Scrolling Background in Unity
One of the more convincing methods to perceiving motion is to have a background that is moving. It’s a trick that’s been used for ages and now you’ll be able to use it too.
First up, create a quad and scale it to the size of your scene:
Next up, you need to import an image into your project that you want as your background. You can Google the type of background image you want if you don’t already have one.
Ideally, you want an image that’s set to tile or one with no overlap along the scroll axis like this one:
After importing the image into your project, you will want to create a new material with your new asset by dragging the image into the “Base Map”:
Create a new script and add the following lines of code at the top of the class, under the first curly brace:
The “speed” variable will dictate how fast the image will scroll and the _renderer variable is what gives us access to the image itself.
Don’t forget to access the MeshRenderer component within the Start() method:
The last line of code we need will put everything together and needs to be accessed from within the Update() method:
Your complete script should look similar to this:
After attaching this script to the quad in the scene, press the play button and your background should start scrolling:
In our code we created a new Vector2 which we applied a variable to the x axis and set 0 to the y axis.
In order to move the background vertically, we can simply swap the 2 numbers like this:
With very little effort, you can now scroll your backgrounds infinitely!