How to Make a 2D Space Shooter in Unity - Part 2

We are going to continue the space shooter game that we started in the Part 1 of this tutorial. This time we will add enemies and handle collisions.

Create Enemies

First let's start by creating enemies. This is going to be very similar to what we did to create the bullets.

Drag the enemy from the Textures folder into the hierarchy tab, add a rigidbody component, and check the "is kinematic" option (we don't want the enemy to be affected by gravity).

Next, add a script component to the enemy called "enemyScript" with this code.

// Public variable that contains the speed of the enemy
public var speed : int = -5;
// Function called when the enemy is created
function Start () {
// Get the rigidbody component
var r2d = GetComponent("Rigidbody2D");
// Add a vertical speed to the enemy
r2d.velocity.y = speed;
// Make the enemy rotate on itself
r2d.angularVelocity = Random.Range(-200, 200);
}
// Function called when the object goes out of the screen
function OnBecameInvisible() {
// Destroy the enemy
Destroy(gameObject);
}

Now that the enemy is done we can make it into a prefab. Just drag the enemy into the Prefabs folder and delete the enemy from the hierarchy.

Spawn Enemies

We want to randomly spawn enemies on the top of the screen. Let's create a new object from where they will appear.

Drag the spawn object from the Textures folder into the hierarchy tab and position it just above the background. To do so make sure that you have the "move" tool selected (it's in the top left corner).

Next, add a new script to the spawn object called "spawnScript" with this code.

// Variable to store the enemy prefab
public var enemy : GameObject;
// Variable to know how fast we should create new enemies
public var spawnTime : float = 2;
function Start() {
// Call the 'addEnemy' function in 0 second
// Then every 'spawnTime' seconds
InvokeRepeating("addEnemy", 0, spawnTime);
}
// New function to spawn an enemy
function addEnemy() {
// Get the renderer component of the spawn object
var rd = GetComponent("Renderer");
// Position of the left edge of the spawn object
// It's: (position of the center) minus (half the width)
var x1 = transform.position.x - rd.bounds.size.x/2;
// Same for the right edge
var x2 = transform.position.x + rd.bounds.size.x/2;
// Randomly pick a point within the spawn object
var spawnPoint = Vector2(Random.Range(x1, x2), transform.position.y);
// Create an enemy at the 'spawnPoint' position
Instantiate(enemy, spawnPoint, Quaternion.identity);
}

This code basically creates a new enemy every spawnTime seconds somewhere on this white line.

Don't forget to set the public variable by dragging the enemy prefab on the "enemy" input.

Now you can test the game by pressing the play button.

Enemies are spawning correctly but we don't have any collisions yet.

Handle Collisions

The spaceship, the enemies, and the bullets should all collide with each other. For this to work, each of these objects needs to have a new component called "box collider". So do "Add Component -> Physics 2D -> Box Collider 2D" for all of them.

Then select the enemy prefab and check the "is trigger" option in the box collider component. This is telling Unity to call the function OnTriggerEnter2D each time the enemy collides with something.

Now add this new function at the end of the enemyScript.

// Function called when the enemy collides with another object
function OnTriggerEnter2D(obj) {
// Name of the object that collided with the enemy
var name = obj.gameObject.name;
// If the enemy collided with a bullet
if (name == "bullet(Clone)") {
// Destroy itself (the enemy) and the bullet
Destroy(gameObject);
Destroy(obj.gameObject);
}
// If the enemy collided with the spaceship
if (name == "spaceship") {
// Destroy itself (the enemy) to keep things simple
Destroy(gameObject);
}
}

And everything should work as expected.

Conclusion

With just a few lines of code we managed to create a simple game that can be played on any platform. I hope that you saw how powerful Unity is.

The Part 2 of this tutorial is over. I may write a Part 3 if I get a lot of people interested, so make sure to spread the word about this tutorial :-)

Get the complete source code of this space shooter game and be notified when I write new tutorials

More than 12,000 people already joined. No spam and unsubscribe at any time.