Skip to content

Commit 043e1c1

Browse files
added ability to double jump
1 parent 7b23638 commit 043e1c1

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

Assets/Scripts/PlayerController.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ enum PlayerState {
2828
private float _jumpLength = 0f; // how high you can jump depending on how long you hold down jump
2929

3030
private bool _canTriggerNewJump = true; // forces the player to let go of the jump button in order to jump again
31+
32+
private int _jumps = 0;
33+
34+
private int _maxJumps = 2;
3135

3236
private float _graceJumpPeriod = 0f; // grace period for jumping after walking off a ledge
3337

@@ -53,14 +57,21 @@ private void Awake() {
5357
_jumpTimer = 0.5f;
5458

5559
ControllerUtils.IgnorePlatformCollision();
60+
} else if (_playerState == PlayerState.Jumping && _jumps < _maxJumps) {
61+
_rigidbody2D.AddForce(new Vector2(0f, 0.5f) * _jumpStrength);
62+
63+
TriggerJump();
5664
}
5765
};
5866

5967
_jumpButtonControls = ControllerUtils.InitializeButtonControls(_playerActions.Player.Jump.controls);
6068
}
6169

6270
private void TriggerJump() {
63-
_canTriggerNewJump = false;
71+
_jumps++;
72+
73+
if (_jumps >= _maxJumps)
74+
_canTriggerNewJump = false;
6475

6576
_playerState = PlayerState.Jumping;
6677

@@ -107,14 +118,9 @@ private void Update() {
107118
if (_jumpTimer > 0f)
108119
_jumpTimer -= Time.deltaTime;
109120

110-
if (_jumpLength > 0f) {
121+
if (_jumpLength > 0f)
111122
_jumpLength -= Time.deltaTime;
112123

113-
if (_jumpLength <= 0f && _playerState == PlayerState.Jumping) {
114-
_playerState = PlayerState.Falling;
115-
}
116-
}
117-
118124
if (_graceJumpPeriod > 0f)
119125
_graceJumpPeriod -= Time.deltaTime;
120126
}
@@ -133,6 +139,8 @@ private void FixedUpdate() {
133139

134140
if (hit) {
135141
_layer = hit.collider.gameObject.layer;
142+
143+
_jumps = 0;
136144

137145
ControllerUtils.IgnorePlatformCollision(false);
138146

ProjectSettings/TagManager.asset

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ TagManager:
1515
- UI
1616
- Ground
1717
- Platform
18-
-
18+
- Wall
1919
-
2020
-
2121
-

0 commit comments

Comments
 (0)