Tuesday, 10 January 2012

Project 2: Space Invaders Clone, Enemy

To create the enemy for our game the first thing we had to do was to create a sphere game object that would later on become our enemy, for the sphere to stand out I created a new material and gave it a bright red colour.
The first thing we had to do with the sphere was to move it off screen, sounds a bit odd I know! But it makes sense. We had to move it upwards along a straight Y Axis until it just went off screen, then I had to take a note of the Y position, then do the same but move it downwards along the Y axis. I have taken a note of both Y positions as they will be used in my script later on to make my Sphere (or enemy) scroll downwards.
Here's a quick view of my current game window;
(Please note, I have moved the sphere onto the game window just so you can see it)
I had to then create a new script that will be used for my enemy. To do this I had to create a new JavaScript and then enter this script, again I will vaguely explain what the code is doing in the double asterisk;

**The Enemy attack speed**
var enemySpeed: int = 5;

function Update ()
{
              **Enemy attack speed**
              AmtToMove = enemySpeed * Time.deltaTime;

              transform.Translate (Vector3.down * AmtToMove);

              **If the enemy has gone off screen**
              if (transform.position.y <= -4.5)
              {
                             **Position it at a random height between 7 and 9**
                             transform.position.y = Random.Range(7, 9);
                             **Position it at random X-Position anywhere across the game window**
                             transform.position.x = Random.Range (-6, 6);
              }
}

After applying this script to the enemy game object when played the enemy scrolls down the screen until vanishing then re-appearing at a random x point above the game window.
The next thing I had to do was to create a script for the player to destroy the enemy.
To do this I had to modify the bullet script so that when the bullet touches the enemy they both get destroyed, but really only the bullet object would be destroyed, the enemy would simply re-spawn at a random x position as previously stated.
Here is the bullet script with the newly added script highlighted in red;


var bulletSpeed: int = 10;

function Update ()
{
              amtToMove = bulletSpeed * Time.deltaTime;
              transform.Translate (Vector3.up *amtToMove);
    
              if( transform.position.y >= 7)
              {
                             Destroy (gameObject);
              }
}

function OnTriggerEnter (otherObject: Collider)

{
              if (otherObject.gameObject.tag == "enemy")
              {
                             otherObject.gameObject.transform.position.y = Random.Range (7, 9);
                             otherObject.gameObject.transform.position.x = Random.Range (-6, 6);
                             Destroy (gameObject);
              }
}


After testing the game the player can now shoot and destroy the enemy, woo!

Now that my game works I can the apply 3D Spaceship that I have created over the current player (cube).
My game window now looks like this;
(I also changed the colour of my background to black)

After this I added a GUI Score so that when you destroy an enemy you gain 10 points, the game window in play mode now looks like this;

This is the end of my space invaders clone, but I would like to come back to this project to maybe add more enemies and also create individual spaceships for them.

No comments:

Post a Comment