2D Unity – Asteroids

Once this week peer reviews for the 3D Unity games were over and done with we sat down to discuss vectors and moving into a 2D project. The walking sim will be ours to polish and add to in the background for use in our portfolios.

Vectors

Okay this next bit is pretty embarrassing. When quizzed on what a vector is I drew a blank even though I’d used them in my project already for rotating the keys, door movement etc. Once explained I kicked myself. A vector is the three values we’ve been associating with X, Y and Z axis, which makes perfect sense why when translating things in Unity I’ve been calling on the function Vector3.

Traditionally a vector is a combined measure of direction and speed and this sense of the word also exists within Unity on rigid bodies for calculating physics on objects.

We ran through a few examples on the board of how simple arithmatic works on vectors and is how everything from Maya to Unity transforms objects.

(4.6, 2.15) – (-1.7, 3.25) = (6.3, -1.1)

(1.5, 2.2, -1.6) * 3 = (4.5, 6.6, -4.8)

(-6, 5.2) * -0.4 = (2.4, -2.08)

Where the vectors are being multiplied by a number, this would be an example of  uniform scaling as all vectors are multiplied by the same amount.

2D in Unity

My lovely corridors in space are gone, relegated to being worked on at home (you know where the entire thing was made anyway) and in front of me is a new blank project with one missing axis. Ladies and gentleman, Z has left the building.

After importing a few provided assets into the project (provided by the wonderful website kenney.nl) we had a spaceship graphic, a background graphic and an asteroid graphic. We dragged the ship and asteroid into the scene and went straight back to vectors to add some animation. Two scripts were created, one for movement and the other for rotation.

using UnityEngine;
using System.Collections;

public class AutoMovement : MonoBehaviour {

public Vector3 speed;

void Update () {
transform.position = transform.position + speed * Time.deltaTime;
}
}

The movement script was simple. We create a public Vector3 variable known as speed, on every frame our current position is added to the value defined in our variable to create our new transform. We could have added a set value in the script rather than defining a variable to add these numbers in a component, however this method makes the script usable for multiple circumstances which is why its important not to use numbers if you can use a variable instead.

using UnityEngine;
using System.Collections;

public class AutoRotate : MonoBehaviour {

public float speed;

void Update () {
transform.Rotate (0, 0, speed);
}
}

I’ve used rotation before, mainly on my spinning keys in my other Unity game. This version is only slightly different. To get the asteroid to spin and not flip around in 2D space we still have to spin it on its Z axis (guess Z is still around after all). So the script has no values to rotate on X,Y but has a variable for Z which we can set in the component.

Here is an example of both scripts in action recording using GIFcam.

asteroids

There wasn’t enough time in the lesson to continue further due to how long the peer review & play tests took but look forward to continuing this in the next session.

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