Skip to content

Commit d042541

Browse files
more fine tuning to platform controller
1 parent 04da1f2 commit d042541

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

Assets/Scenes/SampleScene.unity

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,9 @@ MonoBehaviour:
948948
_wallClingMask:
949949
serializedVersion: 2
950950
m_Bits: 384
951+
_headBumpMask:
952+
serializedVersion: 2
953+
m_Bits: 448
951954
--- !u!1660057539 &9223372036854775807
952955
SceneRoots:
953956
m_ObjectHideFlags: 0

Assets/Scripts/PlayerController.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,14 @@ enum PlayerState {
4141
private int _wallClingingDirection = 0;
4242

4343
private float _wallClingTimer = 0f;
44+
45+
private bool _headBump = false;
4446

4547
[SerializeField] private LayerMask _groundMask;
4648

4749
[SerializeField] private LayerMask _wallClingMask;
50+
51+
[SerializeField] private LayerMask _headBumpMask;
4852

4953
private int _layerBeneath = -1;
5054

@@ -91,8 +95,7 @@ private void Awake() {
9195
private void TriggerJump() {
9296
_jumps++;
9397

94-
if (_jumps >= _maxJumps)
95-
_canTriggerNewJump = false;
98+
_canTriggerNewJump = false;
9699

97100
_playerState = PlayerState.Jumping;
98101

@@ -103,7 +106,11 @@ private void TriggerJump() {
103106

104107
private float HandleJump() {
105108
if (_playerState == PlayerState.WallClinging) {
106-
return _rigidbody2D.linearVelocity.y / 2;
109+
return _rigidbody2D.linearVelocity.y / 1.5f;
110+
}
111+
112+
if (_headBump) {
113+
return -Mathf.Abs(_rigidbody2D.linearVelocity.y);
107114
}
108115

109116
if (ControllerUtils.IsButtonDown(_jumpButtonControls)) {
@@ -170,6 +177,8 @@ private void FixedUpdate() {
170177
if (_jumpTimer <= 0f && (_playerState == PlayerState.Falling || _playerState == PlayerState.Jumping)) {
171178
RaycastHit2D bottomHit = CastInDirection(RayCastRadius, Vector2.down, RayCastDistance, _groundMask);
172179

180+
RaycastHit2D topHit = CastInDirection(RayCastRadius, Vector2.up, RayCastDistance, _headBumpMask);
181+
173182
RaycastHit2D rightHit = CastInDirection(RayCastRadius, Vector2.right, RayCastDistance, _wallClingMask);
174183

175184
RaycastHit2D leftHit = CastInDirection(RayCastRadius, Vector2.left, RayCastDistance, _wallClingMask);
@@ -191,6 +200,8 @@ private void FixedUpdate() {
191200
}
192201

193202
if (bottomHit) {
203+
_headBump = false;
204+
194205
_layerBeneath = bottomHit.collider.gameObject.layer;
195206

196207
_jumps = 0;
@@ -201,6 +212,10 @@ private void FixedUpdate() {
201212
} else {
202213
_layerBeneath = -1;
203214
}
215+
216+
if (topHit) { // kill the current jump
217+
_headBump = true;
218+
}
204219
}
205220
}
206221

@@ -224,7 +239,9 @@ private void OnDrawGizmos() {
224239

225240
DrawHit(leftHit, Vector2.left);
226241

227-
// GroundRayCastDetection();
242+
RaycastHit2D topHit = CastInDirection(RayCastRadius, Vector2.up, RayCastDistance, _headBumpMask);
243+
244+
DrawHit(topHit, Vector2.up);
228245
}
229246

230247
private void DrawHit(RaycastHit2D hit, Vector2 direction) {

0 commit comments

Comments
 (0)