Unity Tutorial: How to Make a Game Like Space Invaders

In this Unity tutorial, you’ll learn how to make a classic 2D space shooting game similar to Space Invaders. By Najmm Shora.

4.9 (9) · 3 Reviews

Download materials
Save for later
Share
You are currently viewing page 5 of 5 of this article. Click here to view the first page.

Adding Simple Dynamic Audio

Go to MusicControl.cs. Open it inside your code editor. Add the following line at the top of the class:

private readonly float defaultTempo = 1.33f;

This line represents the music's default beats per second. You can calculate this value by considering the music has four beats and is three seconds long.

Now, paste the following above StopPlaying:

[SerializeField] 
internal int pitchChangeSteps = 5;

[SerializeField] 
private float maxPitch = 5.25f;

private float pitchChange;

internal float Tempo { get; private set; }

Then, add the following lines after the definition for StopPlaying:

internal void IncreasePitch()
{
    if (source.pitch == maxPitch) 
    {
        return;
    }

    source.pitch = Mathf.Clamp(source.pitch + pitchChange, 1, maxPitch);
    Tempo = Mathf.Pow(2, pitchChange) * Tempo;
}

private void Start()
{
    source.pitch = 1f;
    Tempo = defaultTempo;
    pitchChange = maxPitch / pitchChangeSteps;
}

Here's how the code works:

  • Inside Start, source.pitch and Tempo are set to their default values.
  • IncreasePitch increments the the source audio's pitch by an amount dictated by pitchChange, which in turn is the ratio of maxPitch and pitchChangeSteps. maxPitch also puts an upper limit on the pitch.
  • After changing the pitch, you can calculate the tempo based on the following formula:

pitchchange = -log(2)(tempo1/tempo2) where tempo1 denotes the tempo before the change and tempo2 the tempo after the change.

Now open the InvaderSwarm.cs script and add the following at the end of variable declarations:

[SerializeField] 
private MusicControl musicControl;

private int tempKillCount;

In IncreaseDeathCount, paste the following lines at the end:

tempKillCount++;
if (tempKillCount < invaders.Length / musicControl.pitchChangeSteps) 
{
    return;
}

musicControl.IncreasePitch();
tempKillCount = 0;

Now IncreaseDeathCount tracks the variable tempKillCount to check whether it exceeds invaders.Length / musicControl.pitchChangeSteps. If it does, it calls IncreasePitch and tempKillCount resets.

This means when the player eliminates almost invaders.Length / musicControl.pitchChangeSteps invaders, both audio pitch and tempo increase. The variable Tempo inside MusicControl keeps track of the updated tempo.

Finally, inside Update, replace xIncrement = speedFactor * Time.deltaTime; with:

xIncrement = speedFactor * musicControl.Tempo * Time.deltaTime;

This line ensures the xIncrement considers the music tempo, and the invaders move faster as the music gets faster as the player eliminates more and more invaders.

Save everything. Return to Unity and select Game Controller. Set the Invader Swarm's Music Control to Music.

Save and Play.

The Inspector showing the Invader Swarm component

Try shooting down the invaders. You'll notice they start moving faster.

The invaders getting shot, and the swarm moving at increasing speed.

There's still one minor issue: If you miss any invaders, they continue moving once they reach the bottom of the screen. It would be better to trigger Game Over if the swarm reaches the bottom.

To do this, open InvaderSwarm.cs and add the following at the end of variable declarations:

[SerializeField] 
private Transform cannonPosition;

private float minY;
private float currentY;

Then, paste the following lines at the beginning of Start:

currentY = spawnStartPoint.position.y;
minY = cannonPosition.position.y;

Then add the following at the end of ChangeDirection:

currentY -= ySpacing;
if (currentY < minY) 
{
    GameManager.Instance.TriggerGameOver();
}

Here's what this code does:

  • Inside Start, you set minY to the Y position of the cannon and currentY to the Y position of spawnStartPoint.
  • Whenever called, ChangeDirection decrements currentY until it becomes less than minY, at which point the game ends and shows Game Over.

Save everything. Return to Unity and select Game Controller. Set Cannon Position of Invader Swarm to the Transform of CANNON.

The Inspector window showing the Invader Swarm component

Save and Play. Now, you'll see that Game Over panel is triggered if the swarm goes below the cannon's position.

The Game Over panel showing after invaders reached below the cannon's position.

And you're done!

Where to Go From Here?

You can use the Download Materials button at the top and bottom of this tutorial to download both the starter and final projects.

You may have noticed you didn't get to add any torchkas. Try adding them as a challenge.

Everything you need is already there. You may need to examine the code inside TorchkasManager and the Torchka scripts. If you get stuck, take a look at the Final project for the solution. Good luck!

The full game, with Torchkas!

Thanks for taking the time to read this article. I hope you had fun and learned something new. Please feel free to join the forum below for any questions or comments.

Special thanks to opengameart user jlunesc for some of the CC-BY assets used in the project.