How To Do Wall Sliding & Wall Jump In Unity 2D

Wall sliding and wall jump in unity are popular mechanics that add depth to platformer games. In this tutorial, we’ll guide you through the process of implementing these features. Implementing a wall jump in unity is quite an easy task for me as I have made a platformer in the past.

So, this article is divided into a few sections, the first sections is about setting up the environment like making a separate layer for the wall after that the second section is implementing wall sliding in unity and then, the third we will implement wall jumping in unity.

Implementing Wall Jump In Unity

Difference Wall Jumping Between Wall Sliding

But first, we should know the difference between wall jumping and wall sliding before implementing it in our game.

What Is Wall Sliding?

Wall sliding is you guessed it when the player jumps to the wall and just sticks to it then starts to slide down, if you implement wall sliding it just does not automatically make the player be able to jump while the player is on the wall.

Because remember you must have implemented ground check in unity and if you have not implemented ground check yet then implement it know. The player will only be able to jump when he is on the ground.

What Is Wall jumping?

Wall jumping is when the player is able to jumping while he is sticking and sliding down the wall and implementing this is pretty easy if you ask me. But making the feel of the jump smooth is not very easy you have to find the right values to do so.

Implementing Wall Sliding & Jumping

This whole implementation will be divided into three sections:

  1. Setting Up The Environment
  2. Implementing Wall Sliding In Unity
  3. Implementing Wall Jump In Unity

1. Setting Up The Environment

So, lets start by setting up the environment.

i. Set Up A New Layer

Lets set up a new layer so that we can differentiate in between collision with the ground and collision with wall.

So, make a new layer named “Wall”.

make a new layer named wall

Then assign that layer to the wall Tilemap and in case you are not using it, consider making a Tilemap for your terrain and walls.

assign wall layer to the wall game object

ii. Create Wall Check GameObject

One last thing to do in this process of setting up the environment is to make a empty game object named ‘Wall Check” and place it to the right of the player.

This empty game object will help us detecting whether we are in contact of a wall or not.

create empty game object for wall check

Now, that the environment is all setup lets move on to the next step.

2. Implementing Wall Sliding In Unity

wall sliding in unity

i. Create Variables

Lets make four variables, one for checking if we are wall sliding, other to reference the wall check empty game object, the third one to determine the wall sliding speed and the fourth one to reference the wall layer.

[SerializeField] private Transform wallCheck;
private bool isWallSliding;
[SerializeField] private float wallSlidingSpeed;
[SerializeField] private LayerMask wallLayer;

ii. Make Function To Detect Wall Touch

To slide when we come in contact with a wall, it is important to know whether we are in contact with a wall and to do so lets make a function that return true when we come in contact with a game object with the layer “Wall” assigned to it.

Open the player controller script and make a IsTouchingWall function.

private bool isTochingWall()
{
    return Physics2D.BoxCast(wallCheck.position, new Vector2(1.6f, 0.1f), 0f,Vector2.right, 0.1f,  wallLayer);
}

The box cast function makes an invisible box in the position of the wall check game object and when that invisible box overlaps with the wall it returns true.

Note: You might need to change the size of the box depending on the size of your player.

iii. Using The isTouchingWall Function

Now, to utilize this isTouchingWall function that we just created lets use an if statement which will set the isWallSliding variable to true when the isTouchingWall function returns true, the isGrounded function returns false and the horizontal input variable is not equal to zero.

void update() {
    if (isTouchingWall() && !isGrounded() && horizontalInput != 0) {
        isWallSliding = true;
    } else {
        isWallSliding = false;
    }
}

iv. Making The Player Slide Down

Now, we are going to do a physics related operation so go in the fixed update method, and if the isWallSliding variable is true then we will clamp the rigidbody velocity’s y-axis value between negative wall sliding speed variable and float.maxvalue.

void FixedUpdate() 
{
    if (isWallSliding) 
    {
        rb.velocity = new Vector2(rb.velocity.x, Mathf.Clamp(rb.velocity.y, -wallSlidingSpeed, float.MaxValue));
    }
}

Your player should now be able to slide down a wall if you did everything right.

Note: If you have any bugs do remember to write debug statements everywhere to detect where the bug is present.

Implementing Wall Jump In Unity

wall jump in unity

i. Create Few More Variables

To implement wall jumping in unity, we’ll have to create a four variables.

One for wall jump duration, one for wall jump force and one for checking if the player is jumping right now or not.

[SerializeField] private float wallJumpDuration;
[SerializeField] private Vector2 wallJumpForce;
private bool isWallJmping;

ii. Create Stop Wall Jump Function

Let’s now create a function for stopping wall jump, we’ll invoke this function when we the wall jump duration ends.

private void StopWallJump()
{
    isWallJumping = false;
}

iii. Implementing The Jump Logic

Now, go in the update function and check if isSliding is equal to true then set wall jumping to true and invoke the stop wall jump function that we just created after the wall jump duration has passed.

if (isWallSliding && Input.GetButtonDown("Jump"))
{
    isWallJumping = true;
    Invoke("StopWallJump", wallJumpDuration);
}

Now, go in the fixed update function (because we are doing physics related operations) and check if wall jumping is true then change the player’s rigidbody’s velocity.

void FixedUpdate() 
{
    if (isWallJumping) 
    {
        rb.velocity = new Vector2(-horizontalInput * wallJumpForce.x, wallJumpForce.y);
    }
}

And it’s done.

Conclusion

You have now successfully created sliding on walls as well as jumping while sliding on walls in unity. This tutorial should give you a firm understanding on how these features we’re implemented.

And now you will be able to implement these features for your platformer. But do remember to experiment with values and if any bugs do occur then use print statements to keep record of the code and track the bugs.