2D Unity Asteroids – Part 2

Continuing from last week we updated the 2D space scene to add player controls to the spaceship.

However firstly we sat through a lecture on variables and data types. Enforcing the core knowledge behind some of the scripting we’d already created, but lacked context. We covered;

  • Understanding basic C# data types
  • Declaring variables
  • Access modifiers
  • Compound data types

Naturally its important to know what a data type is, so you know what kinds of information it can hold and what this information can then be used for. The examples given to us were;

  • A string – this is simply a chain of characters (letters or numbers), written as string.
  • An integer – this contains a whole number, written as Int.
  • A floating point – this contains a decimal number, written as float.
  • A boolean – can only be true or false, written as bool.

Now all of this is familiar from my years working in IT. Not only does Excel use similar terminology for types of data it can store in a field, but I also spent time using SQL Server in my old job and that DEFINITELY requires data fields to be determined at the design level of the database.

The format for turning one of these data types into a variable is;

datatype followed by name of data type = given value

For example;

  • string characterName = “George”;
  • Int characterHealth = 99;
  • float characterSpeed = 0.5f;
  • bool characterInvincibility = true;

Variables will always be written in camel case whereas a lot of my time in SQL tended to name things in Pascal case. The last thing worth mentioning is if you need to access your variable from other places, in our case the inspector in Unity you’ll need to make it public. This is achieved by simply writing public in front of the above examples. So in the case of characterSpeed, we could change the characters speed on the fly from the inspector without having to edit the code. This makes the code a lot more re-usable on other game objects.

Back to the ship!

Last week  we got the asteroid moving with a rotation script and an auto movement script, however the scene had no interactivity leaving the ship a stationary derelict lost in space.

  using UnityEngine;
 using System.Collections;
 
 public class ShipController : MonoBehaviour {
 
     public float enginePower = 3f;
     public float rotationPower = 100f;
     public Rigidbody2D myRigidbody;
 
     void Update () {
         // Rotate the ship based on key presses
         float degrees = rotationPower * Input.GetAxis("Horizontal");
         transform.Rotate (0, 0, -degrees * Time.deltaTime);
 
         // Thrust forward
         Vector2 direction = transform.right * Input.GetAxis("Vertical");
         Vector2 thrust = direction * enginePower;
         myRigidbody.velocity = myRigidbody.velocity + thrust * Time.deltaTime;
     }
 }

When beginning the script we declared all our variables needed for the ship to function.

  • A public floating point variable called enginePower to contain the value that would determine the speed of our forward movement.
  • A public floating point variable called rotationPower to contain the value that would determine the speed of our rotation.
  • A public Rigidbody 2D to allow us to apply rigidbody physics into our scripts.

To set up the control of our ship rotation, we declared a float variable named degrees, equal to rotationPower multiplied by key press. To set the rotation our Z value is set as degrees multiplied by ‘DeltaTime’. As discussed last week Delta Time actually works out these values over seconds and not frames, as not all computers render the same amount of frames in a second.

Thrust for our ship is setup in a similar way to above. A vector2 variable is declared as direction which equals transforming right multiplied by our key press (vertical), which takes care of our control. A further vector2 variable is declared named thrust which equals direction multiplied by enginePower (our variable from earlier). Finally we add all of this to the rigid body controller and tell Unity to use the result of thrust over delta time.

The end result looks a little something like this.

shipmoving

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