Skip to main content

Rewriting old player movement code

tsumea

Organisation from Australia, New South Wales

In my first and previous post in this game dev log entry, I had written that I wanted to do a game which was a collection of simple retro games. Unity released a new major release (2019.3) while I was putting the initial project together, and I was curious to see if one of my prototypes from 4 years ago could easily be upgraded from it. Well, it couldn't, not easily. It was a 2D platformer that implemented some useful but old scripts that were now causing errors because of deprecated code. Some of those scripts had also since been made obsolete since the latest Unity update has those features available in a package (although some is still in beta) and it was worth just replacing them with Unity's solution.

I had an old pixel perfect camera script and X-UniTMX (a TMX importer) for bringing in tilemaps from Tiled (a free cross platform tile map editor) to Unity, and they both had to be removed and re-implemented with Unity's own pixel perfect camera and tile editor. I wanted to check out Unity's new universal input manager as well.

Another thing I wanted to fix was some of my old code, starting with the player movement. Since it was an early project of mine, I hadn't discovered some useful concepts like finite state machines or design patterns just yet, and so my character movement code got pretty complicated and difficult to read, particularly since the character had a lot of states (running, jumping, climbing, crawling, falling, on a ledge etc). I think most people start scripting movement by making a player run, then adding if statements to check for a jump button press etc, and keep adding if-or-else statements for more movement types, but if you're adding a whole bunch of them like I did, over time it becomes a confusing mess of if-statement checks. I found this page was really useful as a starting point on doing it the right way.

So I've rewritten the player's movement, and re-implemented it with the animation system I had set up with Unity, and it's working great. The new system is good in that if I wanted to add a bunch of other player states in the future (swimming, flying, wall climbing, whatever), it's easy. After getting my platformer prototype to work again, I thought I'd just do this instead of the retro multi-arcade game I had written about previously for my dev log.

I'm currently working on the graphics, so I'll leave that to the next devlog entry (I should have a playable prototype for download via itch.io as well).