How to Make a Game Like Jetpack Joyride in Unity 2D – Part 1
Learn how to create a game like Jetpack Joyride in Unity 2D in this three part tutorial series. By Mark Placzek.
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
How to Make a Game Like Jetpack Joyride in Unity 2D – Part 1
35 mins
- Getting Started
- Creating and Configuring the Project
- Configuring the Game View
- Adding the Player Character
- Importing Game Assets
- Adding the Player to the Scene
- Slicing The Mouse Sprite Sheet
- Creating a Script to Control the Jetpack
- Adjusting the Gravity
- Adding the Floor and the Ceiling
- Using a Particle System to Create Jetpack Flames
- Creating a Particle System
- Improving the Flames
- Creating a Level Section
- Adding the Room Background
- Using Vertex Snapping
- Using Sorting Layers
- Decorating the Room
- Where to Go From Here?
Adding the Player Character
In this section of the tutorial you will add the player character: a cool mouse with a jetpack. Just when you thought you had seen it all!
Unpack the materials you downloaded for this tutorial and locate the two directories Sprites and Audio. You will not use the audio files until a future part of this tutorial. Just keep them handy for the time being.
Importing Game Assets
To add the assets, open the RocketMouse_Resources folder, select both the Sprites and Audio folders, and drag them onto the Assets folder in the Project view.
You’ve just added all required assets. At this point, it might seem that there are many strange files in there. Don’t worry, most of the images are just decorations and backgrounds. Apart from that, there is a sprite sheet for the mouse character, the laser and the coin objects.
Adding the Player to the Scene
Many animated game sprites are supplied in a sprite sheet, and our heroic mouse is no exception.
Slicing The Mouse Sprite Sheet
Frames of the running, flying and dying animation are contained within the mouse_sprite_sheet. Your first step is to slice it correctly.
Open the Sprites folder in the Project view and find mouse_sprite_sheet. Select it and set its Sprite Mode to Multiple in the Inspector, and then click Apply.
Then click the Sprite Editor button to open the Sprite Editor.
In the Sprite Editor click the Slice button near the left top corner to open the slicing options.
- Set the Type field to Grid By Cell Size.
- Set the Pixel Size to 162 x 156.
- Click the Slice button.
- You will see a faint grid immediately appear. Don’t forget to click the Apply button to save changes.
Still within the Sprite Editor, select the top left image to display its details. Click in the Name field and give the sprite a more appropriate name: mouse_run_0.
Rename the remaining sprites from top-left to bottom-right as follows:
- mouse_run_1
- mouse_run_2
- mouse_run_3
- mouse_fly
- mouse_fall
- mouse_die_0
- mouse_die_1
Click Apply again to save changes.
Close the Sprite Editor. Expand mouse_sprite_sheet in the Project view, and you will see that it was sliced into eight different sprites. Nice!
It is time to actually add something to the scene. Select the sprite named mouse_fly and drag it into the Scene view.
Doing this will create an object in the Hierarchy named mouse_fly (just as was the image used to create it).
Select mouse_fly in the Hierarchy and make the following changes in the Inspector:
- Change name to mouse, since this will better describe the player character.
- Set its Position to (0, 0, 0). You will move the mouse to its final position a bit later, but right now it is better to place it right at the screen center, so that you can see it better.
- Add a Circle Collider 2D component, by clicking Add Component in the Inspector. In the drop down menu select Physics 2D and then choose Circle Collider 2D.
- Set the Radius of the Circle Collider 2D component to 0.5.
- Add a Rigidbody 2D component by clicking Add Component and selecting Physics 2D\Rigidbody 2D.
- Expand the constraints section within the Rigidbody 2D component. Enable the Freeze Rotation Z checkbox.
Here is an image demonstrating all the steps:
The green circle in the Scene view shows the collider; its size changed when you changed the Radius property of the Circle Collider 2D component.
Colliders define a shape that are used by the physics engine to determine collisions with other objects. You could have created a more pixel-perfect collider by using a Polygon Collider 2D component, as in the screenshot below:
However, using complex colliders makes it harder for the physics engine to detect collisions, which in turn creates a performance hit. A good rule is to always use simple colliders whenever possible. As you will see, a circle collider works really well for this game. The only adjustment was the radius of the collider so that it matched the original mouse image.
While colliders define the shape of the object, the Rigidbody is what puts your game object under the control of the physics engine. Without a Rigidbody component, the GameObject is not affected by gravity. Thus, you cannot apply things such as force and torque to the GameObject.
In fact, you wouldn’t even detect collisions between two GameObjects, even if both had Collider components. One of the objects must have a Rigidbody component.
However, while you want the mouse to be affected by gravity and collide with other objects, you don’t want its rotation to be changed. Fortunately, this is easy to solve by enabling the Freeze Rotation property of the Rigidbody 2D component.
Run the scene and watch as the mouse falls down, affected by the gravity force.
But wait! Why did the mouse fall down at all? You didn’t add any gravity to the Rigidbody… or did you? In fact, when you added the Rigidbody 2D component, it was given a default Gravity Scale of 1. This tells the system to make the character fall using the default gravity of the physics engine.
Creating a Script to Control the Jetpack
You won’t let that mouse fall down into the abyss. Not on your watch!
You need to add a script that will enable the jetpack and apply force to the mouse object to move it up and keep it from falling.
To add a script to the mouse object:
- In the Project browser, create a new folder within the RW directory called Scripts, just as you created the Scenes folder before. Make sure that this folder is selected, since Unity will add a new script to the currently selected folder.
- Select Create ▸ C# Script in the top menu and name the script MouseController.
- Drag the MouseController script over mouse in the Hierarchy to add it to the mouse GameObject. This will add a Script component and set its Script property to MouseController script.
Can't add script behaviour MouseController. The script's file name does not match the name of the class defined in the script!
This happens because Unity creates the following class inside the script, and renaming the script doesn’t change the class name inside:
public class NewBehaviourScript
It’s time to write some code. Open the MouseController script by double-clicking it either in the Project view or in the Inspector. This will open MouseController.cs in the editor of your choice.
Add the following jetpackForce
variable just inside the class definition:
public float jetpackForce = 75.0f;
This will be the force applied to the mouse when the jetpack is on.
Just below jetpackForce
, add the following variable:
private Rigidbody2D playerRigidbody;
Next add the following code to the automatically generated Start
method:
playerRigidbody = GetComponent<Rigidbody2D>();
When the game starts, you retain a reference to the player’s Rigidbody. You will need to access this component very frequently in this script, and you don’t want to create a performance hit every time you need to locate it.
Next, add the following method inside the class:
void FixedUpdate()
{
bool jetpackActive = Input.GetButton("Fire1");
if (jetpackActive)
{
playerRigidbody.AddForce(new Vector2(0, jetpackForce));
}
}
FixedUpdate()
is called by Unity at a fixed time interval. All physics-related code is written in this method.
Update
and the FixedUpdate
is that FixedUpdate
is called at a fixed rate, while Update
is simply called for every rendered frame. Since frame rate can vary, the time between subsequent Update
method calls can also vary and physics engines do not work well with variable time steps. This is why FixedUpdate
exists and should be used to write the code related to the physics simulation (e.g. applying force, setting velocity and so on).
In FixedUpdate
, you check if the Fire1 button is currently pressed. In Unity, Fire1 by default is defined as a left mouse button click, the left Control key on a keyboard, or a simple screen tap in the case of an iOS app. For this game, you want the jetpack to engage when the user touches the screen. Therefore, if Fire1 is currently pressed, the code will add a force to the mouse.
AddForce
simply applies the force to the rigidbody. It takes a Vector2
that defines the direction and the magnitude of the force to apply. You will move your hero mouse forward later, so right now you only apply the force to move the mouse up with the magnitude of jetpackForce
.
Run the scene and hold your left mouse button to enable the jetpack and make the mouse move up.