Skip to content
This repository has been archived by the owner on Mar 9, 2024. It is now read-only.

Commit

Permalink
Add Movement Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Kassout committed Apr 18, 2023
1 parent 16b2edc commit c60cded
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 45 deletions.
18 changes: 9 additions & 9 deletions PlatformerController/Assets/Scenes/MainScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -41375,7 +41375,7 @@ Rigidbody2D:
m_Mass: 1
m_LinearDrag: 0
m_AngularDrag: 0.05
m_GravityScale: 4
m_GravityScale: 8
m_Material: {fileID: 6200000, guid: e7310a3f9e2163f45b4d7490fb136f86, type: 2}
m_IncludeLayers:
serializedVersion: 2
Expand All @@ -41385,7 +41385,7 @@ Rigidbody2D:
m_Bits: 0
m_Interpolate: 0
m_SleepingMode: 1
m_CollisionDetection: 0
m_CollisionDetection: 1
m_Constraints: 4
--- !u!61 &1398032901
BoxCollider2D:
Expand Down Expand Up @@ -41444,17 +41444,17 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 71d38265b57cbb848be350e7c638b90f, type: 3}
m_Name:
m_EditorClassIdentifier:
movementSpeed: 10
jumpForce: 16
amountOfJumps: 1
movementSpeed: 9
jumpForce: 20
groundCheckRadius: 0.3
wallCheckDistance: 0.4
wallSlideSpeed: 2
movementForceInAir: 50
wallCheckDistance: 0.65
wallSlideSpeed: 1
airDragMultiplier: 0.95
variableJumpHeightMultiplier: 0.5
wallHopForce: 10
wallJumpForce: 20
wallJumpForce: 30
jumpTimerSet: 0.15
turnTimerSet: 0.1
wallHopDirection: {x: 1, y: 0.5}
wallJumpDirection: {x: 1, y: 2}
groundCheck: {fileID: 360449007}
Expand Down
162 changes: 126 additions & 36 deletions PlatformerController/Assets/Scripts/PlayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,44 @@
public class PlayerController : MonoBehaviour
{
private float _movementInputDirection;
private float _jumpTimer;
private float _turnTimer;
private float _wallJumpTimer;

private int _amountOfJumpsLeft;
private int _facingDirection = 1;
private int _lastWallJumpDirection;

private bool _isFacingRight = true;
private bool _isWalking = false;
private bool _isGrounded = false;
private bool _canJump = false;
private bool _isWallSliding = false;
private bool _isTouchingWall = false;
private bool _isWallSliding = false;
private bool _canNormalJump = false;
private bool _canWallJump = false;
private bool _isAttemptingToJump = false;
private bool _checkJumpMultiplier = false;
private bool _canMove = false;
private bool _canFlip = false;
private bool _hasWallJumped = false;

private Rigidbody2D _rigidbody;
private Animator _animator;

public int amountOfJumps = 1;

public float movementSpeed = 10.0f;
public float jumpForce = 16.0f;
public int amountOfJumps = 1;
public float groundCheckRadius;
public float wallCheckDistance;
public float wallSlideSpeed;
public float movementForceInAir;
public float airDragMultiplier = 0.95f;
public float variableJumpHeightMultiplier = 0.5f;
public float wallHopForce;
public float wallJumpForce;

public float jumpTimerSet = 0.15f;
public float turnTimerSet = 0.1f;
public float wallJumpTimerSet = 0.5f;

public Vector2 wallHopDirection;
public Vector2 wallJumpDirection;

Expand All @@ -51,6 +65,7 @@ private void Update()
UpdateAnimations();
CheckIfCanJump();
CheckIfWallSliding();
CheckJump();
}

private void FixedUpdate()
Expand All @@ -61,7 +76,7 @@ private void FixedUpdate()

private void CheckIfWallSliding()
{
if (_isTouchingWall && !_isGrounded && _rigidbody.velocity.y < 0)
if (_isTouchingWall && _movementInputDirection == _facingDirection && _rigidbody.velocity.y < 0.0f)
{
_isWallSliding = true;
}
Expand All @@ -80,18 +95,23 @@ private void CheckSurroundings()

private void CheckIfCanJump()
{
if ((_isGrounded && _rigidbody.velocity.y <= 0) || _isWallSliding)
if (_isGrounded && _rigidbody.velocity.y <= 0.01f)
{
_amountOfJumpsLeft = amountOfJumps;
}

if (_isTouchingWall)
{
_canWallJump = true;
}

if (_amountOfJumpsLeft <= 0)
{
_canJump = false;
_canNormalJump = false;
}
else
{
_canJump = true;
_canNormalJump = true;
}
}

Expand Down Expand Up @@ -130,57 +150,127 @@ private void CheckInput()

if (Input.GetButtonDown("Jump"))
{
Jump();
if (_isGrounded || (_amountOfJumpsLeft > 0 && _isTouchingWall))
{
NormalJump();
}
else
{
_jumpTimer = jumpTimerSet;
_isAttemptingToJump = true;
}
}

if (Input.GetButtonDown("Horizontal") && _isTouchingWall)
{
if (!_isGrounded && _movementInputDirection != _facingDirection)
{
_canMove = false;
_canFlip = false;

_turnTimer = turnTimerSet;
}
}

if (!_canMove)
{
_turnTimer -= Time.deltaTime;

if (_turnTimer <= 0)
{
_canMove = true;
_canFlip = true;
}
}

if (Input.GetButtonUp("Jump"))
if (_checkJumpMultiplier && !Input.GetButton("Jump"))
{
_checkJumpMultiplier = false;
_rigidbody.velocity = new Vector2(_rigidbody.velocity.x, _rigidbody.velocity.y * variableJumpHeightMultiplier);
}
}

