2023-04-27 12:18:47 -07:00
using System.Collections ;
2023-04-09 21:59:32 -07:00
using UnityEngine ;
2023-04-09 23:09:21 -07:00
using UnityEngine.InputSystem ;
2023-04-09 21:59:32 -07:00
2023-04-17 00:01:49 -07:00
public class PlayerMovement : MonoBehaviour
2023-04-09 21:59:32 -07:00
{
2023-04-27 12:18:47 -07:00
//Scriptable object which holds all the player's movement parameters. If you don't want to use it
//just paste in all the parameters, though you will need to manuly change all references in this script
2023-04-09 23:09:21 -07:00
2023-04-27 12:18:47 -07:00
//HOW TO: to add the scriptable object, right-click in the project window -> create -> Player Data
//Next, drag it into the slot in playerMovement on your player
2023-04-09 23:09:21 -07:00
2023-04-27 12:18:47 -07:00
public PlayerData Data ;
2023-04-17 00:01:49 -07:00
2023-04-27 12:18:47 -07:00
#region Variables
//Components
public Rigidbody2D RB { get ; private set ; }
2023-04-17 00:01:49 -07:00
2023-04-27 12:18:47 -07:00
//Variables control the various actions the player can perform at any time.
//These are fields which can are public allowing for other sctipts to read them
//but can only be privately written to.
public bool IsFacingRight { get ; private set ; }
public bool IsJumping { get ; private set ; }
public bool IsWallJumping { get ; private set ; }
public bool IsSliding { get ; private set ; }
2023-04-17 15:22:24 -07:00
2023-04-27 12:18:47 -07:00
//Timers (also all fields, could be private and a method returning a bool could be used)
public float LastOnGroundTime { get ; private set ; }
public float LastOnWallTime { get ; private set ; }
public float LastOnWallRightTime { get ; private set ; }
public float LastOnWallLeftTime { get ; private set ; }
2023-04-27 19:15:53 -07:00
2023-04-27 15:56:36 -07:00
private bool trumpet = false ;
public bool in_range = false ;
public GameObject enemy ;
2023-04-17 15:22:24 -07:00
2023-04-27 12:18:47 -07:00
//Jump
private bool _isJumpCut ;
private bool _isJumpFalling ;
2023-04-17 16:47:12 -07:00
2023-04-27 12:18:47 -07:00
//Wall Jump
private float _wallJumpStartTime ;
private int _lastWallJumpDir ;
private Vector2 _moveInput ;
public float LastPressedJumpTime { get ; private set ; }
2023-04-27 13:31:04 -07:00
Tutorial_GrapplingRope grapplingRope ;
2023-04-27 12:18:47 -07:00
//Set all of these up in the inspector
[Header("Checks")]
//Size of groundCheck depends on the size of your character generally you want them slightly small than width (for ground) and height (for the wall check)
[SerializeField] private Vector2 _groundCheckSize = new Vector2 ( 0.49f , 0.03f ) ;
[Space(5)]
[SerializeField] private Vector2 _wallCheckSize = new Vector2 ( 0.5f , 1f ) ;
[Header("Layers & Tags")]
[SerializeField] private LayerMask _groundLayer ;
#endregion
private void Awake ( )
2023-04-09 21:59:32 -07:00
{
2023-04-27 12:18:47 -07:00
RB = GetComponent < Rigidbody2D > ( ) ;
2023-04-27 13:31:04 -07:00
grapplingRope = this . gameObject . GetComponent < PlayerBehavior > ( ) . grapplingRope ;
2023-04-17 00:01:49 -07:00
}
2023-04-09 21:59:32 -07:00
2023-04-27 12:18:47 -07:00
private void Start ( )
2023-04-17 00:01:49 -07:00
{
2023-04-27 12:18:47 -07:00
SetGravityScale ( Data . gravityScale ) ;
IsFacingRight = true ;
2023-04-09 21:59:32 -07:00
}
2023-04-09 23:09:21 -07:00
void OnMove ( InputValue value )
{
2023-04-27 12:18:47 -07:00
this . _moveInput = value . Get < Vector2 > ( ) ;
}
void OnJump ( )
{
OnJumpInput ( ) ;
2023-04-09 23:09:21 -07:00
}
2023-04-27 12:18:47 -07:00
private void Update ( )
{
#region TIMERS
LastOnGroundTime - = Time . deltaTime ;
LastOnWallTime - = Time . deltaTime ;
LastOnWallRightTime - = Time . deltaTime ;
LastOnWallLeftTime - = Time . deltaTime ;
LastPressedJumpTime - = Time . deltaTime ;
#endregion
#region INPUT HANDLER
if ( _moveInput . x ! = 0 )
CheckDirectionToFace ( _moveInput . x > 0 ) ;
#endregion
2023-04-27 20:12:07 -07:00
if ( ! IsJumping ) {
print ( "not jumping" ) ;
} else {
print ( "jumping, " + RB . velocity . y ) ;
}
2023-04-27 12:18:47 -07:00
#region COLLISION CHECKS
if ( ! IsJumping )
{
//Ground Check
2023-04-27 19:47:39 -07:00
if ( IsGrounded ( ) ) //checks if set box overlaps with ground
2023-04-27 12:18:47 -07:00
{
LastOnGroundTime = Data . coyoteTime ; //if so sets the lastGrounded to coyoteTime
2023-04-27 19:15:53 -07:00
trumpet = true ;
2023-04-27 12:18:47 -07:00
}
//Right Wall Check
2023-04-27 12:54:31 -07:00
if ( ( ( Physics2D . OverlapBox ( this . transform . position , _wallCheckSize , 0 , _groundLayer ) & & IsFacingRight )
| | ( Physics2D . OverlapBox ( this . transform . position , _wallCheckSize , 0 , _groundLayer ) & & ! IsFacingRight ) ) & & ! IsWallJumping )
2023-04-27 12:18:47 -07:00
LastOnWallRightTime = Data . coyoteTime ;
//Right Wall Check
2023-04-27 12:54:31 -07:00
if ( ( ( Physics2D . OverlapBox ( this . transform . position , _wallCheckSize , 0 , _groundLayer ) & & ! IsFacingRight )
| | ( Physics2D . OverlapBox ( this . transform . position , _wallCheckSize , 0 , _groundLayer ) & & IsFacingRight ) ) & & ! IsWallJumping )
2023-04-27 12:18:47 -07:00
LastOnWallLeftTime = Data . coyoteTime ;
//Two checks needed for both left and right walls since whenever the play turns the wall checkPoints swap sides
LastOnWallTime = Mathf . Max ( LastOnWallLeftTime , LastOnWallRightTime ) ;
2023-04-27 20:12:07 -07:00
}
2023-04-27 12:18:47 -07:00
#endregion
#region JUMP CHECKS
2023-04-27 20:12:07 -07:00
if ( IsJumping & & RB . velocity . y < = 0 )
2023-04-27 12:18:47 -07:00
{
IsJumping = false ;
2023-04-27 20:12:07 -07:00
print ( "isJumping " + IsJumping ) ;
2023-04-27 12:18:47 -07:00
if ( ! IsWallJumping )
_isJumpFalling = true ;
}
if ( IsWallJumping & & Time . time - _wallJumpStartTime > Data . wallJumpTime )
{
IsWallJumping = false ;
2023-04-17 15:22:24 -07:00
}
2023-04-27 12:18:47 -07:00
if ( LastOnGroundTime > 0 & & ! IsJumping & & ! IsWallJumping )
{
_isJumpCut = false ;
if ( ! IsJumping )
_isJumpFalling = false ;
}
//Jump
if ( CanJump ( ) & & LastPressedJumpTime > 0 )
{
IsJumping = true ;
IsWallJumping = false ;
_isJumpCut = false ;
_isJumpFalling = false ;
Jump ( ) ;
2023-04-27 19:47:39 -07:00
if ( ! IsGrounded ( ) & & in_range & & trumpet ) {
Destroy ( enemy . gameObject ) ;
enemy = null ;
in_range = false ;
} else if ( ! IsGrounded ( ) & & ! in_range & & trumpet ) {
trumpet = false ;
}
2023-04-27 12:18:47 -07:00
}
//WALL JUMP
// else if (CanWallJump() && LastPressedJumpTime > 0)
// {
// IsWallJumping = true;
// IsJumping = false;
// _isJumpCut = false;
// _isJumpFalling = false;
// _wallJumpStartTime = Time.time;
// _lastWallJumpDir = (LastOnWallRightTime > 0) ? -1 : 1;
//
// WallJump(_lastWallJumpDir);
// }
#endregion
#region SLIDE CHECKS
if ( CanSlide ( ) & & ( ( LastOnWallLeftTime > 0 & & _moveInput . x < 0 ) | | ( LastOnWallRightTime > 0 & & _moveInput . x > 0 ) ) )
IsSliding = true ;
else
IsSliding = false ;
#endregion
#region GRAVITY
//Higher gravity if we've released the jump input or are falling
if ( IsSliding )
{
SetGravityScale ( 0 ) ;
}
else if ( RB . velocity . y < 0 & & _moveInput . y < 0 )
{
//Much higher gravity if holding down
SetGravityScale ( Data . gravityScale * Data . fastFallGravityMult ) ;
//Caps maximum fall speed, so when falling over large distances we don't accelerate to insanely high speeds
RB . velocity = new Vector2 ( RB . velocity . x , Mathf . Max ( RB . velocity . y , - Data . maxFastFallSpeed ) ) ;
}
else if ( _isJumpCut )
{
//Higher gravity if jump button released
SetGravityScale ( Data . gravityScale * Data . jumpCutGravityMult ) ;
RB . velocity = new Vector2 ( RB . velocity . x , Mathf . Max ( RB . velocity . y , - Data . maxFallSpeed ) ) ;
}
else if ( ( IsJumping | | IsWallJumping | | _isJumpFalling ) & & Mathf . Abs ( RB . velocity . y ) < Data . jumpHangTimeThreshold )
{
SetGravityScale ( Data . gravityScale * Data . jumpHangGravityMult ) ;
}
else if ( RB . velocity . y < 0 )
{
//Higher gravity if falling
SetGravityScale ( Data . gravityScale * Data . fallGravityMult ) ;
//Caps maximum fall speed, so when falling over large distances we don't accelerate to insanely high speeds
RB . velocity = new Vector2 ( RB . velocity . x , Mathf . Max ( RB . velocity . y , - Data . maxFallSpeed ) ) ;
}
else
{
//Default gravity if standing on a platform or moving upwards
SetGravityScale ( Data . gravityScale ) ;
}
#endregion
}
private void FixedUpdate ( )
{
//Handle Run
if ( IsWallJumping )
Run ( Data . wallJumpRunLerp ) ;
else
Run ( 1 ) ;
//Handle Slide
if ( IsSliding )
Slide ( ) ;
2023-04-17 15:22:24 -07:00
}
2023-04-27 12:18:47 -07:00
#region INPUT CALLBACKS
//Methods which whandle input detected in Update()
public void OnJumpInput ( )
2023-04-09 21:59:32 -07:00
{
2023-04-27 12:18:47 -07:00
LastPressedJumpTime = Data . jumpInputBufferTime ;
2023-04-17 00:01:49 -07:00
}
2023-04-27 12:18:47 -07:00
public void OnJumpUpInput ( )
2023-04-17 00:01:49 -07:00
{
2023-04-27 12:18:47 -07:00
if ( CanJumpCut ( ) | | CanWallJumpCut ( ) )
_isJumpCut = true ;
2023-04-17 00:01:49 -07:00
}
2023-04-27 12:18:47 -07:00
#endregion
2023-04-17 00:01:49 -07:00
2023-04-27 12:18:47 -07:00
#region GENERAL METHODS
public void SetGravityScale ( float scale )
{
RB . gravityScale = scale ;
}
#endregion
//MOVEMENT METHODS
#region RUN METHODS
2023-04-17 00:01:49 -07:00
private void Run ( float lerpAmount )
{
2023-04-27 12:18:47 -07:00
//Calculate the direction we want to move in and our desired velocity
float targetSpeed = _moveInput . x * Data . runMaxSpeed ;
//We can reduce are control using Lerp() this smooths changes to are direction and speed
targetSpeed = Mathf . Lerp ( RB . velocity . x , targetSpeed , lerpAmount ) ;
2023-04-17 00:01:49 -07:00
2023-04-27 12:18:47 -07:00
#region Calculate AccelRate
float accelRate ;
2023-04-17 00:01:49 -07:00
2023-04-27 12:18:47 -07:00
//Gets an acceleration value based on if we are accelerating (includes turning)
//or trying to decelerate (stop). As well as applying a multiplier if we're air borne.
if ( LastOnGroundTime > 0 )
accelRate = ( Mathf . Abs ( targetSpeed ) > 0.01f ) ? Data . runAccelAmount : Data . runDeccelAmount ;
else
accelRate = ( Mathf . Abs ( targetSpeed ) > 0.01f ) ? Data . runAccelAmount * Data . accelInAir : Data . runDeccelAmount * Data . deccelInAir ;
#endregion
2023-04-17 00:01:49 -07:00
2023-04-27 12:18:47 -07:00
#region Add Bonus Jump Apex Acceleration
//Increase are acceleration and maxSpeed when at the apex of their jump, makes the jump feel a bit more bouncy, responsive and natural
if ( ( IsJumping | | IsWallJumping | | _isJumpFalling ) & & Mathf . Abs ( RB . velocity . y ) < Data . jumpHangTimeThreshold )
{
accelRate * = Data . jumpHangAccelerationMult ;
targetSpeed * = Data . jumpHangMaxSpeedMult ;
}
#endregion
2023-04-17 00:01:49 -07:00
2023-04-27 12:18:47 -07:00
#region Conserve Momentum
//We won't slow the player down if they are moving in their desired direction but at a greater speed than their maxSpeed
2023-04-27 13:31:04 -07:00
if ( ( Data . doConserveMomentum & & Mathf . Abs ( RB . velocity . x ) > Mathf . Abs ( targetSpeed ) & & Mathf . Sign ( RB . velocity . x ) = = Mathf . Sign ( targetSpeed ) & & Mathf . Abs ( targetSpeed ) > 0.01f & & LastOnGroundTime < 0 ) | | grapplingRope . isGrappling )
2023-04-27 12:18:47 -07:00
{
//Prevent any deceleration from happening, or in other words conserve are current momentum
//You could experiment with allowing for the player to slightly increae their speed whilst in this "state"
accelRate = 0 ;
}
#endregion
2023-04-17 00:01:49 -07:00
2023-04-27 12:18:47 -07:00
//Calculate difference between current velocity and desired velocity
float speedDif = targetSpeed - RB . velocity . x ;
//Calculate force along x-axis to apply to thr player
2023-04-17 00:01:49 -07:00
2023-04-27 12:18:47 -07:00
float movement = speedDif * accelRate ;
2023-04-17 00:01:49 -07:00
2023-04-27 12:18:47 -07:00
//Convert this to a vector and apply to rigidbody
RB . AddForce ( movement * Vector2 . right , ForceMode2D . Force ) ;
2023-04-17 00:01:49 -07:00
2023-04-27 12:18:47 -07:00
/ *
* For those interested here is what AddForce ( ) will do
* RB . velocity = new Vector2 ( RB . velocity . x + ( Time . fixedDeltaTime * speedDif * accelRate ) / RB . mass , RB . velocity . y ) ;
* Time . fixedDeltaTime is by default in Unity 0.02 seconds equal to 50 FixedUpdate ( ) calls per second
* /
}
2023-04-17 15:22:24 -07:00
2023-04-27 12:18:47 -07:00
private void Turn ( )
{
//stores scale and flips the player along the x axis,
Vector3 scale = transform . localScale ;
scale . x * = - 1 ;
transform . localScale = scale ;
IsFacingRight = ! IsFacingRight ;
2023-04-17 15:22:24 -07:00
}
2023-04-27 12:18:47 -07:00
#endregion
2023-04-17 15:22:24 -07:00
2023-04-27 12:18:47 -07:00
#region JUMP METHODS
private void Jump ( )
2023-04-17 15:22:24 -07:00
{
2023-04-27 12:18:47 -07:00
//Ensures we can't call Jump multiple times from one press
LastPressedJumpTime = 0 ;
LastOnGroundTime = 0 ;
#region Perform Jump
//We increase the force applied if we are falling
//This means we'll always feel like we jump the same amount
//(setting the player's Y velocity to 0 beforehand will likely work the same, but I find this more elegant :D)
float force = Data . jumpForce ;
if ( RB . velocity . y < 0 )
force - = RB . velocity . y ;
RB . AddForce ( Vector2 . up * force , ForceMode2D . Impulse ) ;
#endregion
}
private void WallJump ( int dir )
{
//Ensures we can't call Wall Jump multiple times from one press
LastPressedJumpTime = 0 ;
LastOnGroundTime = 0 ;
LastOnWallRightTime = 0 ;
LastOnWallLeftTime = 0 ;
#region Perform Wall Jump
Vector2 force = new Vector2 ( Data . wallJumpForce . x , Data . wallJumpForce . y ) ;
force . x * = dir ; //apply force in opposite direction of wall
if ( Mathf . Sign ( RB . velocity . x ) ! = Mathf . Sign ( force . x ) )
force . x - = RB . velocity . x ;
if ( RB . velocity . y < 0 ) //checks whether player is falling, if so we subtract the velocity.y (counteracting force of gravity). This ensures the player always reaches our desired jump force or greater
force . y - = RB . velocity . y ;
//Unlike in the run we want to use the Impulse mode.
//The default mode will apply are force instantly ignoring masss
//RB.AddForce(force, ForceMode2D.Impulse);
#endregion
}
#endregion
#region OTHER MOVEMENT METHODS
private void Slide ( )
{
//Works the same as the Run but only in the y-axis
//THis seems to work fine, buit maybe you'll find a better way to implement a slide into this system
float speedDif = Data . slideSpeed - RB . velocity . y ;
float movement = speedDif * Data . slideAccel ;
//So, we clamp the movement here to prevent any over corrections (these aren't noticeable in the Run)
//The force applied can't be greater than the (negative) speedDifference * by how many times a second FixedUpdate() is called. For more info research how force are applied to rigidbodies.
movement = Mathf . Clamp ( movement , - Mathf . Abs ( speedDif ) * ( 1 / Time . fixedDeltaTime ) , Mathf . Abs ( speedDif ) * ( 1 / Time . fixedDeltaTime ) ) ;
RB . AddForce ( movement * Vector2 . up ) ;
}
#endregion
#region CHECK METHODS
public void CheckDirectionToFace ( bool isMovingRight )
{
2023-04-27 13:31:04 -07:00
// if (isMovingRight != IsFacingRight)
// Turn();
2023-04-27 12:18:47 -07:00
}
private bool CanJump ( )
{
2023-04-27 19:47:39 -07:00
if ( ! IsGrounded ( ) & & trumpet ) {
return true ;
}
2023-04-27 12:18:47 -07:00
return LastOnGroundTime > 0 & & ! IsJumping ;
}
private bool CanWallJump ( )
{
return LastPressedJumpTime > 0 & & LastOnWallTime > 0 & & LastOnGroundTime < = 0 & & ( ! IsWallJumping | |
( LastOnWallRightTime > 0 & & _lastWallJumpDir = = 1 ) | | ( LastOnWallLeftTime > 0 & & _lastWallJumpDir = = - 1 ) ) ;
}
private bool CanJumpCut ( )
{
return IsJumping & & RB . velocity . y > 0 ;
}
private bool CanWallJumpCut ( )
{
return IsWallJumping & & RB . velocity . y > 0 ;
}
public bool CanSlide ( )
{
if ( LastOnWallTime > 0 & & ! IsJumping & & ! IsWallJumping & & LastOnGroundTime < = 0 )
2023-04-17 15:22:24 -07:00
return true ;
2023-04-27 12:18:47 -07:00
else
return false ;
2023-04-17 15:22:24 -07:00
}
2023-04-27 19:47:39 -07:00
public bool IsGrounded ( ) {
2023-04-27 20:12:07 -07:00
// print(Physics2D.OverlapBox(this.transform.position, _groundCheckSize, 0, _groundLayer) && !IsJumping);
2023-04-27 19:47:39 -07:00
return ( Physics2D . OverlapBox ( this . transform . position , _groundCheckSize , 0 , _groundLayer ) & & ! IsJumping ) ;
}
2023-04-27 12:18:47 -07:00
#endregion
2023-04-17 15:22:24 -07:00
2023-04-27 12:18:47 -07:00
#region EDITOR METHODS
private void OnDrawGizmosSelected ( )
2023-04-17 15:22:24 -07:00
{
2023-04-27 12:18:47 -07:00
Gizmos . color = Color . green ;
2023-04-27 12:54:31 -07:00
Gizmos . DrawWireCube ( this . transform . position , _groundCheckSize ) ;
2023-04-27 12:18:47 -07:00
Gizmos . color = Color . blue ;
2023-04-27 12:54:31 -07:00
Gizmos . DrawWireCube ( this . transform . position , _wallCheckSize ) ;
Gizmos . DrawWireCube ( this . transform . position , _wallCheckSize ) ;
2023-04-09 21:59:32 -07:00
}
2023-04-27 12:18:47 -07:00
#endregion
2023-04-09 21:59:32 -07:00
}