Monday, 9 January 2012

Project 2: Space Invaders Clone, Game Play

To start to create the game play the first thing I had to do was create a new project in Unity and follow the guide lines set by our teacher of how big the game window should be, where the camera should be set to and the lighting etc...
The blank game window looks like this;
Our first task was to create a small cube which will later be the player of our game, here is what my unity window looks like so far.
I then had to start applying a script to the cube (or should I say player!). I will explain each bit of code to say basically what it's there to do.
The first bit of script I applied to the player is below and the text in the double Asterix's is a vague explanation of what the code is there for.




**Declare player default speed** 
var playerSpeed: int = 10;



function Update ()
{
              **This is the Amount to move or translate the player**
              AmtToMove = playerSpeed * Input.GetAxisRaw ("Horizontal") * Time.deltaTime;


              **Move/translate player**
              transform.Translate (Vector3.right * AmtToMove);


**This is to check if the player has touched the left boundary and if it has then to stop it from going any further left**
              if (transform.position.x < -6)
{
                             transform.position.x = -6;
              }


              if (transform.position.x > 6)
              {
                             transform.position.x = 6;
              }




}


After applying this script to the player object I was then able to move it left and right but also the boundary worked as I couldn't move the cube off screen.


Our next task was to create a bullet for the player to fire. To do this I used the Capsule unity game object.
I had to now create a script to shoot the bullet which meant creating a brand new script for the bullet but also adding a bit to the player script as that will be where the bullet will be fired from.
The first bit of script added to the newly created BulletScript  is as follows;


**This declares the speed of the bullet, this can be changed later**
var bulletSpeed: int = 10;


function Update ()
{
              **The amount to move the bullet**
              amtToMove = bulletSpeed * Time.deltaTime;
              transform.Translate (Vector3.up *amtToMove);

              **If the bullet goes off the screen destroy it**
              if( transform.position.y >= 7)
              {
                             Destroy (gameObject);
              }
}


After applying this script to my bullet upon playing the game the capsule appears but goes straight up until it goes off screen, this let me know that the bullet was doing what I have so far asked it to.
Here is an update of what my game screen looks like at the moment;
I then created a Prefab for my bullet. As the bullet is going to be fired from my player it wouldn't really make sense if whoever was playing the game if they could only shoot another bullet once the previous bullet has gone off screen and been destroyed, therefore by creating a prefab this allows the user to shoot multiple bullets. Now if I drag the Bullet game object over to the newly created bullet prefab it will have the same visual image as the object, so therefore I can now delete the Bullet Game Object as I have it in the prefab.
I now had to edit the player script so that it was able to fire a bullet when the space bar was pressed. The code I added is highlighted red.


var playerSpeed: int = 10;
var bullet: Rigidbody;
function Update ()
{
              AmtToMove = playerSpeed * Input.GetAxisRaw ("Horizontal") * Time.deltaTime;

              transform.Translate (Vector3.right * AmtToMove);

              if (transform.position.x < -6)
{
                             transform.position.x = -6;
              }

              if (transform.position.x > 6)
              {
                             transform.position.x = 6;
              }

              if (Input.GetKeyDown ("space"))
              {
                             var tempBullet: Rigidbody;
                             tempBullet = Instantiate (bullet, transform.position, transform.rotation);
              }

I then had to attach a rigid body to my Bullet Prefab. This basically allows me to both control my player and fire bullets.

In the next post I will talk about creating an enemy for my player to fire at.

No comments:

Post a Comment