Create a Continuously Scrolling Background in Unity3D

Pete Thomasberger
3 min readJun 25, 2021

Objective: Create a continuously scrolling starfield background element to simulate movement.

Step 1: In Unity, within the Hierarchy window, create a new Folder called Stars. Drag the starfield asset into the Stars folder and rename it to Stars1. Duplicate the asset and rename the duplicate to Stars2.

Step 2: In the Project window, right-click on the Scripts folder, create a new C# script named Stars, and attach the script to the Stars GameObject in the Hierarchy window.

Step 3: In the Stars script, create two private GameObject variables called _stars1 and _stars2, then use [SerializeField], and save the script.

Step 4: In Unity, select the Stars GameObject in the Hierarchy window and drag the Stars1 and Stars2 GameObjects into the corresponding slots in the Stars (Script) section in the Stars GameObject’s Inspector window.

Step 5: In the Stars script, create a private float variable called _starsSpeed and have its value equal to 2.0f.

Step 6: Create a new private method called StarsStarPosition() and set the start position for _stars1 and _stars 2 so that they are stacked on top of each other on the Y-axis and _stars1 is visible on-screen.

Then call the StarsStartPosition() method within the Start() method.

Step 7: Create a new private method called StarsMovement(). Starting with the _stars1 variable, have _stars1 move down the Y-axis, multiplied by the _starsSpeed variable, then multiplied by Time.deltaTime. Next, create an if statement stating that if _stars1’s position is off-screen, create a new position at the top of the screen, stacked on top of _stars2. Then create the same logic for the _stars2 variable so that it repositions itself above _stars1 when off-screen.

Then, call the StarsMovement() method from within the Update() method.

Save the script, go back into Unity, and the stars will start scrolling indefinitely.

--

--