Kodeco Forums

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.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5460-how-to-make-a-game-like-jetpack-joyride-in-unity-2d-part-3

I’m running into a problem in the “Killing the Mouse” stage and beyond: my mouse won’t respond to the laser nor coins. It ignores them: both the running and flying animations work, but no dying or dying-related sprites occur.

My mouse has the same behaviour as your Hmm… it looks like you can no longer use the jetpack and the mouse doesn’t move forward step. Everything else works as expected, with no code errors. I’ve compared my code (all scripts) to the final code in both steps 2 and 3 with nothing looking different.

Is there a crucial button within the mouse Inspector (maybe in the Circle Collider 2D?) that I might have missed?

Great tutorial so far. Thanks.

Hi Danny,
Thanks for taking the time to work through this substantial tutorial. Let’s have a look at this together…

When you say your mouse ignores the coins and lasers that would suggest that something is not configured correctly with the colliders, or the OnTriggerEnter2D method.
However, you also mention behaviour similar to that just before the ‘Adding the Fall and Die Mouse Animations’… this would suggest the colliders are working, and it is the dying logic that is not working as expected.

I apologise for asking again, but to help me troubleshoot:

Does your mouse is pass through the coins and lasers happily and carry on as normal, ie not collect the coins, and not dropping to the ground or stopping after passing a laser?

Or does your mouse drop to the ground but not transition to the falling and dying animations?

Please let me know what you are seeing, but in the meantime, here are a few things that come to mind to help troubleshoot for now.

  1. Check that you are using the 2D versions of the OnTrigger methods. This is an easy mistake that you can often thank autocomplete for!! The method should be OnTriggerEnter2D and the parameter should be a collider2D. The traditional OnTriggerEnter will not be called using 2D colliders!

  2. Pop a print in your OnTriggerEnter2D to check it is being called.
    print(gameObject.name + " has collided with a " + collider.gameObject.name);
    Run your game, and keep an eye on the console: Are you getting print statements for collision with coins and lasers?

  3. Tags are case sensitive, check your coin tag is Coins to match the conditional:
    if (collider.gameObject.CompareTag(“Coins”)). in the OnTriggerEnter2D method

  4. When I have a hard to track down bug, keep an eye on whats happening in the editor windows. In this instance I would do two things:
    a) Open the Animator window and keep it docked somewhere you can see during testing.
    b) Temporarily change appropriate private variables to public so you can see them change during gameplay, in this case isDead and isGrounded would be useful to keep an eye on. Select the mouse in the Hierarchy so you can see the MouseController script in the inspector.
    Hit play… Are the variables updating accordingly to true when you are grounded or die? If so, are the Parameters also updating in the Animator? If so, are the states transitioning to the correct animations?

As soon as you get back to me on these points we can narrow down the problem, or perhaps you will spot it yourself!

Once again, thanks for working through the tutorial, I look forward to hearing back from you. :]

Hey Mark,

Appreciate the in-depth reply. First off:

Does your mouse is pass through the coins and lasers happily and carry on as normal, ie not collect the coins, and not dropping to the ground or stopping after passing a laser?

That’s correct. The mouse ignores the coins and lasers and carries on running/flying. It does not drop to the ground. It’s as if the coins and lasers aren’t in the scene.

Thanks for the troubleshooting methods. They didn’t work (more below), but helped me get a little more familiar with the code.

  1. I’m using the right OnTrigger methods in the right case and spelling. Just to be sure though: in your reply you said collider2D. My code fails unless I spell it out as onTriggerEnter2D(Collider2D collider) as per your tutorial instructions.

  2. The print I popped into OnTriggerEnter2D had no output in the console. I also tried one in HitByLaser with no luck.

  3. All good with tag spelling and case there, too.

  4. Great future debugging tip, thanks. In this case though the run and fly animator ‘patches’ and now-public isGrounded variable update as expected. The Any State animator ‘patches’ and isDead variable do not update.

So my beginner-brain is going to keep thinking about where I went wrong with the mouse not reading or responding to objects. If it’s useful, I’ve uploaded a copy of where I’m at here.

Thanks again,
Danny

Hi Danny,

Thank you very much for sending on your project and clarifying the issue.

The lack of collision pointed to a problem with either your colliders (which look perfect) or the OnTriggerEnter2D method… bingo!

I found an extremely easy to make typo… In fact, I think I’ve made it myself in the past also!

Your OnTriggerEnter2D needs the capital O at the start of the method name, you currently have onTriggerEnter2D.

These sorts of bugs are a pain to track down as, as you said, you don’t get an error from them! You have done a great job so far, I hope you enjoy the rest of the tutorial and feel free to message again if you run into any further difficulties.

Have fun!
Mark

1 Like

Pretty disappointed in myself for that one. But thankful for your eagle eyes & explanation! Cheers, Mark.

Dug in a little further after your fix if it’s of interest to anyone else:

Search the Unity docs showed me that OnTriggerEnter2D is a function pre-made by Unity. So (I’m assuming) that’s why the exact spelling mattered. HitByLaser and CollectCoin are specific to our game and made up by us. They work even when spelled differently.