Particles. Sweet, beautiful, generated particles.
We’re in space, so obviously we need some stars. We could do this by finding a repeatable transparent image of stars, but we already covered that in Unity Dev: Moving Backgrounds.
Having never used a particle system before, I did my research. Much of the code output here is from: Guido Henkel’s Endless Scrolling Starfield and this is primarily my attempt at explaining what’s going on here.
At the end of it all, we’ll have three objects that are all running off the same script so that we can create a parallax effect. For now, make an empty gameobject to act as the container for our starfields and one empty object for our first starfield.
Create a Particle System component on your first Starfield child object and turn off everything but the Renderer option. Expand the Renderer option and select “Default — Particle” for the Material. We’ll handle everything else with code.
Create a new C# script and attach it to your Starfield object. I called mine “Starfield” (Original, right?)
Let’s kick things off with some variables.
All of these are pretty self explanatory. We’re giving ourselves some values that we can tweak in the Unity editor to fine tune our Starfield effect. By giving some thought to our expectations, we want to be able to adjust the number of stars, give them a slight variance in size, control the area that they’ll actually cover, and also, as a bonus, give them a slight red shift the further back they are (or in this case, the smaller they are since we don’t have true depth).
This seems a bit complex, but let’s go step by step.
We’re grabbing the transform of our background camera where we decided to “film” the background action from in my Moving Backgrounds article because we’re going to want a reference to it when we start to move these stars. Don’t forget to add a matching tag to this camera object! We then create a variable to store each particle that gets generated in a list using our
_maxStars value. Next up, we store a reference to our Particle System component and then null check it with
Finally, we create an offset for our x and y axes so that we can generate these stars from the middle of our particle object.
This section seems complex, but to put it very simply, we’re defining the parameters for each and every star in our Starfield in this for loop, giving them a location to appear, a size within our variance, and if they have that slight red colour shift or not. Then we put them in place with
This is a function we define and use in the bulk of the script. We’re defining a starting position for our stars by finding a random point in our Starfield based on the width and height we provided for it while using our offset to spread them out from the center of our rectangle.
At this point, you can now run your game and you should be seeing a field of stars. If it’s not showing up in your Game window, have a look in the Scene window. You may have to do some tweaking on the object’s position.
Great! We’ve got our stars generating but we want to give them some motion to really bring our world alive.
The general idea here is relatively easy. We’re going to continually shift the Starfield object to the left. As that’s happening, we’re going to loop over all the Particles (Stars) and track their positions individually. When a given star is outside of the camera view (camera position less the
_xOffset ) then we’ll add our
_fieldWidth value to our Star’s position, then subtract the Starfield’s position from the star’s position to move it to the right of the camera’s view.
Note: You’ll need to add a new
_speedvariable to your script for the code above to work. My default is
0.5ffor the speed.
For bonus points, duplicate your Starfield a couple times and start mucking around with the variables we exposed from the script.
In particular is the
_speed variable. With multiple Starfield objects, you can create a parallax effect by having each Starfield set to a different speed. Keep in mind that the further back something is supposed to be, the slower it will go in relation to everything else.