VG1, Quest 10 - Adventure, PT2

Download Files

Supporting files for VG1 quests are part of a single archive that you can download here.

Import Assets

This assignment will continue with the Adventure scene.

Import zelda_world.png from the course files.

Use the following import settings:

Hit Apply whenever prompted.

Open Sprite Editor and use the following Slice settings:

Hit Slice and Apply whenever prompted.

Destructible Environment

Create a new game object called Breakable and attach a Sprite Renderer and a BoxCollider2D. The Sprite component’s Order in Layer should be -1 and should use zelda world sprite #31. Create a Breakable.cs script and attach it to the object.

Prefab this game object and duplicate at least 4 of them to place around the player.

Open Breakable.cs and implement a public Break function that destroys the obstacle.

Facing Direction

In order for our player to attack, we need to keep track of what direction they are facing. An enumeration is a nice way for defining your own data type to keep track of such information. In PlayerController.cs, create an enumeration for Direction.

In the same file, define a variable for keeping track of the player’s facing direction. We will also need to access the SpriteRenderer soon and will configure a list of sprites that represent each facing direction.

Fill the SpriteRenderer reference during the start event.

The animation state will drive the logic for what direction we are facing. By checking the currently rendering player sprite, we can determine facing direction. The flow looks something like this:

A) Controller Input → B) Physics Forces → C) Animation Blending → D) Visible Sprite → E) Facing Direction

To match the Visible Sprite (D) with a Facing Direction (E), the sprites variable on PlayerController should list the sprites that represent the various directions. It is very important that the array indexes match the enumerations. For example, "zelda1_10" must be the sprite for Left because they are both in the position for Item #2 which is the Direction enumeration for Left.

If you used different sprites than what the instructions portrayed in the prior assignment, you will need to configure this variable to match.

Because animations happen after the Update loop, we will put our direction logic in LateUpdate which happens after all animations have resolved. In this code, we loop through each of the configured sprites to see which one is being rendered. When a match is found, its integer index is casted to the matching Direction enumeration. This is why it was important for us to match all of those integers.

Playtest your game and inspect the PlayerController to check if Facing Direction updates appropriately for all 8 idle/walk scenarios. For example, when you walk Down, the facingDirection variable should also report "Down".

Attacks

Now that we know what direction the player is facing, we can define the 4 attack zones where the player will deal damage. Create an empty child object within Player and name it "AttackZones". Make sure it's Position XYZ are set to 0.

Next create four empty children game objects within AttackZones and name them Up, Down, Left, and Right. To help with positioning, change the game object icon on these four children to the red dot. The red dots will help you visualize their positions in the next step.

These positions will determine where the player can do damage. Arrange them around the player like the following diagram as their names suggest (up is above, down is below, etc).

PlayerController needs to keep track of these four attack points.

Fill in the blanks for the attack zones in the inspector. Just like the sprites configuration, the element order in this array MUST MATCH the sequence defined by your enumeration numbers.

Whenever the player attacks, we will use Physics OverlapCircle function to check for targets under our various attack zones. For example, if the player attacks and we are facing up, we will check within a circle at attack zone 0 which represents up.

When OverlapCircleAll returns results, we loop through all of them and process each accordingly depending on what components it has. If an object is breakable, we will break it.

Playtest your game and ensure you can break targets from all 4 directions. Also check for false positives. If you are facing left, the target to your right should not break.

Save and Test

Playtest to ensure all interactions work as expected and that the addition of any new features hasn’t broken any earlier interactions.

Submit Assignment

SAVE any open files or scenes.

Submit your assignment for grading following the instructions supplied for your particular classroom.