How to Make a Game Like Jetpack Joyride in Unity 2D – Part 2

In the second part of a three part series, you will be generating a series of endless rooms, allowing the user to fly through them. By Mark Placzek.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 5 of 5 of this article. Click here to view the first page.

Checking if the Mouse is Grounded

To make the mouse automatically switch states, you will have to update the MouseController script to check if the mouse is currently grounded, then let the Animator know about it.

Open the MouseController script and add the following instance variables:

public Transform groundCheckTransform;
private bool isGrounded;
public LayerMask groundCheckLayerMask;
private Animator mouseAnimator;

The groundCheckTransform variable will store a reference to the groundCheck Empty GameObject that you created earlier. The isGrounded variable denotes if the mouse is grounded, while the groundCheckLayerMask stores a LayerMask that defines what is the ground. Finally, the mouseAnimator variable contains a reference to the Animator component.

Note: It is better to cache components you get by GetComponent in an instance variable, since GetComponent is slower.

To cache the Animator component add the following line of code to Start:

mouseAnimator = GetComponent<Animator>();

Now add UpdateGroundedStatus:

void UpdateGroundedStatus()
{
    //1
    isGrounded = Physics2D.OverlapCircle(groundCheckTransform.position, 0.1f, groundCheckLayerMask);
    //2
    mouseAnimator.SetBool("isGrounded", isGrounded);
}

This method checks if the mouse is grounded and sets the Animator parameter as follows:

  1. To check if the mouse GameObject is grounded, you create a circle of radius 0.1 at the position of the groundCheck object you added to the scene. If this circle overlaps any object that has a Layer specified in groundCheckLayerMask then the mouse is grounded.
  2. This code actually sets the isGrounded parameter of the Animator which then triggers the animation.

Finally, add a call to UpdateGroundedStatus at the end of the FixedUpdate method:

UpdateGroundedStatus();

This calls the method with each fixed update, ensuring that the ground status is consistently checked.

Setting the MouseController Script Parameters for Ground Check

There is only one small step left to make the mouse automatically switch between flying and running. Open Unity and select the mouse GameObject in the Hierarchy.

Search for the Mouse Controller script component. You will see two new parameters exposed in the Inspector:

Click the Ground Check Layer Mask dropdown and select the Ground layer. Drag the groundCheck from the Hierarchy to the Ground Check Transform property.

Run the scene.

Enabling and Disabling the Jetpack Flames

Although you cured the mouse of laziness, you haven’t cured its wastefulness. The jetpack is still firing, even when the mouse is on the ground. Think of the carbon emissions, people!

Fortunately, you only need to add a few tweaks in the code to fix this.

Open the MouseController script and add the following jetpack variable to store a reference to the particle system.

public ParticleSystem jetpack;

Then add the following AdjustJetpack method:

void AdjustJetpack(bool jetpackActive)
{
    var jetpackEmission = jetpack.emission;
    jetpackEmission.enabled = !isGrounded;
    if (jetpackActive)
    {
        jetpackEmission.rateOverTime = 300.0f;
    }
    else
    {
        jetpackEmission.rateOverTime = 75.0f;
    }
}

This method disables the jetpack’s emission when the mouse is grounded. It also decreases the emission rate when the mouse is falling down, since the jetpack might still be active, but not at full strength.

Add a call to this method to the end of FixedUpdate:

AdjustJetpack(jetpackActive);

As a reminder, the jetpackActive variable is true when you the left mouse button is depressed and false when released.

Now switch back to Unity and drag the mouse's jetpackFlames from the Hierarchy to the Jetpack property of the MouseController component.

Run the scene.

Now the jetpack has three different states: It’s disabled when the mouse is grounded, full strength when going up, and runs at a decreased emission rate when the mouse is going down. Things are looking pretty good!

Where to Go From Here?

Enjoying the tutorial series so far? You can download the final project for this part using the materials link at the top or bottom of this tutorial.

The final part of this series adds all the “fun” stuff: lasers, coins, sound effects, and much more!

If you want to know more about Prefabs, the Unity documentation is a good place to start.

If you have any comments, questions or issues, please post them below!

Mark Placzek

Contributors

Mark Placzek

Author

Toby Flint

Tech Editor

Chris Belanger

Editor

Sean Stewart

Illustrator

Sean Duffy

Final Pass Editor

Over 300 content creators. Join our team.