I have implemented a Wall Slide mechanic and I am trying to implement a Wall Jump mechanic. It gets into the Wall Jump function and will jump me up the wall on my y-axis although it does not propel me on my x-axis. I have tried different lines of code and they all do the same thing (occasionally changes my x velocity and always changes my y). Does anyone know the fix for this?
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[Header("Player Object Reference")]
private Rigidbody2D body;
private Animator anim;
[Header("Horizontal")]
[SerializeField] private float runSpeed = 3;
[Header("Vertical")]
[SerializeField] private float jumpHeight = 5;
[Header("Ground Check")]
[SerializeField] private Transform groundCheck;
[SerializeField] private LayerMask groundLayer;
[Header("Wall Check")]
[SerializeField] private Transform wallCheck;
[SerializeField] private LayerMask wallLayer;
//Wall Slide
private bool isWallSliding;
private float wallSlidingSpeed = 2f;
//Wall Jump
private Vector2 wallJumpingPower = new Vector2(8f, 5f);
private float wallJumpingCooldown = 0.5f;
private float nextWallJump = 0f;
//Private Variables
private float horizontalInput = 0f;
private bool isFacingRight = true;
private void Awake()
{
body = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
horizontalInput = Input.GetAxis("Horizontal");
body.velocity = new Vector2(horizontalInput * runSpeed, body.velocity.y);
//Flip Body
if (horizontalInput > 0.01f)
{
transform.localScale = new Vector2(2, 2f);
isFacingRight = true;
}
else if (horizontalInput < -0.01f)
{
transform.localScale = new Vector2(-2f, 2f);
isFacingRight = false;
}
//Jump
Jump();
//Double Jump
//Wall Slide
WallSlide();
//Wall Jump
WallJump();
//Animations
anim.SetBool("run", horizontalInput != 0);
anim.SetBool("grounded", isGrounded());
}
private void Jump()
{
if (Input.GetKey(KeyCode.Space) && isGrounded()) {
body.velocity = new Vector2(body.velocity.x, jumpHeight);
anim.SetTrigger("jump");
}
}
private bool isGrounded()
{
return Physics2D.OverlapCircle(groundCheck.position, 0.1f, groundLayer);
}
private bool isOnWall()
{
return Physics2D.OverlapCircle(wallCheck.position, 0.1f, wallLayer);
}
//private bool isFalling()
//{
// if (!isOnWall() && !isGrounded() && body.velocity.y > -0.1f)
// {
// return true;
// }
// return false;
//}
private void WallSlide()
{
if (isOnWall() && !isGrounded() && horizontalInput != 0f)
{
isWallSliding = true;
body.velocity = new Vector2(body.velocity.x, Mathf.Clamp(body.velocity.y, -wallSlidingSpeed, float.MaxValue));
}
else
{
isWallSliding = false;
}
}
private void WallJump()
{
if (isOnWall() && !isGrounded() && isWallSliding && Input.GetKeyDown(KeyCode.Space) && Time.time > nextWallJump)
{
// Determine direction of wall jump based on player's facing direction
int wallDirection = isFacingRight ? -1 : 1;
body.velocity = new Vector2(10 * wallDirection, jumpHeight);
//Start cooldown for next wallJump
nextWallJump = Time.time + wallJumpingCooldown;
}
}
//private void DoubleJump()
//{
// if (isFalling() )
// {
// }
//}
}