Doing the 2nd edition of Unity in Action mostly involves updating code listings for the latest version of Unity, but my editors and I also decided to add one new chapter. While the majority of feedback from readers has been (thankfully!) very positive, one thing we’ve frequently seen is people wishing there was more content about 2D games. Thus, we decided to add a chapter about developing 2D platform games.
My process for every chapter has been 1) first build the project, then 2) write about what I implemented. So I implemented the shell of a 2D platformer a couple weekends ago, and am now writing the chapter about it. Here is a WebGL build of that project:
(controls explained on that page; left/right move, Space jump)
The features I’m going over in this chapter are horizontal movement with keyboard controls, playing sprite animations, colliding with walls and platforms, jumping, one-way platforms, slopes, moving platforms, and camera control. Pretty much in that order too: I purposely put less critical content at the end that I can cut if the chapter is going long.
I was pleasantly surprised by how little code that took! In part that was because I discovered several newer features in Unity I didn’t know about (eg. there’s a built-in one-way platform component). I also was expecting to hit annoying roadblocks with the physics system, but nope everything just worked right off the bat how I expected. The only exception was sloped floors, where I had to turn off gravity while you’re on the floor so that you won’t slide down.
As you can see, the graphics are extremely primitive. Part of the chapter involves explaining Unity’s Mecanim animation system, but I just drew a simple animated stick figure to demonstrate with. Beyond that, everything is just white blocks. I was considering also doing a tile map in this demo, but decided that’d be too much for one chapter. I am still going to write a section about tile maps, but it’ll just be a sidebar and not part of the primary content.
Incidentally, when I uploaded that WebGL build for this post, I realized I should also do that for a couple other projects from my book, so that people can see things in action. So here is the primitive FPS made in Chapter 3 and the 3rd person demo made in Chapter 7/8 (number increasing for 2nd edition).