Skip to content

Commit b502bd3

Browse files
added variable jump
1 parent 9dc4f7d commit b502bd3

File tree

8 files changed

+111
-37
lines changed

8 files changed

+111
-37
lines changed

.idea/.idea.Unity-Platformer-Template/.idea/.gitignore

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/.idea.Unity-Platformer-Template/.idea/encodings.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/.idea.Unity-Platformer-Template/.idea/indexLayout.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/.idea.Unity-Platformer-Template/.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scenes/SampleScene.unity

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ MonoBehaviour:
666666
_playerState: 0
667667
_rigidbody2D: {fileID: 1986650177}
668668
_movementSpeed: 6
669-
_jumpStrength: 12
669+
_jumpStrength: 6.5
670670
_groundMask:
671671
serializedVersion: 2
672672
m_Bits: 64

Assets/Scripts/ControllerUtils.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using UnityEngine.InputSystem;
2+
using UnityEngine.InputSystem.Controls;
3+
using UnityEngine.InputSystem.Utilities;
4+
5+
public static class ControllerUtils {
6+
7+
public static ButtonControl[] InitializeButtonControls(ReadOnlyArray<InputControl> controls) {
8+
ButtonControl[] buttonControls = new ButtonControl[controls.Count];
9+
10+
for (int i = 0; i < controls.Count; i++) {
11+
buttonControls[i] = (ButtonControl) controls[i];
12+
}
13+
14+
return buttonControls;
15+
}
16+
17+
public static bool IsButtonDown(ButtonControl[] buttonControls) {
18+
foreach (ButtonControl buttonControl in buttonControls) {
19+
if (buttonControl != null && buttonControl.isPressed)
20+
return true;
21+
}
22+
23+
return false;
24+
}
25+
26+
}

Assets/Scripts/ControllerUtils.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/PlayerController.cs

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using UnityEngine;
3+
using UnityEngine.InputSystem.Controls;
34

