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
Share
You are currently viewing page 3 of 7 of this article. Click here to view the first page.

Using a Trigger to Make the Mouse Die Once

During play mode, click on the Animator view after the mouse dies and you will see the die animation is being played on repeat. Oh, the brutality!

This happens because you transition from Any State to die repeatedly, forever. The grounded and dead parameters are always true, which triggers the animator to transition from Any State.

To fix this, you can use a special parameter type called a Trigger. Trigger parameters are very similar to Bools, with the exception that they are automatically reset after use.

Open the Animator view and add a new Trigger parameter called dieOnceTrigger. Set its state to On, by selecting the radio button next to it.

Next, select the transition from Any State to die, and add dieOnceTrigger in the Conditions section.

Next, open the Animations folder in the RW directory in the Project view and select die. In the Inspector, uncheck Loop Time. This will stop the animation from looping.

Run the scene and collide with a laser.

This time the mouse looks far more peaceful!

Adding Coins

While death dealing lasers are fun to implement, how about adding some coins for the mouse to collect?

Creating a Coin Prefab

Creating a coin Prefab is similar to creating the laser, so you should try doing this yourself. Just use the coin sprite and follow these tips:

  • Don’t create any scripts for the coin.
  • Use Circle Collider 2D instead of Box Collider 2D.
  • Enable Is Trigger option for the collider, since you don’t want the coin to stop the mouse movement.

If you have any questions on how to do this, take a look at the expandable section below.

[spoiler title="Creating a coin prefab"]

  1. Open the Sprites folder in the Project view.
  2. Drag a coin sprite into the scene and select it in the Hierarchy.
  3. In the Inspector set its Sorting Layer to Objects.
  4. Add a CircleCollider2D component.
  5. Enable the Is Trigger property of the collider.
  6. Set the Radius of the collider to 0.23.

Here is an image showing all of the steps:

After creating a coin GameObject, drag it from the Hierarchy and into the Prefabs folder in the Project view to create a coin Prefab.

[/spoiler]

Now add several coins to the scene by dragging coin Prefabs to the Scene view. Create something like this:

Run the scene. Grab those coins!

Wow, the poor mouse has been having a really tough time in Part 3 of this tutorial! Why can’t you collect a coin? The mouse dies because the code in MouseController script currently treats any collision as a collision with a laser.

Using Tags to Distinguish Coins From Lasers

To distinguish coins from lasers, you will use Tags, which are made exactly for situations like this.

Select the coin Prefab in the Prefabs folder in the Project view. This will open the Prefab properties in the Inspector. Find the Tag dropdown right below the name field, click to expand it, and choose Add Tag....

This will open the already familiar Tags & Layers editor in the Inspector. In the Tags section add a tag named Coins.

Now select the coin Prefab in the Project view once again, and set its Tag to Coins in the Inspector.

Of course just setting the Tag property doesn’t make the script distinguish coins from lasers. You’ll still need to modify some code.

Updating MouseController Script to Use Tags

Open the MouseController script and add a coins counter variable:

private uint coins = 0;

This is where you’ll store the coin count.

Then add the CollectCoin method:

void CollectCoin(Collider2D coinCollider)
{
    coins++;
    Destroy(coinCollider.gameObject);
}

This method increases the coin count and removes the coin from the scene so that you don't collide with it a second time.

Finally, make the following changes in the OnTriggerEnter2D method:


if (collider.gameObject.CompareTag("Coins"))
{
    CollectCoin(collider);
}
else
{
    HitByLaser(collider);
}

With this change, you call CollectCoin in the case of a collision with a coin, and HitByLaser in all other cases.

Note: In this game there are only two types of objects so it is okay to use the else case for lasers. In a real game, you should assign tags for all object types and check them explicitly.

Run the scene.

That’s much better! The mouse collects coins and dies if it hits a laser. It looks like you’re ready to generate lasers and coins using a script.

Generating Coins and Lasers

Generating coins and lasers is similar to what you did when you generated rooms. The algorithm is almost identical. However, you currently have a Prefab that consists of only one coin. If you write generation code now, you would either generate only one coin here and there on the level, or you'd have to manually create groups of coins programmatically.

How about creating different configurations of coins and generating a pack of coins at once?

Creating a Pack of Coins Prefab

Open the Prefabs folder in the Project viewer and drag 9 coins into the scene using the coin Prefab. It should look something like this:

Select any coin and set its Position to (0, 0, 0). This will be the central coin. You will add all coins into an Empty GameObject, so you need to build your figure around the origin.

After placing the central coin, build a face down triangle shaped figure around the coin. Don’t forget that you can use Vertex Snapping by holding the V key.

Now create an Empty GameObject by choosing GameObject ▸ Create Empty. Select it in the Hierarchy and rename it to coins_v.

Set its Position to (0, 0, 0) so that it has the same position as the central coin. After that, select all coins in the Hierarchy and add them to coins_v. You should get something like this in the Hierarchy:

Select coins_v in the Hierarchy and drag it to Prefabs folder in the Project view to create a new coin formation Prefab.

Note: You can create as many different coins combinations as you want. Just as with rooms, the generator script will provide a property where you will specify all the possible objects to generate.

You're done. Remove all the coins and lasers from the scene since they will be generated by the script.

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.