How to implement wall sliding because tutorials seem difficult to implement with my code

Godot Version

4.3

Question

I am trying to implement wall sliding into my code but I don’t properly know how to do it, I’ve looked at quite a few tutorials but I don’t know how to implement it as the way I’m calculating the jump is different

Here’s my code:

extends CharacterBody2D 

#movement
const speed = 550
@export var acc = 50
@export var dacc = 40
var input_dir: float

#jumping
@export var jump_height : float 
@export var jump_time_to_peak : float
@export var jump_time_to_descent : float
@onready var jump_velocity : float = ((2 * jump_height) / jump_time_to_peak) * -1
@onready var jump_gravity : float = ((-2 * jump_height) / (jump_time_to_peak * jump_time_to_peak)) * -1
@onready var fall_gravity : float = ((-2 * jump_height) / (jump_time_to_descent * jump_time_to_descent)) * -1
@onready var jump_buffer_timer = $jump_buffer_timer
@onready var coyote_timer = $coyote_timer
var buffered_jump = false
var is_jumping = false

#wall sliding and jumping
var wall_jump_pushback =  400


func _physics_process(delta):
	if !is_on_floor():
		velocity.y += GetGravity()*delta
	
	input_dir = Input.get_axis("left", "right") 
	if input_dir != 0.0:
		velocity.x = move_toward(velocity.x, speed * input_dir, acc) 
	else:
		velocity.x = move_toward(velocity.x, 0.0, dacc)
	
	if Input.is_action_just_pressed("jump"):
		buffered_jump = true
		jump_buffer_timer.start()
	if Input.is_action_just_pressed("jump") or buffered_jump == true: 
		jump()
	
	if Input.is_action_just_released("jump") and velocity.y < 0:
		velocity.y = velocity.y * 0.2 
	
	var was_on_floor = is_on_floor()
	
	move_and_slide()
	
	if was_on_floor and !is_on_floor():
		coyote_timer.start() 
		


func GetGravity() -> float: 
	if velocity.y < 0.0:
		return jump_gravity
	if is_on_ceiling() == true:
		return fall_gravity
	else:
		return fall_gravity

func jump():
	if is_on_floor() or !coyote_timer.is_stopped() and !velocity.y < 0:
		velocity.y = jump_velocity
	elif is_on_wall() and input_dir != 0:
		velocity.y = jump_velocity
		velocity.x = wall_jump_pushback * -input_dir

func _on_jump_buffer_timer_timeout() -> void:
		buffered_jump = false

What’s wrong with the code you’ve pasted? Can you explain what you expect to happen vs what really happens? Keep in mind “wall sliding” is a pretty open-ended feature that could mean different things to different people.

1 Like

there’s nothing wrong I’m just curious as to how I should go about setting up wall sliding, as in slowing down while falling next to a wall

I suppose you would want to check if they are on a wall before applying gravity, though you could add it to your own GetGravity function

func GetGravity() -> float: 
	if velocity.y < 0.0:
		return jump_gravity
	elif is_on_ceiling() == true:
		return fall_gravity
	elif is_on_wall():
		return fall_gravity / 2.0
	else:
		return fall_gravity
2 Likes

Thank you so much, that’s much easier than I expected