45
public class PlayerController : MonoBehaviour {
56

@@ -13,16 +14,22 @@ enum PlayerState {
1314
[SerializeField] private PlayerState _playerState = PlayerState.Idle;
1415

1516
private PlayerInputActions _playerActions;
17+
18+
private ButtonControl[] _jumpButtonControls;
1619

1720
[SerializeField] private Rigidbody2D _rigidbody2D;
1821

1922
[SerializeField] private float _movementSpeed = 4;
2023

2124
[SerializeField] private float _jumpStrength = 15;
2225

23-
private float _jumpTimer = 0f;
26+
private float _jumpTimer = 0f; // time before you can be considered on the ground again
27+
28+
private float _jumpLength = 0f; // how high you can jump depending on how long you hold down jump
29+
30+
private bool _canTriggerNewJump = true; // forces the player to let go of the jump button in order to jump again
2431

25-
private float _graceJumpPeriod = 0f;
32+
private float _graceJumpPeriod = 0f; // grace period for jumping after walking off a ledge
2633

2734
[SerializeField] private LayerMask _groundMask;
2835

@@ -34,57 +41,65 @@ private void Awake() {
3441
_playerActions.Player.Move.performed += context => {
3542
_movementInput = context.ReadValue<Vector2>();
3643
};
44+
45+
_jumpButtonControls = ControllerUtils.InitializeButtonControls(_playerActions.Player.Jump.controls);
46+
}
47+
48+
private void TriggerJump() {
49+
_canTriggerNewJump = false;
3750

38-
// replace this with the "mario jump", player has a bigger jump the longer they hold down the jump button
39-
40-
// -- jump -- from https://github.com/Joshalexjacobs/Fantastic-Guac-Endless-Demo/blob/master/player.lua
41-
// if (love.keyboard.isDown('n') and player.isJumping == false and player.isGrounded and player.jumpLock == false) or (pressX() and player.isJumping == false and player.isGrounded and player.jumpLock == false) then -- when the player hits jump
42-
// player.isJumping = true
43-
// player.jumpLock = true
44-
// player.isGrounded = false
45-
// player.dy = -player.initVel -- 6 is our current initial velocity
46-
// jumpTimer = jumpTimerMax // this is like a "jump hold" timer
47-
// elseif (love.keyboard.isDown('n') and jumpTimer > 0 and player.isJumping) or (pressX() and jumpTimer > 0 and player.isJumping) then
48-
// player.dy = player.dy + (-0.5) // I think this is a double jump
49-
// elseif (love.keyboard.isDown('n') == false and player.isJumping) or (pressX() == false and player.isJumping) then -- if the player releases the jump button mid-jump...
50-
// if player.dy < player.termVel then -- and if the player's velocity has reached the minimum velocity (minimum jump height)...
51-
// player.dy = player.termVel -- terminate the jump
52-
// end
53-
// player.isJumping = false
54-
// end
55-
56-
_playerActions.Player.Jump.performed += _ => {
57-
if (_playerState != PlayerState.Jumping && _playerState != PlayerState.Falling) {
58-
_playerState = PlayerState.Jumping;
51+
_playerState = PlayerState.Jumping;
5952

60-
_rigidbody2D.AddForce(new Vector2(0f, 40f) * _jumpStrength);
53+
_jumpTimer = 0.2f;
54+
55+
_jumpLength = 0.3f;
56+
}
6157

62-
_jumpTimer = 0.2f;
63-
} else if (_playerState == PlayerState.Falling && _graceJumpPeriod > 0f) {
64-
_playerState = PlayerState.Jumping;
65-
66-
_rigidbody2D.AddForce(new Vector2(0f, 40f) * _jumpStrength);
58+
private float HandleJump() {
59+
if (ControllerUtils.IsButtonDown(_jumpButtonControls)) {
60+
if (_canTriggerNewJump && _playerState != PlayerState.Jumping && _playerState != PlayerState.Falling) {
61+
TriggerJump();
6762

68-
_jumpTimer = 0.2f;
63+
return _jumpStrength;
64+
} else if (_playerState == PlayerState.Jumping && _jumpLength > 0f) {
65+
return _jumpStrength;
66+
} else if (_canTriggerNewJump && _playerState == PlayerState.Falling && _graceJumpPeriod > 0f) {
67+
TriggerJump();
68+
69+
return _jumpStrength;
6970
}
70-
};
71-
}
71+
} else if (_playerState == PlayerState.Jumping) {
72+
_jumpLength = 0f;
73+
} else if (_canTriggerNewJump == false && (_playerState == PlayerState.Idle || _playerState == PlayerState.Walking)) {
74+
_canTriggerNewJump = true;
75+
}
7276

77+
return _rigidbody2D.linearVelocity.y;
78+
}
79+
7380
private Vector2 HandleControllerMovement() {
7481
float x = _movementInput.x * _movementSpeed;
7582

7683
if (_playerState == PlayerState.Idle && (x > 0.01 || x < -0.01)) {
7784
_playerState = PlayerState.Walking;
78-
} else if (_playerState == PlayerState.Walking && x < 0.01 && x > -0.01) {
85+
} else if (_playerState == PlayerState.Walking && x < 0.01 && x > -0.01) {
7986
_playerState = PlayerState.Idle;
8087
}
8188

82-
return new Vector2(x, _rigidbody2D.linearVelocity.y);
89+
return new Vector2(x, HandleJump());
8390
}
8491

8592
private void Update() {
8693
if (_jumpTimer > 0f)
8794
_jumpTimer -= Time.deltaTime;
95+
96+
if (_jumpLength > 0f) {
97+
_jumpLength -= Time.deltaTime;
98+
99+
if (_jumpLength <= 0f && _playerState == PlayerState.Jumping) {
100+
_playerState = PlayerState.Falling;
101+
}
102+
}
88103

89104
if (_graceJumpPeriod > 0f)
90105
_graceJumpPeriod -= Time.deltaTime;
@@ -110,8 +125,8 @@ private void FixedUpdate() {
110125

111126
// collision detection
112127

113-
private const float GroundRayCastRadius = 0.25f;
114-
private const float GroundRayCastDistance = 0.25f;
128+
private const float GroundRayCastRadius = 0.3f;
129+
private const float GroundRayCastDistance = 0.3f;
115130

116131
// gizmos
117132

0 commit comments

Comments
 (0)