Skip to content

Commit 04da1f2

Browse files
work on wall jump
1 parent 043e1c1 commit 04da1f2

File tree

3 files changed

+102
-37
lines changed

3 files changed

+102
-37
lines changed

Assets/Scenes/SampleScene.unity

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -211,31 +211,31 @@ Transform:
211211
m_Children: []
212212
m_Father: {fileID: 0}
213213
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
214-
--- !u!1 &1409261625
214+
--- !u!1 &645401453
215215
GameObject:
216216
m_ObjectHideFlags: 0
217217
m_CorrespondingSourceObject: {fileID: 0}
218218
m_PrefabInstance: {fileID: 0}
219219
m_PrefabAsset: {fileID: 0}
220220
serializedVersion: 6
221221
m_Component:
222-
- component: {fileID: 1409261628}
223-
- component: {fileID: 1409261627}
224-
- component: {fileID: 1409261626}
225-
m_Layer: 7
226-
m_Name: Platform (3)
222+
- component: {fileID: 645401456}
223+
- component: {fileID: 645401455}
224+
- component: {fileID: 645401454}
225+
m_Layer: 8
226+
m_Name: Wall (1)
227227
m_TagString: Platform
228228
m_Icon: {fileID: 0}
229229
m_NavMeshLayer: 0
230230
m_StaticEditorFlags: 0
231231
m_IsActive: 1
232-
--- !u!61 &1409261626
232+
--- !u!61 &645401454
233233
BoxCollider2D:
234234
m_ObjectHideFlags: 0
235235
m_CorrespondingSourceObject: {fileID: 0}
236236
m_PrefabInstance: {fileID: 0}
237237
m_PrefabAsset: {fileID: 0}
238-
m_GameObject: {fileID: 1409261625}
238+
m_GameObject: {fileID: 645401453}
239239
m_Enabled: 1
240240
serializedVersion: 3
241241
m_Density: 1
@@ -275,13 +275,13 @@ BoxCollider2D:
275275
m_AutoTiling: 0
276276
m_Size: {x: 1, y: 1}
277277
m_EdgeRadius: 0
278-
--- !u!212 &1409261627
278+
--- !u!212 &645401455
279279
SpriteRenderer:
280280
m_ObjectHideFlags: 0
281281
m_CorrespondingSourceObject: {fileID: 0}
282282
m_PrefabInstance: {fileID: 0}
283283
m_PrefabAsset: {fileID: 0}
284-
m_GameObject: {fileID: 1409261625}
284+
m_GameObject: {fileID: 645401453}
285285
m_Enabled: 1
286286
m_CastShadows: 0
287287
m_ReceiveShadows: 0
@@ -333,17 +333,17 @@ SpriteRenderer:
333333
m_WasSpriteAssigned: 1
334334
m_MaskInteraction: 0
335335
m_SpriteSortPoint: 0
336-
--- !u!4 &1409261628
336+
--- !u!4 &645401456
337337
Transform:
338338
m_ObjectHideFlags: 0
339339
m_CorrespondingSourceObject: {fileID: 0}
340340
m_PrefabInstance: {fileID: 0}
341341
m_PrefabAsset: {fileID: 0}
342-
m_GameObject: {fileID: 1409261625}
342+
m_GameObject: {fileID: 645401453}
343343
serializedVersion: 2
344344
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
345-
m_LocalPosition: {x: -5.25, y: 2.45, z: 0}
346-
m_LocalScale: {x: 4.78, y: 1, z: 1}
345+
m_LocalPosition: {x: -8.31, y: 2.3552, z: 0}
346+
m_LocalScale: {x: 1.18066, y: 12.877043, z: 1}
347347
m_ConstrainProportionsScale: 0
348348
m_Children: []
349349
m_Father: {fileID: 0}
@@ -939,11 +939,15 @@ MonoBehaviour:
939939
m_EditorClassIdentifier: Assembly-CSharp::PlayerController
940940
_playerState: 0
941941
_rigidbody2D: {fileID: 1986650177}
942+
_boxCollider2D: {fileID: 1986650178}
942943
_movementSpeed: 6
943944
_jumpStrength: 6.5
944945
_groundMask:
945946
serializedVersion: 2
946947
m_Bits: 192
948+
_wallClingMask:
949+
serializedVersion: 2
950+
m_Bits: 384
947951
--- !u!1660057539 &9223372036854775807
948952
SceneRoots:
949953
m_ObjectHideFlags: 0
@@ -953,4 +957,4 @@ SceneRoots:
953957
- {fileID: 1904460580}
954958
- {fileID: 1684821267}
955959
- {fileID: 1447408137}
956-
- {fileID: 1409261628}
960+
- {fileID: 645401456}

