Learning Unity 2D

by re-creating Mega Man

GitHub commit.

Note a few things going on in the gif above:

  • When respawning, lives decrement.
  • Wave remains the same, however.
  • Health goes back to 100%.

And while I didn’t make a gif, there is detection for when you die and have no lives left, we get a nice blinking “Game Over” screen. I’ll enhance that later to go back to the title after a period of time (simple coroutine).

The trickiest part of respawning was re-enabling and resetting the state of so many different things. As a result, there is an event fired in the stage when a respawn is about to happen, and there are FIVE listeners to that event:

  1. PlayerStateController – Re-enables movement and weaponry.
  2. DamageController – Re-enabled the SpriteRenderer and Collider2D.
    • I don’t like this place for it. I considered making a separate component strictly for dealing with the animation/sprites of Mega Man, and may still go that route if I can make a clean separation.
  3. PlayerStats – Resets the current health.
  4. HealthController – Resets the current health in the UI.
  5. LivesDisplay – Updates the lives count in the UI.

So many things happening. It feels a bit like spaghetti, but maybe it’s decent design: lots of separate components tracking solely what they need to track. A respawn event causes a lot of resetting, so many it’s OK there are so many listeners. I even had to keep track of the initial spawning sprite and position just so it consistently happens in the same spot.

As for the logic to restart the stage, most of that was just stopping the existing stage coroutine, and starting a fresh one. I did re-organize how the encounters are processed slightly so we can keep track and support respawning on the same wave, but it’s not a big change.

There is a bug I need to track down though. On the first death, you can’t just keep moving Rush around. But, on subsequent deaths, you can keep moving him around until you respawn. I think I prefer being able to move around Rush while waiting to respawn, so I’m going to see if I can figure out what’s the difference and adjust.

Other Notes

I also did a couple other things:

Up Next

A lot of the logic in the Cut Man stage script is stuff I’d want to make common amongst all stages, so I’ll probably refactor into a parent class other stages can extend from.

I’ll also add some more waves with my two new enemies (2 Eyes and Clam).

And, finally… a boss?? Maybe?!

Posted in

Leave a comment