Player 3D and 2D Wall Jump Tutorial for Unity

Wall jumping, a mechanic prevalent in platformers and action games, allows players to leap off walls, adding complexity and versatility to navigation. This tutorial aims to integrate wall jumping into the provided FPS Controller. By the end, the player character will be able to execute wall jumps in both 2D and 3D settings.

Implementing Wall Jump for 3D using the 'SC_FPSController' script

Starting with the given base code from the Unity FPS Controller tutorial, we'll introduce the wall jump mechanic.

Step 1: Setting Up Wall Detection

  • Add a new layer in Unity named "Wall" for objects that can be wall-jumped off of.
  • Update the 'SC_FPSController' class, which you can get from our Unity FPS Controller tutorial, with the following variables:
public LayerMask wallLayer;
public float wallJumpForce = 10f;
private bool isTouchingWall = false;

Step 2: Detecting the Wall

  • Incorporate the following logic inside the 'Update()' method to detect a wall:
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.right, out hit, 1f, wallLayer) || Physics.Raycast(transform.position, -transform.right, out hit, 1f, wallLayer))
{
    isTouchingWall = true;
}
else
{
    isTouchingWall = false;
}

This raycast in the code above will detect walls to the left and right of the player.

Step 3: Implementing the Wall Jump for 3D

  • Within the 'Update()' method, after the ground jump logic, insert:
else if (Input.GetButton("Jump") && canMove && isTouchingWall)
{
    moveDirection.y = wallJumpForce;

    // This adds a bit of horizontal force opposite the wall for a more dynamic jump
    if (Physics.Raycast(transform.position, transform.right, 1f, wallLayer))
    {
        moveDirection += -transform.right * wallJumpForce / 2.5f; // Adjust the divisor for stronger/weaker push.
    }
    else if (Physics.Raycast(transform.position, -transform.right, 1f, wallLayer))
    {
        moveDirection += transform.right * wallJumpForce / 2.5f;
    }
}

This logic in the code above checks if the player is pressing the jump button, can move, and is touching a wall. If so, it imparts a vertical force and a slight horizontal force away from the wall.

Implementing Wall Jump for 2D using the 'CharacterController2D' script

Starting with the given base code from the 2D Character Controller for Unity tutorial, we'll introduce the wall jump mechanic.

Step 1: Setting Up Wall Detection

  • Add a new layer in Unity named "Wall" for objects that can be wall-jumped off of.
  • Update the 'CharacterController2D' class, which you can get from our 2D Character Controller for Unity tutorial, with the following variables:
public LayerMask wallLayer;
public float wallJumpForce = 10f;
private bool isTouchingWall = false;

Step 2: Detecting the Wall in 2D

  • Incorporate the following logic inside the 'Update()' method to detect a wall:
RaycastHit2D wallHitLeft = Physics2D.Raycast(transform.position, Vector2.left, 1f, wallLayer);
RaycastHit2D wallHitRight = Physics2D.Raycast(transform.position, Vector2.right, 1f, wallLayer);

if (wallHitLeft.collider != null || wallHitRight.collider != null)
{
    isTouchingWall = true;
}
else
{
    isTouchingWall = false;
}

Step 3: Implementing the Wall Jump for 2D

  • Within the 'Update()' method, after the ground jump logic, insert:
if (Input.GetKeyDown(KeyCode.W) && isTouchingWall && !isGrounded)
{
    r2d.velocity = new Vector2(r2d.velocity.x, jumpHeight);
    if (wallHitLeft.collider != null)
    {
        r2d.AddForce(new Vector2(wallJumpForce, 0)); // Push to the right when wall is on the left
    }
    else if (wallHitRight.collider != null)
    {
        r2d.AddForce(new Vector2(-wallJumpForce, 0)); // Push to the left when wall is on the right
    }
}

Questions Addressed:

  1. How can a character detect a wall in Unity?: A character can detect walls using raycasting. By shooting an invisible ray from the player in the desired direction, one can determine if an object, like a wall, intersects with this ray.
  2. What is the difference between 2D and 3D wall jump implementation in Unity?: In Unity, 2D physics uses 'Physics2D' methods (like 'Physics2D.Raycast()'), while 3D physics uses 'Physics' methods (like 'Physics.Raycast()'). The logic remains relatively similar, but methods and directions (Vector2 for 2D, Vector3 for 3D) vary.

Conclusion

This addition enables the character in the 2D space to jump off walls. Experimentation with the force values might be necessary to ensure smooth gameplay. By integrating these steps, players will find the 2D character's movement to be more versatile and dynamic in environments with walls.

Suggested Articles
Top-Down Player Controller Tutorial for Unity
RTS and MOBA Player Controller for Unity
Flashlight Tutorial for Unity
Adding Double Jump Support to a 2D Platformer Character Controller in Unity
Implementing Parkour System in Unity
3D Worm Controller Tutorial for Unity
Rigidbody-based Planetary Player Controller for Unity