Assets/Scripts/PlayerController.cs

Lines changed: 82 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ enum PlayerState {
88
Idle, // grounded not moving
99
Walking, // grounded moving
1010
Jumping, // pressed jump, hasn't touched ground yet
11-
Falling // didn't press jump, velocity of y is negative
11+
Falling, // didn't press jump, velocity of y is negative
12+
WallClinging, // player is clinging to a wall
1213
}
1314

1415
[SerializeField] private PlayerState _playerState = PlayerState.Idle;
@@ -18,6 +19,8 @@ enum PlayerState {
1819
private ButtonControl[] _jumpButtonControls;
1920

2021
[SerializeField] private Rigidbody2D _rigidbody2D;
22+
23+
[SerializeField] private BoxCollider2D _boxCollider2D;
2124

2225
[SerializeField] private float _movementSpeed = 4;
2326

@@ -34,10 +37,16 @@ enum PlayerState {
3437
private int _maxJumps = 2;
3538

3639
private float _graceJumpPeriod = 0f; // grace period for jumping after walking off a ledge
40+
41+
private int _wallClingingDirection = 0;
42+
43+
private float _wallClingTimer = 0f;
3744

3845
[SerializeField] private LayerMask _groundMask;
46+
47+
[SerializeField] private LayerMask _wallClingMask;
3948

40-
private int _layer = -1;
49+
private int _layerBeneath = -1;
4150

4251
private Vector2 _movementInput;
4352

@@ -49,7 +58,19 @@ private void Awake() {
4958
};
5059

5160
_playerActions.Player.Jump.performed += context => {
52-
if (_layer == ControllerUtils.PlatformLayer && Math.Abs(_movementInput.y - (-1)) < 0.01f) {
61+
if (_wallClingTimer > 0f) return;
62+
63+
if (_playerState == PlayerState.WallClinging) {
64+
_rigidbody2D.AddForce(new Vector2(_wallClingingDirection == 1 ? 25 : -25, 50) * _jumpStrength);
65+
66+
_wallClingTimer = 0.15f;
67+
68+
TriggerJump();
69+
70+
return;
71+
}
72+
73+
if (_layerBeneath == ControllerUtils.PlatformLayer && Math.Abs(_movementInput.y - (-1)) < 0.01f) {
5374
_playerState = PlayerState.Falling;
5475

5576
_canTriggerNewJump = false;
@@ -81,6 +102,10 @@ private void TriggerJump() {
81102
}
82103

83104
private float HandleJump() {
105+
if (_playerState == PlayerState.WallClinging) {
106+
return _rigidbody2D.linearVelocity.y / 2;
107+
}
108+
84109
if (ControllerUtils.IsButtonDown(_jumpButtonControls)) {
85110
if (_canTriggerNewJump && _playerState != PlayerState.Jumping && _playerState != PlayerState.Falling) {
86111
TriggerJump();
@@ -123,10 +148,18 @@ private void Update() {
123148

124149
if (_graceJumpPeriod > 0f)
125150
_graceJumpPeriod -= Time.deltaTime;
151+
152+
if (_wallClingTimer > 0f)
153+
_wallClingTimer -= Time.deltaTime;
154+
}
155+
156+
private RaycastHit2D CastInDirection(float radius, Vector2 direction, float distance, LayerMask mask) {
157+
return Physics2D.CircleCast(transform.position, radius, direction, distance, mask);
126158
}
127159

128160
private void FixedUpdate() {
129-
_rigidbody2D.linearVelocity = HandleControllerMovement();
161+
if (_wallClingTimer <= 0f)
162+
_rigidbody2D.linearVelocity = HandleControllerMovement();
130163

131164
if (_rigidbody2D.linearVelocity.y < 0 && _playerState != PlayerState.Jumping && _playerState != PlayerState.Falling) {
132165
_graceJumpPeriod = 0.15f;
@@ -135,47 +168,74 @@ private void FixedUpdate() {
135168
}
136169

137170
if (_jumpTimer <= 0f && (_playerState == PlayerState.Falling || _playerState == PlayerState.Jumping)) {
138-
RaycastHit2D hit = Physics2D.CircleCast(transform.position, GroundRayCastRadius, Vector2.down, GroundRayCastDistance, _groundMask);
171+
RaycastHit2D bottomHit = CastInDirection(RayCastRadius, Vector2.down, RayCastDistance, _groundMask);
172+
173+
RaycastHit2D rightHit = CastInDirection(RayCastRadius, Vector2.right, RayCastDistance, _wallClingMask);
174+
175+
RaycastHit2D leftHit = CastInDirection(RayCastRadius, Vector2.left, RayCastDistance, _wallClingMask);
176+
177+
if (rightHit && (rightHit.collider.CompareTag("Platform") || rightHit.collider.CompareTag("Wall")) && Math.Abs(_movementInput.x - 1) < 0.1f) {
178+
_wallClingingDirection = -1;
179+
180+
_playerState = PlayerState.WallClinging;
181+
182+
_jumps = 0;
183+
} else if (leftHit && (leftHit.collider.CompareTag("Platform") || leftHit.collider.CompareTag("Wall")) && Math.Abs(_movementInput.x - (-1)) < 0.1f) {
184+
_wallClingingDirection = 1;
185+
186+
_playerState = PlayerState.WallClinging;
187+
188+
_jumps = 0;
189+
} else {
190+
_wallClingingDirection = 0;
191+
}
139192

140-
if (hit) {
141-
_layer = hit.collider.gameObject.layer;
193+
if (bottomHit) {
194+
_layerBeneath = bottomHit.collider.gameObject.layer;
142195

143196
_jumps = 0;
144197

145198
ControllerUtils.IgnorePlatformCollision(false);
146199

147200
_playerState = PlayerState.Idle;
148-
}
149-
else {
150-
_layer = -1;
201+
} else {
202+
_layerBeneath = -1;
151203
}
152204
}
153205
}
154206

155207
// collision detection
156208

157-
private const float GroundRayCastRadius = 0.3f;
158-
private const float GroundRayCastDistance = 0.3f;
209+
private const float RayCastRadius = 0.3f;
210+
private const float RayCastDistance = 0.3f;
159211

160212
// gizmos
161213

162214
private void OnDrawGizmos() {
163-
GroundRayCastDetection();
164-
}
165-
166-
private void GroundRayCastDetection() {
167-
Vector2 origin = transform.position;
168-
Vector2 direction = Vector2.down;
215+
RaycastHit2D bottomHit = CastInDirection(RayCastRadius, Vector2.down, RayCastDistance, _groundMask);
216+
217+
DrawHit(bottomHit, Vector2.down);
218+
219+
RaycastHit2D rightHit = CastInDirection(RayCastRadius, Vector2.right, RayCastDistance, _wallClingMask);
220+
221+
DrawHit(rightHit, Vector2.right);
169222

170-
RaycastHit2D hit = Physics2D.CircleCast(origin, GroundRayCastRadius, direction, GroundRayCastDistance, _groundMask);
223+
RaycastHit2D leftHit = CastInDirection(RayCastRadius, Vector2.left, RayCastDistance, _wallClingMask);
224+
225+
DrawHit(leftHit, Vector2.left);
226+
227+
// GroundRayCastDetection();
228+
}
171229

230+
private void DrawHit(RaycastHit2D hit, Vector2 direction) {
172231
Gizmos.color = hit ? Color.green : Color.red;
173-
Gizmos.DrawLine(origin, origin + direction * GroundRayCastDistance);
174-
Gizmos.DrawWireSphere(origin + direction * GroundRayCastDistance, GroundRayCastRadius);
232+
Gizmos.DrawLine(transform.position, (Vector2) transform.position + direction * RayCastDistance);
233+
234+
Gizmos.DrawWireSphere((Vector2) transform.position + direction * RayCastDistance, RayCastRadius);
175235

176236
if (hit) {
177237
Gizmos.color = Color.yellow;
178-
Gizmos.DrawWireSphere(hit.point, GroundRayCastRadius);
238+
Gizmos.DrawWireSphere(hit.point, RayCastRadius);
179239
}
180240
}
181241

ProjectSettings/TagManager.asset

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ TagManager:
66
tags:
77
- Ground
88
- Platform
9+
- Wall
910
layers:
1011
- Default
1112
- TransparentFX

0 commit comments

Comments
 (0)