Asteroid Dodging in 2D (part 1)

After having a peer review of our walking simulator games, we began learning about vectors and how to utilise them in Unity. We’ve already been using vectors to transform, scale, and rotate (perhaps without really knowing it) while using Unity to build our games. Each item will have a transform component attached to it, denoting where it is in Unity on an X, Y, and Z axis. We can also rotate on each of these, and scale, using the transform window:


In 3D projects, we use all three dimensions (vectors) to determine the position of an item, but in 2D projects you only need to use the X and Y axis (although rotation, as we’ve now discovered, is done on the Z axis because otherwise it looks odd).

Ant gave us a few exercises to practice our understanding of vectors and moving objects with arithmetic. Here are just a few to keep in mind for future understanding:

(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)

In the above examples, we also utilise a scaler number, which is useful for scripts where we want the object in our game to move in an even, linear fashion.

Eventually, we were let lose to start creating our own asteroid game with some pre-made assets; these particular ones are from (the king of sprites), where you can get free and open-source 2D or 3D assets. I’ll need to keep this site in mind for future projects and perhaps trying to build my own practice game for the 2D skills we are learning and will continue to learn.

Some of this is coming back to me when I started the Rogue tutorial, including adding a background to this kind of game. I began by taking the provided stars sprite and converting it to the advanced texture type. From there, I fiddled with a few of the settings, such as the wrap mode (changing it to repeat) and the mesh type (being a full rect instead of tight).


This is an important step before creating a quad and resizing it to the width/height of the viewable game area.

Once you do that, you can add your stars pattern as the shader and change the type to Unlit/Texture so it’s not too dark and isn’t affected by any external light sources (which we don’t have at the moment, but in the future we might).

You then have to alter the tiling so it looks a little better for the image quality. Sadly, the example stars background we were given isn’t the highest quality, but an 8 x 1 tiling setting seems to make it good enough.

Finally, we were ready to start the animation of the asteroid and put our newfound knowledge of vectors to use. We were guided through the process of creating two new types of scripts: one was to move the asteroid and the other was to rotate it.

The movement script was simple, in that all we had to do was declare a new variable and then, on update, we would transform the position of the asteroid. The vectors would be known as the speed and the position would be calculated from the time the last position was determined, rather than at an excessive rate (using deltatime).

using UnityEngine;
using System.Collections;

public class AutoMovement : MonoBehaviour {

public Vector3 speed;

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

Once we attached the script to the asteroid, we would be able to plug in the X, Y, and Z values rather than rely on the script to do that for us. This will help in the future for any other objects we may want to move automatically, which is why it’s important not to use set numbers in scripts if a variable can be defined instead.

From there, we followed the same sort of structure for the rotation script:

using UnityEngine;
using System.Collections;

public class AutoRotate : MonoBehaviour {

public float speed;

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

We have been showed rotation before, namely when we went through the roller ball tutorial and had the pick-ups spin, but I’ve always used them early on in my walking simulator for doors. Since we wanted the asteroid to look like it was spinning as it was facing the camera (and not Paper Mario style), we needed to use the Z axis, which is why we use a float (numeric variable) to determine the rate at which it will spin, unique to the asteroid the script is attached.

While the scripts are simple, the effect is quite nice and will be much more chaotic once more asteroids are added, zooming in different directions.



Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s