Skip to content

PlayerMovement Class

Dawnosaur edited this page Jul 26, 2023 · 2 revisions

Overview

The PlayerMovement class is responsible for handling the movement and physics interactions of the player character in the game. It allows the player to run, jump, dash, and slide based on user input.

Public Properties

  • Data: A reference to a Scriptable Object (PlayerData) that holds all the player's movement parameters.
  • RB: A reference to the Rigidbody2D component attached to the player GameObject.
  • AnimHandler: A reference to the PlayerAnimator script that handles player animations.

State Parameters

  • IsFacingRight: A boolean flag indicating whether the player is facing right or left.
  • IsJumping: A boolean flag indicating whether the player is currently jumping.
  • IsWallJumping: A boolean flag indicating whether the player is currently performing a wall jump.
  • IsDashing: A boolean flag indicating whether the player is currently dashing.
  • IsSliding: A boolean flag indicating whether the player is currently sliding on a wall.
  • LastOnGroundTime: A float representing the time since the player was last on the ground.
  • LastOnWallTime: A float representing the time since the player was last touching a wall.
  • LastOnWallRightTime: A float representing the time since the player was last touching a right-facing wall.
  • LastOnWallLeftTime: A float representing the time since the player was last touching a left-facing wall.
  • LastPressedJumpTime: A float representing the time since the player last pressed the jump button.
  • LastPressedDashTime: A float representing the time since the player last pressed the dash button.

Check Parameters

  • _groundCheckPoint: A transform representing the position to check for ground contact.
  • _groundCheckSize: A Vector2 representing the size of the ground check box.
  • _frontWallCheckPoint: A transform representing the position to check for contact with the front wall (facing the player).
  • _backWallCheckPoint: A transform representing the position to check for contact with the back wall (opposite direction of the player).
  • _wallCheckSize: A Vector2 representing the size of the wall check box.
  • _groundLayer: A LayerMask defining the ground layer(s) that the player can stand on.

Methods

  • Awake(): Initializes the RB and AnimHandler properties by getting the Rigidbody2D and PlayerAnimator components from the GameObject.
  • Start(): Sets the initial gravity scale and facing direction of the player.
  • Update(): Handles player input, updates timers, and checks for various conditions like jumping, dashing, and sliding.
  • FixedUpdate(): Handles the player's movement during the FixedUpdate phase.

Input Callbacks

  • OnJumpInput(): Called when the jump input is detected.
  • OnJumpUpInput(): Called when the jump input is released.
  • OnDashInput(): Called when the dash input is detected.

Movement Methods

  • Run(float lerpAmount): Handles player running movement based on user input.
  • Turn(): Flips the player character to face the opposite direction.
  • Jump(): Performs a regular jump by adding force to the Rigidbody2D component.
  • WallJump(int dir): Performs a wall jump by adding force to the Rigidbody2D component in the opposite direction of the wall.
  • Slide(): Handles the sliding movement of the player on a wall.

Check Methods

  • CheckDirectionToFace(bool isMovingRight): Checks the direction the player is moving and flips the character if necessary.
  • CanJump(): Returns true if the player can perform a regular jump.
  • CanWallJump(): Returns true if the player can perform a wall jump.
  • CanJumpCut(): Returns true if the player can cut their jump (release the jump button early to reduce jump height).
  • CanWallJumpCut(): Returns true if the player can cut their wall jump.
  • CanDash(): Returns true if the player can perform a dash.
  • CanSlide(): Returns true if the player can slide on a wall.

Editor Methods

  • OnDrawGizmosSelected(): Draws Gizmos in the Unity editor to visualize the ground and wall check points and sizes.

Public Helper Methods

  • SetGravityScale(float scale): Sets the gravity scale of the Rigidbody2D component.
  • Sleep(float duration): Temporarily freezes the game for the specified duration, adding a juiciness effect to the dash.