Creating a Scrolling Background (2D Space Shooter)

Marcus Ansley
2 min readMay 12, 2021

--

In-game view

I noticed a couple of course-mates implementing some nifty scrolling backgrounds to their space shooters so thought I’d join the bandwagon 😉 I wasn’t initially sure how to do this but then realised I was probably overthinking it: I just need about two or three background sprites which move down and eventually get repositioned at the top of the scene.

While we could do this with a single, appropriate background image, we could also do this with a static background image and have some moving sprites over the top of it. In my case, these moving sprites would be of some stars, and a brilliant free asset pack (currently on the Asset Store) provided everything I needed as far as the visuals are concerned.

So after swapping out the background image, I worked on the logic for the moving sprites (which is admittedly fairly straightforward). As with a lot of other objects in the game, I’m using the Transform.Translate method and Vector3.Down to move the sprites down (multiplied by a ‘speed’ multiplier), and am checking to see whether the object’s transform’s Y value is less than -8 (in which case, it’ll be off-screen and safe to move back to the top).

As you can see it snaps back to the top of the scene

For a little bit of polish on this whole effect, I have the background remain static when the player begins the game, and after they destroy the initial asteroid I begin the scrolling, increasing the speed of this scrolling to its max over the course of a couple seconds (using Mathf.Lerp on the mulitiplier in a separate coroutine). I also have the player’s thruster kick into life when this happens, but I might also add a scaling effect to the thruster to further give the impression/illusion of the player gaining speed and traversing the cosmos 😉

I’m glad to finally have this in the game and was pleasantly surprised at how inexpensive the effect is and how quick it was to set up (I do feel it adds a nice bit of energy to what might otherwise be a somewhat static game).

--

--