This repository accompanies Developing 2D Games with Unity: Independent Game Programming with C# by Jared Halpern (Apress, 2018).
Download the files as a zip using the green button, or clone the repository to your machine using Git.
Release v1.0 corresponds to the code in the published book, without corrections or updates.
If you find any typos, errors in code, or otherwise incorrect information, please feel free to file an Issue in GitHub. See the file Contributing.md for more information on how you can further contribute to this repository.
Credit: @RenatiusvanSander
As noted here, occasionally the Unity Editor will set the Z-coordinate on a newly created GameObject Transform to be negative. If this happens and the Z-coordinate is set to a negative number, you won't see it when the Scene plays. To fix this, change the Z-coordinate to be 0 on the GameObject Transform.
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.
Credit: @josenerydev
Issue with format of file: OutdoorObjects.png
. (fixed in GitHub repo)
In the Creating Tile Palettes section, the Tile Palette
window in Unity 2018.3 is now located under Window -> 2D -> Tile Palette
.
Credit: @omundy
Make sure to attach the RoundCameraPos
script to the virtual camera object. Drag and drop the RoundCameraPos
onto "CM vcam1".
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.
Credit: @omundy
Create the GameObject in the hierarchy view (not project view).
Credit: @mprogers
In the Build the Consumable Script section, third paragraph, note that there is a distinction between the behavior of Scriptable Objects when running a project in the Editor, versus when running in a deployed (to device) build: Scriptable Objects cannot be used to save changes to data in a deployed build. More on this subject can be read here.
Credit: @omundy
In the Assembling Our Item section, third paragraph, rename the Scriptable Object, "Coin" (not "Item").
Credit: @omundy
In the Player Collisions section, the first line of code section // 3
, should be:
print("Hit: " + hitObject.objectName);
Credit: @omundy
Steps 10 and 11 are duplicates of step 3 and can be disregarded.
Credit: @omundy
Under the heading Create the HealthBar Script, right-click in the MonoBehaviours folder and create a new C# script called HealthBar."
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.
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.
Credit: @omundy
With InventoryObject
selected, in the Canvas Scaler component, set Reference Pixels Per Unit
to 32.
Credit: @omundy
In the The DamageCharacter() method section, the code should be added to the Enemy
class.
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();
}
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.
Credit: @omundy
In the Enemy Walk Animation section, select the Enemy prefab then open the Animator window as seen in Figure 8-3.
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.
Credit: @rdimitriev
In the method UpdateState()
, the case for Quadrant.West:
should be changed from: quadrantVector = new Vector2(-1.0f, 1.0f);
to quadrantVector = new Vector2(-1.0f, 0.0f);