Platformer

A short clip of the completed assignment in action. You must login to blackboard/panopto for the embedded videos to show!

Content Videos

These videos cover the high-level concepts for this week's topic (the things covered in class on Thu kickoff).

Start Here

Grab the repo first! There is starter project/code in there.

https://classroom.github.com/a/u8XC69Xv

Out of the box, it should run as below.

The initial code doesn't do much, but has several things set up for you.

Editor configuration

  • Entities: camera, player character, ground, and a platform
  • Player sprite sheet (megaman), already sliced into frames
  • BoxCollider2D component on the player entity
  • PlatformPlayer script on the player entity

Code (in PlatformPlayer)

  • Public variables to link the STANDING and JUMPING sprite frames
  • Screen and player boundaries (extents)
  • Horizontal movement w/ constant velocity using AD or the left/right arrows
  • Setting the STANDING sprite in code and flipping its direction based on movement direction

Stage 1: Gravity Falls

In this stage, you will implement the below funtionality:

  • Falling under the influence of gravity
  • Stopping when you hit any platform including the ground platforms (grounded)
  • Only allow player controlled movement only when grounded
  • Using the JUMPING sprite image when not grounded

Limitations of this Stage

  • While the player stops when they hit the platform/ground, they still "sink" into it slightly
  • The player can walk through the platform
  • Horizontal movement is abrupt (no smoothing)

Free Falling

To get started, just try adding gravity. Integrate a constant downward acceleration vector every frame to add more and more downward velocity to the player entity. Recall the formula is just like the formula to integrate velocity to update position.

Hint

Here is a video showing an extra example of acceleration due to gravity.

It's the Landing that Gets You

Here is the complete algorithm (with the existing code in it) to implement this Stage. See below for more implementation details.

Ground Check

To use raycast, you need to first set up the Ground/Platform entities

  • Add BoxCollder2D components to the Ground and Platform entities in the scene
  • Be sure to check the Is Trigger box
  • Add a new layer called "Platform" as layer 6
  • Set it as the layer for the Ground and Platform entities

Once those steps are complete, you can put in the code to do the raycast and note whether the Player character is grounded or not.

Raycast down from the player to find the closest Platform/Ground

  • Raycast documentation here: https://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html
  • Use Layer 6 as your layerMask
  • Cast straight down from the character feet (use SELF_EXTENTS to calculate where the bottom of the sprite is)
  • Check the resulting RaycastHit2D object (doc here: https://docs.unity3d.com/ScriptReference/RaycastHit2D.html)
    • If collider is null, no hit
    • Otherwise, see if distance is very small (like 0.001f), meaning it's right there under you

Using the results of the ground check raycast you can complete the algorithm given above.

Hint

Below is a video showing an extra raycasting example with code.

Stage 2: Jump and Slide

In this stage, you will implement the below funtionality:

  • Smooth acceleration-based player movement
  • Jumping

Limitations

  • The raycast works to land on any platform on the Platform layer with a trigger collider
  • However, the player can still walk through the platform and jumps halfway up through it
  • This is because the raycast ground check doesn't prevent overlaps (collision response), it only stops falling

Smooth Moves

To implement acceleration-based player movement, replace the line that sets constant velocity based on user input (in the grounded branch) with this algorithm:

Jump Around

To implement jumping, add this algorithm to the end of the grounded branch:

Stage 3: Platforms

In this stage, you will implement the below funtionality:

  • Collision response to prevent overlaps with the platforms
    • Not sinking into them or walking/jumping through them

You and I...Collide

Unity's collision system efficiently checks for overlaps in the colliders on all entities that are hooked into the system. To enable collision checks for our Player entity, add a Rigidbody2D component and set the Body Type to Kinematic.

Any time Unity detects overlap between the Player entity BoxCollider2D and any trigger collider on another entity, it will call the method OnTriggerStay2D on all components that implement it on the Player entity.

Add the following method to your PlatformPlayer script to respond to collisions by moving the Player entity out of overlap.

Finishing the Lab

Add more platforms and test out the code.

In the above collision algorithm, it notes that shorter overlap distance is used as an approximation for collision response. This approximation creates an edge case where it can fail to identify whether you hit the side or the top of the platform. If you jump at the corners enough, you can trigger it, and this would be unacceptable to your players.

It is not required, but I encourage you to look closely at the algorithm and consider how to remove that approximation and be precise.