How to Make a Game Like Jetpack Joyride in Unity 2D – Part 3
- 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!
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!
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.
- 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.
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.
- 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
toggleIntervalso 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
rotationSpeedvariable 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
Add the following code to the
timeUntilNextToggle = toggleInterval;
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
timeUntilNextToggle -= Time.deltaTime;
if (timeUntilNextToggle <= 0)
isLaserOn = !isLaserOn;
laserCollider.enabled = isLaserOn;
laserRenderer.sprite = laserOnSprite;
laserRenderer.sprite = laserOffSprite;
timeUntilNextToggle = toggleInterval;
transform.RotateAround(transform.position, Vector3.forward, rotationSpeed * Time.deltaTime);
Here is what this code does:
- Decreases the time left until next toggle.
timeUntilNextToggleis 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
timeUntilNextTogglevariable since the laser has just been toggled.
- Rotates the laser around the z-axis using its
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.