2D Platformer Mechanics
Wall jump, wall slide, dash, and double jump mechanics for 2D platformers. Drop-in extension for the 2D Player Controller.
No APIs that Unity 6.0 to 6.6 made obsolete or removed. How this is checked
What it adds to your GameObject
A MonoBehaviour component: attach it to a GameObject. This is the Inspector you get, read from the source: 19 serialized fields with their defaults.
When to use it
Designed for 2D platformers like Celeste-style or Metroidvania games that need advanced movement mechanics. Drop this alongside the basic 2D Player Controller to add wall jumping, dashing, and double jumping. Each mechanic can be individually toggled, so use only what your game needs.
Setup
- Attach PlatformerMechanics2D to your player (alongside PlayerController2D)
- Create a WallCheck child transform at character's side
- Assign ground and wall check references
- Enable/disable individual mechanics in inspector
- Wall slide: hold into wall while falling
- Wall jump: press Jump while wall sliding
- Double jump: press Jump in mid-air
- Dash: press Shift (configurable) for burst speed
Features
- Wall slide with configurable slide speed when pressing into walls
- Wall jump that launches away from the wall with adjustable force vector
- Double jump (or triple+) with configurable max air jumps
- Dash with speed, duration, cooldown, and gravity override
- Ground and wall detection via Physics2D with LayerMask filtering
- Editor gizmos for wall check ray visualization
Inspector fields
Every field 2D Platformer Mechanics exposes in the Inspector, with the default it starts at.
| Field | Type | Default | Group |
|---|---|---|---|
Wall CheckwallCheck | Transform | None | Wall Detection |
Wall Check DistancewallCheckDistance | float | 0.3 | Wall Detection |
Wall LayerwallLayer | LayerMask | Nothing | Wall Detection |
Enable Wall SlideenableWallSlide | bool | true | Wall Slide |
Wall Slide SpeedwallSlideSpeed | float | 2 | Wall Slide |
Enable Wall JumpenableWallJump | bool | true | Wall Jump |
Wall Jump ForcewallJumpForce | Vector2 | (12, 16) | Wall Jump |
Wall Jump DurationwallJumpDuration | float | 0.15 | Wall Jump |
Enable Double JumpenableDoubleJump | bool | true | Double Jump |
Double Jump ForcedoubleJumpForce | float | 14 | Double Jump |
Max Air JumpsmaxAirJumps | int | 1 | Double Jump |
Enable DashenableDash | bool | true | Dash |
Dash SpeeddashSpeed | float | 25 | Dash |
Dash DurationdashDuration | float | 0.15 | Dash |
Dash CooldowndashCooldown | float | 0.8 | Dash |
Dash KeydashKey | KeyCode | LeftShift | Dash |
Ground CheckgroundCheck | Transform | None | Ground Check |
Ground Check RadiusgroundCheckRadius | float | 0.2 | Ground Check |
Ground LayergroundLayer | LayerMask | Nothing | Ground Check |
Public API
What other scripts can call on PlatformerMechanics2D.
| Member | Returns | Description |
|---|---|---|
IsWallSliding property | bool | Is the character currently wall sliding? |
IsDashing property | bool | Is the character currently dashing? |
Requirements
- Requires Rigidbody2D on the same GameObject. Unity adds it automatically when you attach the script.
- Reads the legacy Input Manager (UnityEngine.Input). If Project Settings > Player > Active Input Handling is set to Input System Package only, Unity throws an InvalidOperationException; set it to Both.
Common mistakes
You must create a wallCheck child Transform positioned at the character's side edge — without it, wall detection won't work. The wallLayer and groundLayer must be set correctly in the Inspector; if they overlap with the player's own layer, you'll get false positives. The dash temporarily sets gravityScale to 0, so don't modify gravity from another script during a dash.
Source code
PlatformerMechanics2D.cs, 182 lines, MIT licensed. Paste it into a new C# file of the same name.
using UnityEngine;
/// <summary>
/// Advanced 2D platformer mechanics: wall slide, wall jump, dash, and double jump.
/// Works alongside PlayerController2D.
/// </summary>
[RequireComponent(typeof(Rigidbody2D))]
public class PlatformerMechanics2D : MonoBehaviour
{
[Header("Wall Detection")]
[SerializeField] private Transform wallCheck;
[SerializeField] private float wallCheckDistance = 0.3f;
[SerializeField] private LayerMask wallLayer;
[Header("Wall Slide")]
[SerializeField] private bool enableWallSlide = true;
[SerializeField] private float wallSlideSpeed = 2f;
[Header("Wall Jump")]
[SerializeField] private bool enableWallJump = true;
[SerializeField] private Vector2 wallJumpForce = new Vector2(12f, 16f);
[SerializeField] private float wallJumpDuration = 0.15f;
[Header("Double Jump")]
[SerializeField] private bool enableDoubleJump = true;
[SerializeField] private float doubleJumpForce = 14f;
[SerializeField] private int maxAirJumps = 1;
[Header("Dash")]
[SerializeField] private bool enableDash = true;
[SerializeField] private float dashSpeed = 25f;
[SerializeField] private float dashDuration = 0.15f;
[SerializeField] private float dashCooldown = 0.8f;
[SerializeField] private KeyCode dashKey = KeyCode.LeftShift;
[Header("Ground Check")]
[SerializeField] private Transform groundCheck;
[SerializeField] private float groundCheckRadius = 0.2f;
[SerializeField] private LayerMask groundLayer;
private Rigidbody2D rb;
private int airJumpsRemaining;
private bool isWallSliding;
private bool isWallJumping;
private float wallJumpTimer;
private int wallDirection;
private bool isDashing;
private float dashTimer;
private float dashCooldownTimer;
private bool isGrounded;
private float originalGravityScale;
/// <summary>Is the character currently wall sliding?</summary>
public bool IsWallSliding => isWallSliding;
/// <summary>Is the character currently dashing?</summary>
public bool IsDashing => isDashing;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
originalGravityScale = rb.gravityScale;
}
private void Update()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);
if (isGrounded)
airJumpsRemaining = maxAirJumps;
CheckWall();
HandleWallSlide();
HandleWallJump();
HandleDoubleJump();
HandleDash();
}
private void CheckWall()
{
float moveInput = Input.GetAxisRaw("Horizontal");
bool isTouchingWall = false;
if (wallCheck != null && Mathf.Abs(moveInput) > 0.1f)
{
isTouchingWall = Physics2D.Raycast(
wallCheck.position,
Vector2.right * Mathf.Sign(moveInput),
wallCheckDistance,
wallLayer
);
wallDirection = (int)Mathf.Sign(moveInput);
}
isWallSliding = enableWallSlide && isTouchingWall && !isGrounded && rb.linearVelocity.y < 0f;
}
private void HandleWallSlide()
{
if (isWallSliding)
{
rb.linearVelocity = new Vector2(rb.linearVelocity.x, -wallSlideSpeed);
}
}
private void HandleWallJump()
{
if (isWallJumping)
{
wallJumpTimer -= Time.deltaTime;
if (wallJumpTimer <= 0f)
isWallJumping = false;
}
if (enableWallJump && isWallSliding && Input.GetButtonDown("Jump"))
{
isWallJumping = true;
wallJumpTimer = wallJumpDuration;
rb.linearVelocity = new Vector2(
-wallDirection * wallJumpForce.x,
wallJumpForce.y
);
// Flip character away from wall
transform.localScale = new Vector3(-wallDirection, 1f, 1f);
}
}
private void HandleDoubleJump()
{
if (!enableDoubleJump) return;
if (Input.GetButtonDown("Jump") && !isGrounded && !isWallSliding && airJumpsRemaining > 0)
{
rb.linearVelocity = new Vector2(rb.linearVelocity.x, doubleJumpForce);
airJumpsRemaining--;
}
}
private void HandleDash()
{
if (!enableDash) return;
if (dashCooldownTimer > 0f)
dashCooldownTimer -= Time.deltaTime;
if (isDashing)
{
dashTimer -= Time.deltaTime;
if (dashTimer <= 0f)
{
isDashing = false;
rb.gravityScale = originalGravityScale;
}
return;
}
if (Input.GetKeyDown(dashKey) && dashCooldownTimer <= 0f)
{
isDashing = true;
dashTimer = dashDuration;
dashCooldownTimer = dashCooldown;
float direction = transform.localScale.x;
rb.linearVelocity = new Vector2(direction * dashSpeed, 0f);
rb.gravityScale = 0f;
}
}
#if UNITY_EDITOR
private void OnDrawGizmosSelected()
{
if (wallCheck != null)
{
Gizmos.color = Color.blue;
Gizmos.DrawLine(wallCheck.position, wallCheck.position + Vector3.right * wallCheckDistance);
Gizmos.DrawLine(wallCheck.position, wallCheck.position + Vector3.left * wallCheckDistance);
}
}
#endif
}