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.

Leave a rating/review
Download materials
Save for later

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.

  1. 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.
  2. Select laser_on in the Hierarchy and rename it to laser.
  3. Set the Sprite Renderer Sorting Layer to Objects.
  4. Add a Box Collider 2D component.
  5. 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.
  6. 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.
  7. Create a new C# Script named LaserScript and attach it to laser.
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.

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:

public Sprite laserOnSprite;
public Sprite laserOffSprite;
public float toggleInterval = 0.5f;
public float rotationSpeed = 0.0f;
private bool isLaserOn = true;
private float timeUntilNextToggle;
private Collider2D laserCollider;
private SpriteRenderer laserRenderer;

It might seem like a lot of variables, but in fact everything is quite trivial.

  1. 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.
  2. 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. The rotationSpeed variable serves a similar purpose and specifies the speed of the laser rotation.
  3. These two private variables are used to toggle the laser’s state.
  4. 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:

timeUntilNextToggle = toggleInterval;
laserCollider = gameObject.GetComponent<Collider2D>();
laserRenderer = gameObject.GetComponent<SpriteRenderer>();
  1. This will set the time until the laser should toggle its state for the first time.
  2. 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:

timeUntilNextToggle -= Time.deltaTime; 
if (timeUntilNextToggle <= 0)
    isLaserOn = !isLaserOn;
    laserCollider.enabled = isLaserOn;
    if (isLaserOn)
        laserRenderer.sprite = laserOnSprite;
        laserRenderer.sprite = laserOffSprite;
    timeUntilNextToggle = toggleInterval;
transform.RotateAround(transform.position, Vector3.forward, rotationSpeed * Time.deltaTime);

Here is what this code does:

  1. Decreases the time left until next toggle.
  2. If timeUntilNextToggle is equal to or less then zero, it is time to toggle the laser state.
  3. Sets the correct state of the laser in the private variable.
  4. 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.
  5. 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.
  6. Resets the timeUntilNextToggle variable since the laser has just been toggled.
  7. Rotates the laser around the z-axis using its rotationSpeed.
Note: To disable rotation, you can simply set rotationSpeed to zero.

Setting the Laser Script Parameters

Switch back to Unity and select the laser in the Hierarchy. Make sure the Laser Script component is visible.

Drag the laser_on sprite from the Project view to the Laser On Sprite property of the Laser Script component in the Inspector.

Then drag the laser_off sprite to the Laser Off Sprite property.

Set Rotation Speed to 30.

Now set the laser Position to (2, 0.25, 0) to test that everything works correctly. Run the scene, and you should see the laser rotating nicely.

Now, turn the laser into a prefab. You should be able to do this on your own by now, but check the hints below if you need help.

[spoiler title="Need help creating a laser prefab?"]
Easy: drag the laser into the Prefabs folder in the Project view.


Mark Placzek


Mark Placzek


Toby Flint

Tech Editor

Chris Belanger


Sean Stewart


Sean Duffy

Final Pass Editor

Over 300 content creators. Join our team.