Asteroid Dodging in 2D (part 2)

We continued working on our asteroid game in class today, learning more about variables and data types. Our goals ended up being:

  1. We would gain a greater understanding of basic C# data types
  2. Learn about declaring variables (which we’ve already been doing, we just may not have realised it)
  3. How we use access modifiers to make variables available from other places
  4. What compound data types are and how we may have already used them

Since game design is so integral to this course, it was interesting to find out that a lot of it boils down data manipulation – I’ve done this in other jobs before starting this course, so it’s a comfort to know I’m not straying far from familiar territory.

It was important to first understand what a data type is: it can be defined as the form of information being dealt with. This can include an integer, string, floating point, and boolean.

  • An integer is a whole number => written as int
    • ex) int myInteger = 4;
  • A string is a chain of characters => written as string
    • ex) string myString = “Hello world!”;
  • A floating point is a decimal number => written as float
    • ex) float enginePower = 0.5f;
  • A boolean can either be true or false => written as bool
    • ex) bool myBoolean = false;

These are all built-in or primitive types of data; basic pieces of information built into the C# language for you to freely use (which is why we call on system.collection at the beginning of our script).

You need to declare your variable at the top of your script, using the correct syntax (as above, in the examples) – you write the data type and then the name you’re giving it, along with any values. A variable will always be in camel case, while classes will always start with an uppercase. To allow access to your variable from other places, such as the inspector, you will need to add an access modifier of “public” in front of the declaration.

int myInteger = 4; becomes public int myInteger = 4;

variablesinspector

To test how these variables work, we were tasked with testing a few of them and printing out to the console. This was done by creating a simple script:

variables

variablesconsole

 

A success! It was time to put this knowledge to practical application in our asteroid game. We want our ship to move when the player presses down on the arrow keys, which requires a new script and some new variables.

 

shipcontroller

The above script is what we implemented for the ship’s controller – it would allow the player to move the ship forwards or backwards, and rotate around its axis. At the start of the script, we declared three new variables in the same format we’d just learned. We then needed to utilise these variables in two portions of the Update section.

Time.deltaTime is always something I think is so useful: instead of relying on the frame rate, which could be inconsistent across computers, it makes time frame rate independent. It meant that we would be rotating N degree per second, instead of per frame.

We continue to declare some more Vector2 variables for the ship’s thrusters. Vector2 is actually known as a compound data type (because they contain more than one other data type, floats in this case). Vector3 is also a compound data type. However, Ant did say we would go into compound data types at a later date, but it’s something interesting to think about since we’ve used Vector3s and Vector2s already.

shipcontrollerinspector

 

Since I made the variables public, they could now be accessed in the UNity inspector. We had our Engine Power, our Rotation Power, and the RigidBody so we could apply physics to the ship. Adding a RigidBody 2D component in the inspector, we could then drag that into the My Rigidbody to complete the script.

 

 

The final result was a spaceship that could move and dodge asteroids:

shipcontrollergif

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