I’m trying to implement a wall jump feature but I can’t seem to make it work. My wall jump method never gets executed and I can’t figure out why. I think it has something to do with my wall slide. Any help is appreciated.
public class PlayerController : MonoBehaviour
{
public Rigidbody2D playerRB;
private BoxCollider2D playerBC2D;
private Animator playerAnimator;
[Header("Slide")]
public BoxCollider2D playerSlideHitbox;
public float slideTime = 1f;
public bool isSliding = false;
[Header("Movement")]
public float xMovementInput;
public float yMovementInput;
public float movementSpeed;
public float baseMoveSpeed;
public float inAirMovementSpeed;
public float maxAirSpeed;
[SerializeField]
private float currentSpeed = 0f;
public float startSpeed = 10f;
public float minSpeed = 8f;
public bool isFacingRight;
[Header("Jump")]
public float jumpForce = 10f;
private bool isGrounded;
public Transform groundCheck;
public float checkRadius;
public LayerMask whatIsGround;
private float jumpCounter;
public float fallingGravity;
public float defaultGravity;
public float jumpCutMultiplier;
private float xVelocityBeforeJump;
[Header("Wall Slide")]
public Transform frontCheck;
public LayerMask whatIsWall;
private bool isTouchingFront;
public bool isWallSliding;
public float wallSlidingSpeed;
[Header("Wall Jump")]
public float xWallForce;
public float yWallForce;
public bool isWallJumping;
public float wallJumpTime;
[Header("Hang Jump")]
public float hangTime;
private float hangCounter;
[Header("Jump Buffer")]
public float jumpBufferLength = 0.1f;
private float jumpBufferCounter;
//[Header("Parkour Moves")]
private bool canBackflip;
[Header("Sounds")]
public AudioSource footstepsAudio;
void Start()
{
playerRB = GetComponent<Rigidbody2D>();
playerBC2D = GetComponent<BoxCollider2D>();
playerAnimator = GetComponent<Animator>();
isFacingRight = true;
}
private void Update()
{
xMovementInput = UnityEngine.Input.GetAxis("Horizontal");
yMovementInput = UnityEngine.Input.GetAxis("Vertical");
isGrounded = Physics2D.OverlapCircle(groundCheck.transform.position, checkRadius, whatIsGround);
isTouchingFront = Physics2D.OverlapCircle(frontCheck.transform.position, checkRadius, whatIsGround);
if (isTouchingFront && !isGrounded && yMovementInput > 0 && !Input.GetButtonDown("Jump"))
{
isWallSliding = true;
WallSlide();
}
else
{
isWallSliding = false;
}
if (isGrounded)
{
playerRB.gravityScale = defaultGravity;
playerRB.drag = 1;
hangCounter = hangTime;
jumpCounter = 0f;
}
else
{
if (playerRB.velocity.y < 0)
{
playerRB.gravityScale = fallingGravity;
}
hangCounter -= Time.deltaTime;
playerRB.drag = 0;
}
if (Input.GetButtonDown("Jump") && isGrounded || Input.GetButtonDown("Jump") && hangCounter > 0)
{
PlayerJump();
playerAnimator.SetBool("isJumping", true);
}
if (Input.GetKeyDown(KeyCode.LeftShift) && isGrounded && !isSliding)
{
PlayerSlide();
}
if (isWallSliding && Input.GetKeyDown(KeyCode.Space) && !isWallJumping)
{
WallJump();
}
FlipPlayerHorizontal();
currentSpeed = playerRB.velocity.x * 3.6f;
currentSpeed = Mathf.Abs(currentSpeed);
if (Input.GetAxis("Horizontal") != 0)
{
playerAnimator.SetBool("isRunning", true);
}
if (currentSpeed == 0)
{
playerAnimator.SetBool("isRunning", false);
}
//Parkour moves.
BackFlip();
}
private void FixedUpdate()
{
HorizontalMovement();
}
void LateUpdate()
{
if (isGrounded)
{
playerAnimator.SetBool("isJumping", false);
}
}
void HorizontalMovement()
{
if (playerRB.velocity.x > 0)
{
isFacingRight = true;
}
else if (playerRB.velocity.x < 0)
{
isFacingRight = false;
}
if (xMovementInput != 0 && isGrounded)
{
playerRB.velocity = new Vector2(xMovementInput * movementSpeed * Time.deltaTime, playerRB.velocity.y);
}
else if (xMovementInput == 0 && isGrounded)
{
playerRB.velocity = new Vector2(0, playerRB.velocity.y);
}
else if (!isGrounded && !isWallJumping && !isWallSliding && currentSpeed < maxAirSpeed || !isGrounded && !isWallJumping && !isWallSliding && (isFacingRight && xMovementInput < 0 || !isFacingRight && xMovementInput > 0))
{
playerRB.velocity = new Vector2(playerRB.velocity.x + xMovementInput * inAirMovementSpeed * Time.deltaTime, playerRB.velocity.y);
}
}
void PlayerJump()
{
playerAnimator.SetBool("isRunning", false);
playerRB.velocity = new Vector2(playerRB.velocity.x, jumpForce);
jumpCounter += Time.deltaTime;
}
void PlayerSlide()
{
playerBC2D.enabled = false;
playerSlideHitbox.enabled = true;
transform.localScale = new Vector2(transform.localScale.x, 2f);
isSliding = true;
StartCoroutine(EndSlide(slideTime));
}
IEnumerator EndSlide(float waitTime)
{
yield return new WaitForSecondsRealtime(waitTime);
playerBC2D.enabled = true;
playerSlideHitbox.enabled = false;
isSliding = false;
transform.localScale = new Vector2(transform.localScale.x, 4f);
}
void FlipPlayerHorizontal()
{
if (playerRB.velocity.x < 0f && !isWallJumping)
{
transform.localScale = new Vector3(-4, transform.localScale.y);
isFacingRight = false;
}
else if (playerRB.velocity.x > 0f && !isWallJumping)
{
transform.localScale = new Vector3(4, transform.localScale.y);
isFacingRight = true;
}
}
void WallSlide()
{
playerRB.velocity = new Vector2(0f, Mathf.Clamp(playerRB.velocity.y, -wallSlidingSpeed, float.MaxValue));
}
void WallJump()
{
Debug.Log("Wall Jump");
if (!isWallJumping && isFacingRight)
{
playerRB.velocity = new Vector2(-xWallForce, yWallForce);
isWallJumping = true;
}
else if (!isWallJumping && !isFacingRight)
{
playerRB.velocity = new Vector2(xWallForce * Time.deltaTime, yWallForce * Time.deltaTime);
isWallJumping = true;
}
Invoke("SetWallJumpingToFalse", wallJumpTime);
}
private void OnCollisionEnter2D(Collision2D other)
{
if (isTouchingFront && !isWallJumping && !isWallSliding)
{
playerRB.velocity = new Vector2(0, playerRB.velocity.y);
}
}
void SetWallJumpingToFalse()
{
isWallJumping = false;
}
int RoundUpAndDown(float wallJumpInput)
{
wallJumpInput = xMovementInput;
if (wallJumpInput > 0f)
{
wallJumpInput = Mathf.Ceil(wallJumpInput);
return (int)wallJumpInput;
}
else if (wallJumpInput < 0f)
{
wallJumpInput = Mathf.FloorToInt(wallJumpInput);
return (int)wallJumpInput;
}
else
{
return 0;
}
}
void ResetMoveSpeed()
{
movementSpeed = baseMoveSpeed;
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Back Flip"))
{
canBackflip = true;
}
}
private void OnTriggerExit2D(Collider2D other)
{
if (other.CompareTag("Back Flip"))
{
canBackflip = false;
}
}
private void BackFlip()
{
if (canBackflip && Input.GetButtonDown("Jump"))
{
Debug.Log("Backflip");
}
}
}