Eye of the Nile Docs
Everything you need to know to get started!
Loading...
Searching...
No Matches
PlayerMovement Class Reference

Detailed Description

Handles the player movement, such as walking and jumping, when the user presses the corresponding keys.

Documentation updated 10/8/2024

Author
Stephen Nuttall, Roy Pascual, Alexander Art
Todo

Implement features that make movement more smooth.

Add animation trigger for when the playing is falling (don't know if it should be its own animation or keep the last frame of the jump animation).

Fix bug: Make coyote time jump availability reset immediately after a jump without groundDetector overriding it.

Public Member Functions

void Jump (float newJumpForce)
 Makes the player jump by canceling current velocity, adding upwards force, triggering the animation and sound effect, and setting isFalling to false. This jump allows for the amount of force applied to be specified.
 

Private Member Functions

void Awake ()
 Set references.
 
void Start ()
 Set linear drag in rigidbody to linearDrag, and jumpsAvailable to maxJumpChain.
 
void Update ()
 Determines how the player's movement should be processed this frame. See detailed view for steps.
 
void Jump ()
 Makes the player jump by canceling current velocity, adding upwards force, and setting isFalling to false.
 

Basic movement configuration

Information that can be configured in the Unity Editor to customize how the player moves.

Todo
This goes for nearly every script, but public variables like these that are only public so they can be edited in the Unity Editor should be changed to [SerializeField]. By replacing public with [SerializeField], the variable is private and cannot be edited arbitrarily by another script, but can still be viewed and changed in the Unity Editor. Example: under no circumstances do we expect the jumpForce to be changed by code. It makes more sense to use SpecialJump() or to add a new function that can properly handle whatever you're trying to do, rather than add potentially conflicting functionality in another script. What if you accidentally change jumpForce in the middle of a jump? This could create unexpected behavior that could break things. (If you're new to comp sci, you'll learn more about public and private variables and when to use/not use them in Computing III and IV).
float moveVelocity = 12.0f
 
float jumpForce = 10.0f
 The amount of force the player should jump with (how high does the player jump).
 
float linearDrag = 1.0f
 The amount of linear drag that should be applied to the rigidbody.
 
float maxJumpDuration = 0.2f
 How long the player is able to hold jump button before the jump stops increasing in power.
 
float glideSpeed = -3.5f
 
float coyoteTime = 0.5f
 
float airTime = 0.0f
 The amount of time since the player was last on the ground.
 
float jumpHeldDuration = 0.0f
 The amount of time the player has held the vertical input during a jump. Value is 0 if the player is not jumping.
 
bool coyoteJumpAvailable = false
 Used to prevent multiple coyote jumps before the player returns to the ground.
 
bool OnWarp = false [get, set]
 

Double Jumping

Information related to jump chains (double jumping, tripple jumping, etc) and their functionality.

int maxJumpChain = 1
 Maximum amount of times the player can jump without touching the ground.
 
int jumpsAvailable
 Amount of jumps still available in the jump chain before the player must touch the ground again to jump.
 

Reference Variables

These variables are references to other components on the player this script needs to communicate with.

Animator animator
 Reference to the player's animator component.
 
PlayerHealth objectHealth
 References to the player's PlayerHealth component.
 
Rigidbody2D rb
 
GroundDetector groundDetector
 Reference to the player's GroundDetector - a small trigger zone beneath the player's feet that detects if the player is on the ground.
 

Sound Effect References

These variables are references to the sounds that should be played when certain movements or actions occur.

EventReference jumpSFX
 

Member Function Documentation

◆ Awake()

void PlayerMovement.Awake ( )
private

Set references.

◆ Jump() [1/2]

void PlayerMovement.Jump ( )
private

Makes the player jump by canceling current velocity, adding upwards force, and setting isFalling to false.

◆ Jump() [2/2]

void PlayerMovement.Jump ( float newJumpForce)

Makes the player jump by canceling current velocity, adding upwards force, triggering the animation and sound effect, and setting isFalling to false. This jump allows for the amount of force applied to be specified.

Parameters
newJumpForceAmount of force to apply to the jump.

◆ Start()

void PlayerMovement.Start ( )
private

Set linear drag in rigidbody to linearDrag, and jumpsAvailable to maxJumpChain.

◆ Update()

void PlayerMovement.Update ( )
private

Determines how the player's movement should be processed this frame. See detailed view for steps.

Steps:

  • Get horizontal and vertical input.
  • If the player is not currently warping (using a door or going through an exit) update their velocity to match the horizontal input.
  • Flip the player to face the direction they are moving in.
  • Check if the player is falling.
  • Disable the option for coyote time jumps if the player is moving up.
  • If the conditions are met, let the player jump.
    • Conditions to initialize a jump:
      • Vertical input is positive (the user is pressing either, W or the up arrow) or the user is pressing the space bar. This also gets reffered to as "the jump button."
      • The player is not currently warping (using a door or going through an exit).
      • One of these three scenarios are true:
        1. The player is on the ground (on ground scenario).
        2. The player has recently been on the ground and has not already used a coyote time jump since last grounded (coyote time scenario).
        3. There is at least one jump available for a jump chain (double jump scenario).
    • What do to when a jump is initialized:
      • Count the number of double jumps the player has left.
      • Force the player upwards and update isFalling (by using the Jump() function).
      • Play jumping animation and sound.
      • Count how long the jump has been held.
  • The jump button can be held to make a jump last slightly longer and go slightly higher.
    • Conditions to sustain a jump:
      • A jump must have already been initialized (see above).
      • The jump must not have already been sustained for too long.
    • When the jump button is held:
      • Continue to force the player upwards and update isFalling.
      • Continue to count how long the jump button has been held.

Member Data Documentation

◆ airTime

float PlayerMovement.airTime = 0.0f

The amount of time since the player was last on the ground.

◆ animator

Animator PlayerMovement.animator

Reference to the player's animator component.

◆ coyoteJumpAvailable

bool PlayerMovement.coyoteJumpAvailable = false
private

Used to prevent multiple coyote jumps before the player returns to the ground.

◆ coyoteTime

float PlayerMovement.coyoteTime = 0.5f

If the player walks off an edge, coyote time is the amount of time after leaving the ground that they can still jump. This prevents jumps from having to be frame perfect. The user gets a little bit of leniency with the timing of their jumps.

◆ glideSpeed

float PlayerMovement.glideSpeed = -3.5f

The maximum velocity that the player can fall when vertical input is positive (gliding). More negative = faster fall.

◆ groundDetector

GroundDetector PlayerMovement.groundDetector
private

Reference to the player's GroundDetector - a small trigger zone beneath the player's feet that detects if the player is on the ground.

◆ jumpForce

float PlayerMovement.jumpForce = 10.0f

The amount of force the player should jump with (how high does the player jump).

◆ jumpHeldDuration

float PlayerMovement.jumpHeldDuration = 0.0f
private

The amount of time the player has held the vertical input during a jump. Value is 0 if the player is not jumping.

◆ jumpsAvailable

int PlayerMovement.jumpsAvailable
private

Amount of jumps still available in the jump chain before the player must touch the ground again to jump.

◆ jumpSFX

EventReference PlayerMovement.jumpSFX
private

◆ linearDrag

float PlayerMovement.linearDrag = 1.0f

The amount of linear drag that should be applied to the rigidbody.

◆ maxJumpChain

int PlayerMovement.maxJumpChain = 1

Maximum amount of times the player can jump without touching the ground.

Note
0 would be a normal jump, 1 allows for double jumping, 2 for triple jumping, etc.

◆ maxJumpDuration

float PlayerMovement.maxJumpDuration = 0.2f

How long the player is able to hold jump button before the jump stops increasing in power.

◆ moveVelocity

float PlayerMovement.moveVelocity = 12.0f

The speed the player should walk at (how fast does the player walk).

◆ objectHealth

PlayerHealth PlayerMovement.objectHealth

References to the player's PlayerHealth component.

◆ rb

Rigidbody2D PlayerMovement.rb
private

Reference to the player's rigidbody component.

Property Documentation

◆ OnWarp

bool PlayerMovement.OnWarp = false
getset

True if the player is standing over a warpable door. Currently used to prevent the player from jumping when trying to use a door. Use WarpInfo.CurrentlyWarping to check if the player is currently warping to another area through a StageWarp, such as a door or some other exit.

Todo
Make this an event rather than a public bool. This is the more "proper" way to do this as it prevents OnWarp from being changed arbitrarily.

The documentation for this class was generated from the following file: