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?
Setting Footstep and Jetpack Script Variables
Switch back to Unity and select the mouse GameObject in the Hierachy. You’re going to work only within the Inspector window. Collapse all components except Mouse Controller.
Now drag the top Audio Source component to Footsteps Audio in the Mouse Controller script component.
After that, drag the second Audio Source component to the Jetpack Audio in the Mouse Controller script component.
Run the scene. Now you should hear the footsteps when the mouse is running on the floor and jetpack engine when it’s flying. Also the jetpack sound should become stronger when you enable the jetpack by holding the left mouse button.
Adding Music
To add music, follow these simple steps:
- Select the Main Camera in the Hierarchy.
- Add an Audio Source component in the Inspector.
- Drag the music asset from the Project view to the Audio Clip property.
- Make sure Play On Awake and Loop are enabled.
- Finally, decrease the Volume to 0.3, since the music is quite loud compared to the other sounds.
That’s it. Run the scene and enjoy some music!
Adding a Parallax Background
Currently this room with a view is pretty... well, blue.
However, there are two ways to solve it:
- Create something to show behind the window.
- Don’t use windows!
Of course you’ll go with the first option. But instead of adding a motionless background image, you will add a parallax background.
You will add two Quads, one for the background and one for the foreground parallax layer.
You will set a texture for each quad, and instead of moving quads to simulate movement, you will simply move the textures within the quad at a different speed for the background and the foreground layer.
You will set a texture for each quad, and instead of moving quads to simulate movement, you will simply move the textures within the quad at a different speed for the background and the foreground layer.
Preparing the Background Images
To use background images with quads, you need to adjust how they are imported to Unity.
Open the Sprites folder in the Project view and select window_background. In the Inspector change its Texture Type to Default instead of Sprite (2D and UI). After that change Wrap Mode to Repeat and click Apply.
Do the same for the window_foreground image.
Creating Another Camera
Wait, what, another camera? The Main Camera is reserved for following the mouse through the level. This new camera will render the parallax background and won't move.
Create a new camera by selecting GameObject ▸ Camera. Select it in the Hierarchy and make the following changes in the Inspector:
- Rename it to ParallaxCamera.
- Set the Position to (0, 10, 0).
- Set the Projection to Orthographic.
- Set the Size to 3.2, the same size as the Main Camera.
Since you have two cameras, you also have two audio listeners in the scene. Disable the Audio Listener in ParallaxCamera or you will get the following warning:
There are 2 audio listeners in the scene. Please ensure there is always exactly one audio listener in the scene.
Creating Quads
Create two Quad objects by choosing GameObject ▸ 3D Object ▸ Quad. Name the first quad parallaxBackground and the second parallaxForeground. Drag both quads to ParallaxCamera to add them as children.
Select parallaxBackground and change its Position to (0, 0.7, 10) and Scale to (11.36, 4.92, 1).
Note: You use this scale to accommodate the background image's size of 1136 × 492 px without distortion.
Note: You use this scale to accommodate the background image's size of 1136 × 492 px without distortion.
Select parallaxForeground and set its Position to (0, 0.7, 9) and Scale to (11.36, 4.92, 1).
Setting Quad Textures
Open the Sprites folder in the Project view. Drag the window_background over to parallaxBackground and window_foreground over parallaxForeground in the Hierarchy.
Then select parallaxForeground in the Hierarchy. You will see that a Mesh Renderer component was added. Click on the Shader drop down and select Unlit ▸ Transparent.
Do the same for parallaxBackground.
This is what you should see in the Scene view right now.
If you disable 2D mode and rotate the scene a little, you can see how all the scene components are positioned and layered.
Run the scene. You will see that the background is in front of the main level. This is useful so you can see how the textures move with ParallaxScrolling. Once you have the textures moving, you will move it to the background.
Making Textures Move
You will not move the Quads. Instead, you're going to move the textures of the quads by changing the texture offset. Since you set the Wrap Mode to Repeat the texture will repeat itself.
Create a new C# Script called ParallaxScroll and attach it to ParallaxCamera.
Open the ParallaxScroll script and add the following instance variables:
//1
public Renderer background;
public Renderer foreground;
//2
public float backgroundSpeed = 0.02f;
public float foregroundSpeed = 0.06f;
//3
public float offset = 0.0f;
Here is a breakdown of what these variables will do:
- The
Renderer
variables will hold a reference to the Mesh Renderer component of each of the quads so that you can adjust their texture properties. - The
backgroundSpeed
andforegroundSpeed
just define the speed for each background. - The
offset
will be provided by the player’s position. This will enable you to couple the mouse’s movement to the movement of the parallax background. If your pick up a power up and boost forward, the background will move quickly. If the player dies, the movement stops.
Add the following code to the Update
method:
float backgroundOffset = offset * backgroundSpeed;
float foregroundOffset = offset * foregroundSpeed;
background.material.mainTextureOffset = new Vector2(backgroundOffset, 0);
foreground.material.mainTextureOffset = new Vector2(foregroundOffset, 0);
This code increases the texture offset of each of the quad’s textures with the offset
, thus moving it. The resulting speed is different since the script uses the backgroundSpeed
and foregroundSpeed
coefficients.
Switch back to Unity and select ParallaxCamera in the Hierarchy. Drag the parallaxBackground quad to the Background field of the ParallaxScroll script and parallaxForeground to Foreground.
Now open the MouseController script and add the following public variable:
public ParallaxScroll parallax;
Then add the following code to the end of the FixedUpdate
method:
parallax.offset = transform.position.x;
Switch back to Unity and select the mouse GameObject in the Hierarchy. Make sure the MouseController script is visible in the Inspector.
Drag ParallaxCamera from the Hierarchy to the Parallax field in the Inspector.
This will allow the MouseController script to change the offset variable of the ParallaxScroll script with respect to the mouse’s position.
Run the scene, and behold the beautiful parallax effect!
But what about the level itself? You can’t see it!