Asteroid Dodging in 2D (part 4)

We’ve covered a multitude of things during our time building an Asteroid dodger in Unity; today we covered conditional statements (as well as adding a timer to the game), how to create Prefabs, and including pick-ups for the player to collect before they can move on to the next level.

I’ve used conditional statements in previous projects, especially our walking simulator games, so I was already fairly comfortable with the syntax. By definition, a conditional statement will only happen under certain circumstances; an IF statement lets you ask a question, where if it’s true the block of code following it will run.

There are 3 part to an IF statement:

  1. The keyword IF
  2. Condition (in normal brackets)
  3. Body (in curly brackets)

So, for example:

if (number == 1) {
Debug.Log (“Heads”);
}

The above example existed in the made-up scenario of flipping a coin, where the randomised number could be either heads (1) or tails (2). Using this basic outline of an IF statement, we could turn our attentions to the asteroid game and see how to use IF statements to round out the game even further.

leveltiming

In the above, we added a timer to the level – by declaring a new public float of time = 5f; we could set the length of the level in seconds. Since it was a public float, it could also be altered for each individual level. This would be very useful if some levels are more/less challenging than others, thus the player needs more/less time to get to the goal.

Moving on from there, I worked on a way to have the time show up on the screen itself, so the player could see how many seconds they had left. It wasn’t very nice to give them a time limit but not tell them what it was! So, adding UnityEngine.UI in, I added a text box to my canvas and started to declare my variable to hold the time that would be printed. The only issue with this was text boxes in Unity can’t accept a float, which was what our time was. I figured out a way to fix that:

timerstring

The above code would take the value of time, round it to the nearest whole integer, and then convert it to a string. This way, it could be easily printed to the UI. It would just need to be called in Start and Update in order to work.

As seen here, it was easy to call the function and all of it worked just fine in the end.

Finally, we had a multi-level game with a start screen, credits, and different ways to interact with the environment (crash, run out of time, reach a goal, etc.). The next thing to learn was adding pick-ups. We had done something very similar in the Roll A Ball tutorial a few months ago, but Ant kindly provided us a walk-through video on how we could add some star pick-ups to the game.

Following the tutorial was straight forward and ended up using more conditional statements, both basic IF ones and IF/ELSE statements.

pickups

I really do enjoy writing this kind of code, since it’s so versatile and you can really do so much in a game just by knowing this. It brings about an added challenge to the game: the player will have to collect a star on screen, but if the player hasn’t collected the star yet then they can’t move on to the next level. The next level will only load if the starCollect bool is changed from false to true. If the player tries to go straight for the goal, then they’ll just fly right through until they’ve collected the pick-up.

gameplaywstar

What would be interesting to test out would be collecting multiple stars in the level, therefore changing the IF statement to something where the number of stars collect has to equal however many are in the scene. I’ll have to look into it some more, but for now the basis of the game is quite good and gives a lot of ideas for changes that could be done to add a personal touch.

One more thing we covered in the lesson was Prefabs. This idea is absolutely essential to creating larger games where the levels share one or more objects; the asteroid game is no different, since each level will share a player ship, environment colliders, and even the timer text I’ve now added.

prefabsBy dragging and dropping the objects from your hierarchy to your project build, it creates a snapshot of the object as it already is, and thus it can be dropped into any scene you like with the same scripts, settings, etc. This essentially means you can create levels much quicker and have fewer mistakes across the board!

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s