Platformer

Platformer

START

Before you start! Take a look at the existing project setup and code, try it out and make sure (1) it works and (2) you understand it. It's mostly bringing over things we've already done, but there are a few new things:

  • A sprite sheet with multiple images
  • Public variables to link images (STANDING and JUMPING)
  • Placeholder code to set the image and facing

As a quick exercise, update the input controls callback to set direction to a new Vector2 with only the horizontal component of the input. Since different controller types might send a range of values 0.0f - 1.0f, normalize the resulting vector.

1
Gravity Falls
  • 3.1.1 Fall under the influence of gravity (acceleration)
  • 3.1.2 Stop when you hit a platform or ground (raycast)
  • 3.1.3 Switch between the STANDING and JUMPING sprite images
  • 3.1.4 Only allow player controlled movement when grounded

In this stage, you will implement gravity and basic platform collision. Your character will fall under gravity, stop when hitting platforms, and only allow movement when grounded.

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. The formula to update velocity from acceleration is the same as the formula to update position from velocity.

It's the Landing that Gets You

Here is the complete algorithm (with the existing code in it) to implement this Stage. Ignore the "user input" line as we've upgraded that to the new input system callbacks. Note that we have two different cases to deal with - behavior in the air and behavior on the ground. Splitting entity behavior up into states like this, controlled by a condition, is a key pattern in game code.

Implementation details below.

Ground Check Setup

You'll need to implement ground checking using Unity's raycast system to detect when your character is touching a platform.

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

  • Add BoxCollider2D 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.

Save Point
SAVE POINT
commit your work
🪙 5
2
Jump and Slide
  • 3.2.1 Smooth acceleration-based player movement
  • 3.2.2 Jump, only allowed when grounded

In this stage, you will implement smooth player movement and jumping mechanics. The player will have acceleration-based movement instead of instant velocity changes.

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

Jumping applies vertical force to the entity, resulting in vertical acceleration. However, in almost all cases a simpler model called impulse is sufficient. Impulse means that we apply instantaneous velocity to the object. This is how we were doing movementt, what we did with collision, and how you do jumping, knockback, and any other sudden movement.

When the player hits the jump key, immediately add some JUMP_SPEED constant to the velocity.y component and the player will start moving upward. That will switch them to the non-grounded state and acceleration due to gravity will kick in to bring them back down in a nice, familiar hyperbolic arc. Be sure to leave horizonal velocity alone so that they don't stop on a dime and jump straight up.

The input system callback for jump is already in place. In the callback, set a boolean to let you know in the next FixedUpdate that you should make them jump. Make sure they can only jump when grounded and that you clear the jump boolean so that they don't keep jumping (or immediately jump back up when they land).

Save Point
SAVE POINT
commit your work
🪙 5
3
Platforms
  • 3.3.1 Collision response to prevent overlaps with the platforms
  • 3.3.2 Not sinking into them or walking/jumping through them

In this stage, you will implement full collision response to prevent overlaps with platforms. Your character will no longer sink into platforms or walk 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. This tells Unity to perform collision detection, but not collision response. We are still controlling what happens after the collision is detected.

Now add BoxCollider2D components to the Player and to the platform and ground prefabs.

With the rigidbody in place, Unity checks for overlap between the Player collider and any trigger collider on any other entity. If detected, it will call the method OnTriggerStay2D on all components that implement it on whichever entity in the collision has the rigidbody (which could be both). The argument to the callback is the collider component from the other entity, so that you know what you hit.

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

Finishing the Assignment

Add more platforms and test out the code. In the collision response algorithm above, 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.

Draw out the different cases of hitting the top vs. side of the platform and implement a better strategy to detect which part of the platform the player hit.

Save Point
SAVE POINT
commit your work