Asteroid Dodging in 2D (part 5)

I feel like this will be the last blog post about this until I have some more time to look into it again over the Christmas holidays. We wrapped up some pretty simple coding and were let loose in class to play each other’s games – I quite like mine after I did a few more modifications today. It seems like I have more ideas floating around in my head as to what I could add, or even change, to make the game more my own and practice my programming skills at the same time.

For the first 20 minutes or so of class, we were shown the code to make the camera follow the player ship around the screen. This wasn’t exactly something I had been thinking about adding to the game, but it did end up looking all right. It was also only about 2 lines of code, so it wasn’t exactly difficult to add to a new CameraController script:


This sort of code could be quite useful, depending on the type of game being made. I could imagine it being exceptionally useful in something like a side-scroller where you want the camera to following the player as he/she moves along the playable area. Adding this to the asteroid game meant I had to do some alterations to the game scene itself. I could create a much longer level, but I also been to add some boundaries to either side of the level, so the player wouldn’t just wander off into non-playable space.


After that, I decided to add a rotating background by adding my star background material to a spinning cylinder set behind the 2D screen. I’d seen mention of something like this when I was looking into how to create tiled backgrounds in 2D Unity and thought it was quite cool. After all, space could be moving all around you, even if you’re not! I also just thought it was a good opportunity to try and do it myself for practice.


However, because I didn’t see the point in adding yet another rotation script when I technically already had one for the asteroids, I decided to just alter it with a couple of extra lines:


Rather than just having a public float of “speed”, I thought I could just change the variable and add two new ones, so I could punch in the X, Y, and Z rotations of whatever object the script was attached to.

This worked a treat and the background now spun around.

While I’d looked into adding the pick-ups Ant mentioned in his overview videos, it could be an added challenge to throw in a few of them for the player to pick up before being able to go through my newly-created rift (to get to the next level). I worked with Marc to try and figure out the code, but I did have to ask for a little bit of help in the end; I had been close to figuring it out, but had left in some code that didn’t mesh well with what we were trying to do.

We’d ended up changing the starCollected part of the script we’d done from the tutorials to instead create a list within Unity, which could keep track of how many stars were picked up in the level. Then, because the stars would be added to a list, but destroyed if the player picked them up, we could add an if statement in as such:


This way, once the list of stars (or pickups, as the tag remained unchanged from the video tutorials) reached less than 1, the player could move through to the next level. This all seemed to work very well and I dotted a couple of stars throughout my level – in rather difficult locations, I suppose, since even I have trouble getting them all in the allotted time!

I also ended up adding a couple more scripts to the game, including one to spawn asteroids randomly – according to the people who played the game during the open session in the last 20 minutes of class, this added quite the challenge.


My biggest problem for this script was getting the coding in the right place, because I just plucked it out of the Ant’s tutorial video. As can be seen, I had to comment out some parts that didn’t work properly for me, so perhaps I could look a little more into this to make it a bit cleaner. It’s possible that the asteroids are also spawning on top of the player ,because this script was meant to be attached to the camera, and could potentially insta-kill the ship in some cases. Again, this is something I need to fix, possible by using an empty game object to spawn the asteroids instead of the main camera.

Finally, the level was coming together nicely, but there was one last thing I wanted to add before I thought I could then make a game out of everything I had. I’d seen it in some older videos on Moodle, through the Unity section, that Ant must have posted for a previous class/year. In it, his spaceship used the Unity particle system to add a nice jet burn whenever the player moved.

I went through the video and I’m really glad I did, because I think the effect is really good! The particle system in Unity could prove to be a lot of fun to play with, so I’d like to look into it a bit more – I’m wondering about rain or snow effects in game, or even missiles/obstacles dropping from the sky that the player has to avoid.

Overall, I think the game looks quite smart and I’m looking forward to polishing it up to make at least a semi-playable game with… let’s start with 5 or so levels and see how I go!



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s