Hop across from walls and ceilings too

Holding away from a wall or ceiling for MAX_SECS_TO_UNSTICK, while not
moving along it, now hops straight across to a surface within
HOP_MAX_HEIGHT on that side and sticks to it. With nothing in reach (or
while moving along the surface) it lets go as before.

- Wall hops go at crawling speed on an arc that lands at the same height,
  with no air steering until they land.
- Ceiling hops push down onto the floor below.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
SnekInAScarf 2026-10-31 17:16:48 -07:00
parent 4b96d21add
commit e8cf70e6e5

View file

@ -1,5 +1,8 @@
extends CharacterBody2D
# Note: should check if i want unsticking to occur while moving on ceiling or wall.
# so far it isn't the case with ground movement.
const GROUND_SPEED = 150.0
const AIR_SPEED = 75.0
@ -40,10 +43,13 @@ const MAX_SECS_TO_UNSTICK = 0.5
# Hop: holding up (and not left/right) on the floor for MAX_SECS_TO_UNSTICK hops just high enough
# to reach a ceiling up to HOP_MAX_HEIGHT above us (measured from our top).
# With no ceiling in reach it's just a little HOP_NO_CEILING_HEIGHT hop.
# Walls and ceilings work the same way with the direction away from them, but
# with nothing in reach we let go as usual instead of doing a little hop.
const HOP_MAX_HEIGHT = 32.0
const HOP_NO_CEILING_HEIGHT = 16.0
const HOP_OVERSHOOT = 2.0 # Aim slightly past the ceiling so we still hit it with some speed.
var timeSpentHoldingUpOnFloor := 0.0
var isHoppingAcross := false # Hopping from a wall to the one opposite: no air steering until we land.
var isStickingToWall := false
var timeSpentUnstickingWall = 0.0
@ -129,6 +135,7 @@ func _physics_process(delta: float) -> void:
return
var wasInAir := _get_surface_normal() == Vector2.ZERO and not isClimbingLip
if not wasInAir: isHoppingAcross = false
# Hanging on a lip and pressing up: swing around onto the wall above.
var wantsToClimb := verticalInput > 0 or timeSinceUpPressed <= CLIMB_INPUT_BUFFER
@ -154,7 +161,11 @@ func _physics_process(delta: float) -> void:
else: timeSpentUnstickingCeiling = 0.0
if timeSpentUnstickingCeiling >= MAX_SECS_TO_UNSTICK:
isStickingToCeiling = false
velocity.y = get_gravity().y * delta
# Standing still with a floor within reach below: hop down onto it.
# Otherwise just let go.
var floorDistance := -1.0 if horizontalInput else _get_hop_distance(Vector2.DOWN)
if floorDistance >= 0.0: velocity.y = sqrt(2.0 * get_gravity().y * floorDistance)
else: velocity.y = get_gravity().y * delta
else: velocity.y = CEILING_STICK_SPEED
# Move up and down along a wall when sticking to it.
elif isStickingToWall:
@ -183,7 +194,11 @@ func _physics_process(delta: float) -> void:
if pressingAwayFromWall and not ignoreWallUnstickUntilRelease: timeSpentUnstickingWall += delta
else: timeSpentUnstickingWall = 0.0
if timeSpentUnstickingWall >= MAX_SECS_TO_UNSTICK:
velocity.x = horizontalInput * GROUND_SPEED
# Not climbing, with a wall within reach opposite: hop across to it.
# Otherwise just let go.
var wallDistance := -1.0 if verticalInput else _get_hop_distance(Vector2(wallNormalX, 0))
if wallDistance >= 0.0: _hop_across(wallDistance)
else: velocity.x = horizontalInput * GROUND_SPEED
else:
velocity.x = -wallNormalX * WALL_STICK_SPEED
# Otherwise move horizontally based on what we're on.
@ -195,7 +210,7 @@ func _physics_process(delta: float) -> void:
velocity.x = -lipDirection * WALL_STICK_SPEED if verticalInput > 0 else 0.0
elif is_on_floor() or isStickingToCeiling:
velocity.x = _accelerate(velocity.x, horizontalInput * GROUND_SPEED)
else:
elif not isHoppingAcross:
velocity.x = _air_steer(velocity.x, horizontalInput)
_hold_at_edge(delta)
@ -255,10 +270,24 @@ func _physics_process(delta: float) -> void:
# How high to hop: up to the ceiling above us if there's one in reach.
func _get_hop_height() -> float:
var hit := move_and_collide(Vector2(0, -HOP_MAX_HEIGHT), true)
if hit and hit.get_normal().y > 0.5:
var distance := _get_hop_distance(Vector2.UP)
return distance if distance >= 0.0 else HOP_NO_CEILING_HEIGHT
# How far to hop in direction to reach a surface facing us within
# HOP_MAX_HEIGHT (plus HOP_OVERSHOOT), or -1 if there isn't one.
func _get_hop_distance(direction: Vector2) -> float:
var hit := move_and_collide(direction * HOP_MAX_HEIGHT, true)
if hit and hit.get_normal().dot(-direction) > 0.5:
return hit.get_travel().length() + HOP_OVERSHOOT
return HOP_NO_CEILING_HEIGHT
return -1.0
# Hops from the wall we're on to the one distance away opposite, at crawling
# speed, on an arc that lands at the same height we left from.
func _hop_across(distance: float) -> void:
var flightTime := distance / GROUND_SPEED
velocity.x = wallNormalX * GROUND_SPEED
velocity.y = -0.5 * get_gravity().y * flightTime
isHoppingAcross = true
# Moves speed towards target: GROUND_ACCEL when speeding up, GROUND_FRICTION
# when slowing down or stopping.