private void Jump()
private void CheckJump()
{
if (_canJump && !_isWallSliding)
if (_jumpTimer > 0)
{
_rigidbody.velocity = new Vector2(_rigidbody.velocity.x, jumpForce);
_amountOfJumpsLeft--;
// Wall Jump
if (!_isGrounded && _isTouchingWall && _movementInputDirection != 0 && _movementInputDirection != _facingDirection)
{
WallJump();
}
else if (_isGrounded)
{
NormalJump();
}
}
else if (_isWallSliding && _movementInputDirection == 0 && _canJump) // Wall Hop

if (_isAttemptingToJump)
{
_isWallSliding = false;
_jumpTimer -= Time.deltaTime;
}

if (_wallJumpTimer > 0)
{
if (_hasWallJumped && _movementInputDirection == -_lastWallJumpDirection)
{
_rigidbody.velocity = new Vector2(_rigidbody.velocity.x, 0.0f);
_hasWallJumped = false;
}
else if (_wallJumpTimer <= 0)
{
_hasWallJumped = false;
}
else
{
_wallJumpTimer -= Time.deltaTime;
}
}
}

private void NormalJump()
{
if (_canNormalJump)
{
_rigidbody.velocity = new Vector2(_rigidbody.velocity.x, jumpForce);
_amountOfJumpsLeft--;
Vector2 forceToAdd = new Vector2(wallHopForce * wallHopDirection.x * -_facingDirection, wallHopForce * wallHopDirection.y);
_rigidbody.AddForce(forceToAdd, ForceMode2D.Impulse);
_jumpTimer = 0f;
_isAttemptingToJump = false;
_checkJumpMultiplier = true;
}
else if ((_isWallSliding || _isTouchingWall) && _movementInputDirection != 0 && _canJump)
}

private void WallJump()
{
if (_canWallJump)
{
_rigidbody.velocity = new Vector2(_rigidbody.velocity.x, 0.0f);
_isWallSliding = false;
_amountOfJumpsLeft = amountOfJumps;
_amountOfJumpsLeft--;
Vector2 forceToAdd = new Vector2(wallJumpForce * wallJumpDirection.x * _movementInputDirection, wallJumpForce * wallJumpDirection.y);
_rigidbody.AddForce(forceToAdd, ForceMode2D.Impulse);
_jumpTimer = 0f;
_isAttemptingToJump = false;
_checkJumpMultiplier = true;
_turnTimer = 0f;
_canMove = true;
_canFlip = true;
_hasWallJumped = true;
_wallJumpTimer = wallJumpTimerSet;
_lastWallJumpDirection = -_facingDirection;
}
}

private void ApplyMovement()
{
if (_isGrounded)
{
_rigidbody.velocity = new Vector2(movementSpeed * _movementInputDirection, _rigidbody.velocity.y);
}
else if (!_isGrounded && !_isWallSliding && _movementInputDirection != 0)
if (!_isGrounded && !_isWallSliding && _movementInputDirection == 0)
{
var forceToAdd = new Vector2(movementForceInAir * _movementInputDirection, 0);
_rigidbody.AddForce(forceToAdd);

if (Mathf.Abs(_rigidbody.velocity.x) > movementSpeed)
{
_rigidbody.velocity = new Vector2(movementSpeed * _movementInputDirection, _rigidbody.velocity.y);
}
_rigidbody.velocity = new Vector2(_rigidbody.velocity.x * airDragMultiplier, _rigidbody.velocity.y);
}
else if (!_isGrounded && !_isWallSliding && _movementInputDirection == 0)
else if (_canMove)
{
_rigidbody.velocity = new Vector2(_rigidbody.velocity.x * airDragMultiplier, _rigidbody.velocity.y);
_rigidbody.velocity = new Vector2(movementSpeed * _movementInputDirection, _rigidbody.velocity.y);
}

if (_isWallSliding)
Expand All @@ -194,7 +284,7 @@ private void ApplyMovement()

private void Flip()
{
if (!_isWallSliding)
if (!_isWallSliding && _canFlip)
{
_facingDirection *= -1;
_isFacingRight = !_isFacingRight;
Expand Down

0 comments on commit c60cded

Please sign in to comment.