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.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Unity Tutorial: How to Make a Game Like Space Invaders
40 mins
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
andTempo
are set to their default values. -
IncreasePitch
increments the the source audio's pitch by an amount dictated bypitchChange
, which in turn is the ratio ofmaxPitch
andpitchChangeSteps
.maxPitch
also puts an upper limit on the pitch. - After changing the pitch, you can calculate the tempo based on the following formula:
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.
Try shooting down the invaders. You'll notice they start moving faster.
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 setminY
to the Y position of the cannon andcurrentY
to the Y position ofspawnStartPoint
. - Whenever called,
ChangeDirection
decrementscurrentY
until it becomes less thanminY
, 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.
Save and Play. Now, you'll see that Game Over panel is triggered if the swarm goes 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!
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.