Category Archives: Beginning Game Design

Beginning Unity Game design from T1 2015-16

Unity — progressing through levels

In our block breaker game we have a sequence of levels we want the user to go through — main menu to level 1, then 2, then 3, and eventually, a win screen. How do we manage that.

Well, our levelManager script uses the Application.LoadLevel(“levelName”) to load a specific level. You can use the same method with a number to load a level, too. That number is based on the order the level appears in your build settings, and it is called the index.

The Application object also has a property called Application.loadedLevel, which returns the index of the current level. So, to load the next level in order, all you have to do is call:

Application.LoadLevel(Application.loadedLevel + 1);

I’d suggest dropping that into a new function in your levelManager called void LoadNextLevel(){…}

Hope that helps.

GD: Preventing Boring Loops

Today we worked on two tools to help prevent boring loops.

  1.  Change the collider on the paddle to a polygon collider.  Give it angles at either side to give the player some control on the direction of the ball.
  2. In the ball script, put in code to change the velocity by a random amount on the x and y axes.

First, get the RigidBody2D

generate two random numbers (changeX and changeY)-> Random.Range(-2f, 2f);

change the velocity:

rb.velocity = new Vector2(rb.velocity.x + changeX, rb.velocity.y + changeY);

 

Tracking Ball to Paddle

Today we talked about how to get the ball to follow along with the paddle until the start of the game.  We didn’t get all the way, but we got most of the way.  Here are the steps we talked about needing to do:

  • At the start, the ball should follow along with the paddle
    • maintain the same relative position
  • When the user clicks the button, it should release the ball
    • needs to launch the ball

So, to manage the first of these, we need to start out knowing the distance between the paddle and the ball.  We’re going to do all this work in a script attached to the ball.  It is going to need to know things about the paddle.  The easiest way to do this is to have a public variable called paddle.  The type of that is going to be the same as the class name you gave the paddle’s script.  Mine is “mouseControl”.

So in the ball script, after the class declaration, you want something like:

public mouseControl paddle;

Then in Unity you’re going to need to drag the paddle into that portion of the ball’s Inspector to link the paddle to the ball’s script.

You also will need variables to hold the difference in position between the ball and the paddle at the start of the game. Those don’t need to be public, but they do need to be global.  Under the line of code above, I added the following to declare two float variables called offsetX and offsetY:

float offsetX, offsetY;

So, in void Start(), you want to calculate that difference.

offsetX = this.transform.position.x – paddle.transform.position.x;

I’ll let you figure out the offsetY code — it’s quite similar.

Then, in Update, you want to follow the paddle with every frame, plus the offsets.  I like to separate out the pieces to avoid a long line of code, so I create a couple new floats and then create the new Vector3 using those.

float newX = paddle.transform.position.x + offsetX;

float newY… (I’ll let you figure that one out, too.)

Then you set the new position.

this.transform.position = new Vector3(newX, newY);

And that should have them flying along together.  It breaks the game overall, so check back for the next step next week.

 

Identifying a win in Unity blockbreaker

Identifying a win in something like a block breaker game means that you have to have a count of the number of blocks created and you need to subtract one every time the block gets destroyed.

If you use a normal variable, the count will be one for each one, and they won’t add up together — the count would belong to each instance of block.  What you really want is a count that belongs to the class — the factory that makes the blocks.  So, you use what is called a Static variable.

public static int blockCount;

 

Then in Start (or Awake) you want to add one to that variable:

blockCount++;

Finally, in the Collision code that destroys the block, you want to subtract one, check if the total is zero (or less) and respond appropriately.  In this example, I’m just going to print “You Win!” to the console.

void OnCollisionEnter2D(Collision2D other)
{
print(“I’ve been hit!”);
blockCount–;
print(blockCount + ” blocks left”);
if(blockCount <= 0)
{
print(“You Win!!!”);
//OR LOAD THE NEXT LEVEL OR GO TO WIN MENU…WHATEVER IT IS
}
GameObject.Destroy(gameObject, 0.5f)
}