How to Make a Game Like Jetpack Joyride in Unity 2D – Part 3
In the final part of this series, you will generate some coins and lasers, construct a scrolling background, all to some fancy beats. 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
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 3
55 mins
- Getting Started
- Adding Lasers
- Creating a Laser
- Turning the Laser On and Off From the Script
- Setting the Laser Script Parameters
- Killing the Mouse
- Adding the Fall and Die Mouse Animations
- Transitioning to Fall and Die Animations
- Using a Trigger to Make the Mouse Die Once
- Adding Coins
- Creating a Coin Prefab
- Using Tags to Distinguish Coins From Lasers
- Updating MouseController Script to Use Tags
- Generating Coins and Lasers
- Creating a Pack of Coins Prefab
- Adding New Parameters to GeneratorScript
- Adding the Method to Add a New Object
- Generating and Removing Objects When Required
- Setting up Script Parameters
- Adding GUI Elements
- Displaying Coin Count
- Raising the Dead
- Adding Sound and Music
- Hitting Laser Sound
- Collecting Coin Sound
- Jetpack and Footsteps Sounds
- Adding Audio Sources
- Switching Between Footsteps and Jetpack Sounds
- Setting Footstep and Jetpack Script Variables
- Adding Music
- Adding a Parallax Background
- Preparing the Background Images
- Creating Another Camera
- Creating Quads
- Setting Quad Textures
- Making Textures Move
- Fixing the Order of the Cameras
- Where to Go From Here?
This is the final part of our three-part tutorial series on how to create a game like Jetpack Joyride in Unity 2D. If you’ve missed the previous parts, you should head back and complete Part 1 and Part 2 first.
In this part you will add lasers, coins, sound effects, music and even parallax scrolling. So enough talking, let’s get to the fun!
Getting Started
You can continue on with this tutorial using the project you created in the second part. Alternatively, you can download the RocketMouse Part 2 Final project in the materials at the top or bottom of this tutorial.
Open the RocketMouse.unity scene and get going!
Adding Lasers
The mouse flying through the room is great, but to make things interesting you’ll add some obstacles. What can be cooler than lasers?
Lasers will be generated randomly in a similar manner to the room generation, so you need to create a Prefab. You will need to create a small script to control the laser also.
Creating a Laser
Here are the steps required to create a laser object:
This is convenient for many reasons. For example, if the mouse dies on top of the laser, it won’t hang in the air, lying there on the laser. Also, the mouse would likely still move forward a bit after hitting the laser instead of bouncing back due to inertia. Besides that, real lasers are not a hard, physical object, so enabling this property simulates the real laser.
- In the Project view, find the laser_on sprite in the Sprites folder and drag it into the scene. Since the laser Prefab will consist of only the laser itself you don’t have to position it at the origin.
- Select laser_on in the Hierarchy and rename it to laser.
- Set the Sprite Renderer Sorting Layer to Objects.
- Add a Box Collider 2D component.
- Enable the Is Trigger property in the Box Collider 2D component.
Note: When the Is Trigger property is enabled, the collider will trigger collision events, but will be ignored by the physics engine. In other words, if the mouse touches the laser you will get notified. However, the laser will not block the mouse movement.
This is convenient for many reasons. For example, if the mouse dies on top of the laser, it won’t hang in the air, lying there on the laser. Also, the mouse would likely still move forward a bit after hitting the laser instead of bouncing back due to inertia. Besides that, real lasers are not a hard, physical object, so enabling this property simulates the real laser.
- Set the Size of the collider: X to 0.18 and Y to 3.1. This creates a collider only where the laser is, leaving the emitters on both ends absolutely safe.
- Create a new C# Script named LaserScript and attach it to laser.
This is convenient for many reasons. For example, if the mouse dies on top of the laser, it won’t hang in the air, lying there on the laser. Also, the mouse would likely still move forward a bit after hitting the laser instead of bouncing back due to inertia. Besides that, real lasers are not a hard, physical object, so enabling this property simulates the real laser.
Here is the full list of steps displayed:
Turning the Laser On and Off From the Script
Open the LaserScript and add the following instance variables:
//1
public Sprite laserOnSprite;
public Sprite laserOffSprite;
//2
public float toggleInterval = 0.5f;
public float rotationSpeed = 0.0f;
//3
private bool isLaserOn = true;
private float timeUntilNextToggle;
//4
private Collider2D laserCollider;
private SpriteRenderer laserRenderer;
It might seem like a lot of variables, but in fact everything is quite trivial.
- The Laser has two states: On and Off, and there is a separate image for each state. You will specify each state image in just a moment.
- These properties allow you to add a bit of random fluctuation. You can set a different
toggleInterval
so that all lasers on the level don’t work exactly the same. By setting a low interval, you create a laser that will turn on and off quickly, and by setting a high interval you will create a laser that will stay in one state for some time. TherotationSpeed
variable serves a similar purpose and specifies the speed of the laser rotation. - These two private variables are used to toggle the laser’s state.
- These two private variables hold references to the laser collider and renderer so that their properties can be adjusted.
Here is an example of different laser configurations, each with different toggleInterval
and rotationSpeed
.
Add the following code to the Start
method:
//1
timeUntilNextToggle = toggleInterval;
//2
laserCollider = gameObject.GetComponent<Collider2D>();
laserRenderer = gameObject.GetComponent<SpriteRenderer>();
- This will set the time until the laser should toggle its state for the first time.
- Here we save references to the collider and renderer as you will need to adjust their properties during their lifetime.
To toggle and rotate the laser add the following to the Update
method:
//1
timeUntilNextToggle -= Time.deltaTime;
//2
if (timeUntilNextToggle <= 0)
{
//3
isLaserOn = !isLaserOn;
//4
laserCollider.enabled = isLaserOn;
//5
if (isLaserOn)
{
laserRenderer.sprite = laserOnSprite;
}
else
{
laserRenderer.sprite = laserOffSprite;
}
//6
timeUntilNextToggle = toggleInterval;
}
//7
transform.RotateAround(transform.position, Vector3.forward, rotationSpeed * Time.deltaTime);
Here is what this code does:
- Decreases the time left until next toggle.
- If
timeUntilNextToggle
is equal to or less then zero, it is time to toggle the laser state. - Sets the correct state of the laser in the private variable.
- The laser collider is enabled only when the laser is on. This means that mouse can fly through the laser freely if it is off.
- Set the correct laser sprite depending on the laser state. This will display the laser_on sprite when the laser is on, and the laser_off sprite when the laser is Off.
- Resets the
timeUntilNextToggle
variable since the laser has just been toggled. - Rotates the laser around the z-axis using its
rotationSpeed
.
rotationSpeed
to zero.