Watching The World Go By!
Objective: to give the Player an illusion of forward movement by implement a scrolling background and particle systems in Unity 2D.
In my current version of the GameDevHQ Space Shooter Unity 2D Game Project, the assets provided included an image of a space background with stars, galaxies, and nebulas. Quite nice, but after a while, I felt it lacked a bit of je ne sais quoi. I just wanted something a little better, something that would add to the visual experience of playing the game. It needed to move. Here’s how I went about doing this…
The first task was to create an empty game object, reset it to the origin, and set it as a child of the MainCamera. I renamed my object ScrollingBackground, and it will serve as the “parent” for all the elements of my, well, scrolling background.
For the background, I searched for a nice star field image in png format and imported it into my Assets/Images.
I then created a new material, saved it as Starfield, and in the albedo field, I dragged into it the desired image file.
I then set the shader to UI/Unlit/Detail.
I then set up a new child game object. This one is called SpaceBackground. I added a Mesh Filter and Mesh Renderer components.
In the Mesh Renderer, I dragged the Starfield material into Element 0 of Materials.
The next step is to add the following script to the SpaceBackground game object. You start by caching a reference to the Player and the background mesh renderer.
In the Update(), you use mainTextureOffset to offset the main texture at every frame (Mesh Renderer material) to give the illusion of moving through space.
This would take care of one method. The other way to create the illusion of movement is to add a particle system at the top of the screen and have the generated particles translate downwards across the game view.
I added two Particle Systems and set them as children of the SpaceBackground. One is called SpaceClouds, and the other SpaceDust.
Here’s a side-by-side comparison of the settings between both Particle Systems:
Both Particle Systems will be identical for the Emission, Shape, and Renderer sections.
And finally, I tweaked the Color over Lifetime of the SpaceClouds to add a bit of variation:
The final results can be seen below. Starting with an empty background, then with the scrolling starfield, and finally, the clouds and dust are added.
I hope you’ve enjoyed this article and will find it useful in the development of your own Unity game projects. Thanks for reading :)