Browse Source

Basic wall jump

jeff
Jeff 2 years ago
parent
commit
f5fd3985d1
5 changed files with 73 additions and 24 deletions
  1. BIN
      Assets/Scenes/Level 1.unity
  2. BIN
      Assets/Scenes/WalljumpTest.unity
  3. +7
    -0
      Assets/Scenes/WalljumpTest.unity.meta
  4. +59
    -20
      Assets/Scripts/Character/CharacterController2D.cs
  5. BIN
      ProjectSettings/TagManager.asset

BIN
Assets/Scenes/Level 1.unity (Stored with Git LFS) View File

size 272425

BIN
Assets/Scenes/WalljumpTest.unity (Stored with Git LFS) View File

size 275436

+ 7
- 0
Assets/Scenes/WalljumpTest.unity.meta View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 556c065fcd69be141a2f1267d7801f32
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

+ 59
- 20
Assets/Scripts/Character/CharacterController2D.cs View File

@ -10,14 +10,18 @@ public class CharacterController2D : MonoBehaviour
[SerializeField] private LayerMask m_WhatIsGround; // A mask determining what is ground to the character [SerializeField] private LayerMask m_WhatIsGround; // A mask determining what is ground to the character
[SerializeField] private Transform m_GroundCheck; // A position marking where to check if the player is grounded. [SerializeField] private Transform m_GroundCheck; // A position marking where to check if the player is grounded.
[SerializeField] private Transform m_CeilingCheck; // A position marking where to check for ceilings [SerializeField] private Transform m_CeilingCheck; // A position marking where to check for ceilings
[SerializeField] private Transform m_WallCheck;
[SerializeField] private Collider2D m_CrouchDisableCollider; // A collider that will be disabled when crouching [SerializeField] private Collider2D m_CrouchDisableCollider; // A collider that will be disabled when crouching
const float k_GroundedRadius = .2f; // Radius of the overlap circle to determine if grounded const float k_GroundedRadius = .2f; // Radius of the overlap circle to determine if grounded
private bool m_Grounded; // Whether or not the player is grounded. private bool m_Grounded; // Whether or not the player is grounded.
private bool m_TouchingWall;
const float k_CeilingRadius = .2f; // Radius of the overlap circle to determine if the player can stand up const float k_CeilingRadius = .2f; // Radius of the overlap circle to determine if the player can stand up
private Rigidbody2D m_Rigidbody2D; private Rigidbody2D m_Rigidbody2D;
private bool m_FacingRight = true; // For determining which way the player is currently facing. private bool m_FacingRight = true; // For determining which way the player is currently facing.
private Vector3 m_Velocity = Vector3.zero; private Vector3 m_Velocity = Vector3.zero;
private enum Direction { NA=0, L, R };
private Direction m_LastWallJumpDirection = Direction.NA;
[Header("Events")] [Header("Events")]
[Space] [Space]
@ -43,23 +47,39 @@ public class CharacterController2D : MonoBehaviour
private void FixedUpdate() private void FixedUpdate()
{ {
bool wasGrounded = m_Grounded;
m_Grounded = false;
// The player is grounded if a circlecast to the groundcheck position hits anything designated as ground
// This can be done using layers instead but Sample Assets will not overwrite your project settings.
Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i].gameObject != gameObject)
{
m_Grounded = true;
if (!wasGrounded)
OnLandEvent.Invoke();
}
}
CheckGrounded();
CheckTouchingWall();
} }
private void CheckGrounded()
{
bool wasGrounded = m_Grounded;
m_Grounded = false;
Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i].gameObject != gameObject)
{
m_Grounded = true;
if (!wasGrounded)
OnLandEvent.Invoke();
}
}
}
private void CheckTouchingWall()
{
m_TouchingWall = false;
if(!m_Grounded)
{
if(Physics2D.OverlapCircle(m_WallCheck.position, 0.2f, m_WhatIsGround))
{
m_TouchingWall = true;
}
}
}
public void Move(float move, bool crouch, bool jump) public void Move(float move, bool crouch, bool jump)
{ {
@ -129,12 +149,31 @@ public class CharacterController2D : MonoBehaviour
{ {
// Add a vertical force to the player. // Add a vertical force to the player.
m_Grounded = false; m_Grounded = false;
m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
// Reset wall jump switch
m_LastWallJumpDirection = move > 0 ? Direction.R : Direction.L;
} }
}
private void Flip()
else if (m_TouchingWall && jump)
{
if (m_FacingRight && m_LastWallJumpDirection == Direction.L)
{
m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
m_LastWallJumpDirection = Direction.R;
}
else if (!m_FacingRight && m_LastWallJumpDirection == Direction.R)
{
m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
m_LastWallJumpDirection = Direction.L;
}
}
}
private void Flip()
{ {
// Switch the way the player is labelled as facing. // Switch the way the player is labelled as facing.
m_FacingRight = !m_FacingRight; m_FacingRight = !m_FacingRight;

BIN
ProjectSettings/TagManager.asset (Stored with Git LFS) View File

size 588

Loading…
Cancel
Save