~/book - Developing 2D Games with Unity

Jared Halpern

In 2018, I embarked on a 7.5 month project to write a programming book. 408 pages later, I had written a book on Developing 2D Games with Unity. My book is available through Amazon.com and the publishers website, Apress.com. It’s currently being used as a course textbook at Davidson College for a course on game development which is pretty neat.

I’ve continued to blog about developing games with Unity on this site, as well as Medium.

Book Summary

Follow a walkthrough of the Unity Engine and learn important 2D-centric lessons in scripting, working with image assets, animations, cameras, collision detection, and state management. In addition to the fundamentals, you’ll learn best practices, helpful game-architectural patterns, and how to customize Unity to suit your needs, all in the context of building a working 2D game. While many books focus on 3D game creation with Unity, the easiest market for an independent developer to thrive in is 2D games. 2D games are generally cheaper to produce, more feasible for small teams, and more likely to be completed. If you live and breathe games and want to create them then 2D games are a great place to start.

By focusing exclusively on 2D games and Unity’s ever-expanding 2D workflow, this book gives aspiring independent game developers the tools they need to thrive. Various real-world examples of independent games are used to teach fundamental concepts of developing 2D games in Unity, using the very latest tools in Unity’s updated 2D workflow.

New all-digital channels for distribution, such as Nintendo eShop, XBox Live Marketplace, the Playstation Store, the App Store, Google Play, itch.io, Steam, and GOG.com have made it easier than ever to discover, buy, and sell games. The golden age of independent gaming is upon us, and there has never been a better time to get creative, roll up your sleeves, and build that game you’ve always dreamed about. Developing 2D Games with Unity can show you the way.

What You’ll Learn

Who This Book Is For

Hobbyists with some knowledge of programming, as well as seasoned programmers interested in learning to make games independent of a major studio.




Errata

Chapter 3, Page 53

Credit: @omundy

After stopping the playing scene, add the animation for the player’s idle state. From the Sprites folder, select the last two sprites from the Player32x32 sprite-sheet, titled: Player32x32_12 and Player32x32_13 and drag them onto the PlayerObject. When prompted by the Create New Animation save window, name the created animation-clip, “player-idle” and save to the Animations -> Animations folder.

Chapter 4

Credit: @josenerydev

Issue with format of file: OutdoorObjects.png. (fixed in GitHub repo)

Chapter 4, Page 93

In the Creating Tile Palettes section, the Tile Palette window in Unity 2018.3 is now located under Window -> 2D -> Tile Palette.

Chapter 4, Page 128

Credit: @omundy

Make sure to attach the RoundCameraPos script to the virtual camera object. Drag and drop the RoundCameraPos onto “CM vcam1”.

Chapter 4, Page 137

Credit: @omundy

The interface has been updated in Unity 2018.3. The menu item “Edit Physics Shape” has been renamed to “Custom Physics Shape”. After selecting Custom Physics Shape, click the “Generate” button on the right before editing.

Chapter 5, Page 147

Credit: @omundy

Create the GameObject in the hierarchy view (not project view).

Chapter 5, Page 162

Credit: @omundy

In the Assembling Our Item section, third paragraph, rename the Scriptable Object, “Coin” (not “Item”).

Chapter 5, Page 164

Credit: @omundy

In the Player Collisions section, the first line of code section // 3, should be:

print("Hit: " + hitObject.objectName);

Chapter 5, Page 166

Credit: @omundy

Steps 10 and 11 are duplicates of step 3 and can be disregarded.

Chapter 6, Page 198

Credit: @omundy

Under the heading Create the HealthBar Script, right-click in the MonoBehaviours folder and create a new C# script called HealthBar.”

Chapter 6, Page 202-204

Credit: @omundy

Ensure a copy of the HealthBarObject is in the Hierarchy view and select it. Add the HealthBar script this HealthBarObject. When working with the HealthBar in these next 3 pages (202-204), work with the copy of HealthBarObject in the Hierarchy view, not the copy in the Prefab folder. Make sure to apply changes when you’re done, so the changes propagate to the Prefab.

Note: The location of the button to apply changes to a prefab has changed in Unity 2018. The button is now located in the top right of the Inspector, in a drop-down menu called Overrides.

Chapter 6, Page 204

Credit: @omundy

The property, Character Category which appears in the source and in Figure 6-22 may be disregarded, or used later on in your game to broadly distinguish between what is/isn’t an enemy, or the player (or even an NPC) instead of creating a tag for each character type.

Chapter 6, Page 207

Credit: @omundy

With InventoryObject selected, in the Canvas Scaler component, set Reference Pixels Per Unit to 32.

Chapter 7, Page 263

Credit: @omundy

In the The DamageCharacter() method section, the code should be added to the Enemy class.

Chapter 7, Page 269

Credit: @omundy

In the Refactoring Prefab Instantiation section, remove all four lines from Start() and add them to the new ResetCharacter() override.

After you add the ResetCharacter() method to the Player class, add the following method

private void OnEnable() {
	ResetCharacter();
}

Chapter 7, Page 275

Credit: @omundy

You’ll notice when you run into the enemies they spin. Like you did with the PlayerObject, check Freeze Rotation for the Z axis inside the Rigidbody2D on the EnemyObject prefab.

Chapter 8, Page 287

Credit: @omundy

In the Enemy Walk Animation section, select the Enemy prefab then open the Animator window as seen in Figure 8-3.

Chapter 8, Page 357

Credit: @VADS

In the implementation of the GetQuadrant() method, disregard the first two variable declarations: Vector2 mousePosition = Input.mousePosition; and Vector2 playerPosition = transform.position; as they are not used.