Asteroid Dodging in 2D (part 3)

In our programming class today we continued to learn more about coding; more specifically, we covered Functions (also known as Methods or Subroutines). It was important to understand the concepts of a function before we would learn how to define a parameter and discuss the process, before covering Unity’s message system.

A function is a named, reusable collection of instructions. We’ve used them plenty of times since we started coding in Unity, we just may never have realised their purpose or why we were doing what we were doing. One of the major benefits of using functions is that you can store code to be used later, over and over again. For example, we have been using void Update () and void Start () from the very beginning of our classes – these are both built-in functions within Unity.

All functions have 3 main parts:

  1. a return type (the “void” we’ve been using is a data type… or lack thereof, but you could use int if you were looking to call a function about numbers)
  2. a name (or identifier) (such as Update, Start, OnCollisionEnter2D, etc.)
  3. a set of parameters (this is optional, but they’re the contents of the brackets)

A more complicated example would be:

int AddNumbers (int a, int b) {}

  • int = the return type
  • AddNumbers = the name
  • (int a, int b) = the set of parameters

Once we’ve made a function, we have to call it. A function call has two parts:

  1. the function’s name
  2. a set of parameters (if needed)

Overall we were shown that functions can be great for “refactoring” (a fancy word for cleaning up) your code – all you’re doing is taking your snippets out of bulky code and simplifying your overall script.
updatedshipcontrollerscript

After all that, we were set about to alter the code we’d done for our ship last week. Both the RotateShip and AccelerateShip could be simplified in the Update function.

Rather than having a massive amount of code in our Update function, we created two separate functions for the ship and called on them from Update. This can become even more useful in games where there are more functions than what we just have here.

 

Another useful tool we were shown today was using the Unity menu system. While I tried to some with my walking simulator, I did have issues trying to get the different parts of code to work the way I wanted – every time I tried to bring up a keypad with buttons, the game would slightly break. It will be extremely useful to learn more about buttons from the very basics and how they can be integrated into our 2D and 3D games.

Our first task was to navigate through the menus to add a button to our UI:

canvaswithbuttons
In this screengrab, there are two buttons, but we began with just looking at the Start Game button and ended up adding more as we saw fit.

With this button, we were shown a new script we could implement: when the button is clicked by the player, Unity would load up the first level of the game.

buttoncontrollerscript

This script is very similar to the original one we looked at the other week, when the ship would collide and “die” in game, the game would reload. However, if the ship managed to reach its goal, it would move on to the next level.

 

buttoncomponent

After this, we needed to look at the components. From here, we can see the default button script where we could change the image of our button from the default Unity one to one we made ourselves. However, the more important section is at the bottom where it says “On Click”. This is what will happen when the player clicks on the button. You could implement a variety of things, however here we knew we wanted to call on a script we’d attached to our button (the ButtonController) and the scene that would load was going to be scene1 (or the first level of my game).

I thought that this was brilliant and felt that I could practice a bit more by altering a few other parts of my script. Ant also suggested we could add a credits screen from the main menu, that would take the player to a new scene with the credits displayed.

Below was the final result of the new alterations and a meagre play-through that will be the start of my game. The first asteroid was still being used as the “next level” goal, but I was thinking of implementing wormholes or portals that the player could fly through to get to higher levels.

asteroidgame2

I also noticed that Ant has a number of tutorials based on this lesson linked through Moodle, so I think I’ll have a bash at adding a few more things to the game before our next class. Chances are, I’ll post about the tutorial as/when I get to it!

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