Wall Jumping Help!

Godot Version

4.2.1

Question

How do I fix this script to make it allow me to wall jump?

I’m relatively new to Godot and I want to make a platformer… a test one at last and I want to make the character do a wall jump. I followed one tutorial and the Godot Docs but got nowhere. I even tried Reddit. So, here’s the script for it. Help will be much appreciated!

extends CharacterBody2D

@export var speed = 400.0
@export var jump_speed = -1400.0
@export var wall_jump_push = 100

var gravity = 120
var wall_slide_gravity = 100
var is_wall_sliding = false
	
func _physics_process(delta):
	
	var collision = move_and_collide(velocity * delta)
	if collision:
		Input.get_axis("ui_up", "ui_down")
		
	var input_direction = Input.get_axis("ui_left", "ui_right")
	velocity.x = input_direction * speed
	
	move_and_slide()
	jump()
	wall_slide(delta)

func jump():
	velocity.y += gravity
	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = jump_speed
		
func wall_jump():
	if is_on_wall() and Input.is_action_pressed("jump"):
		gravity = 75.0
	else:
		jump()
		

func wall_slide(delta):
	if is_on_wall() and !is_on_floor():
		if Input.is_action_pressed("ui_left") or Input.is_action_pressed("ui_right"):
			is_wall_sliding = true
		else:
			is_wall_sliding = false
	else:
		is_wall_sliding = false
		
	if is_wall_sliding:
		velocity.y += (wall_slide_gravity * delta)
		velocity.y = min(velocity.y, wall_slide_gravity)

hmm ok, first… the function func wall_jump(): is never called - that is probably the major problem.

I’t looks like it is coded to replace the call for jump()
like:

	move_and_slide()
	wall_jump()
	wall_slide(delta)

Now you have wall_jump() called each physics step, but from what I see there is no code here making the actual wall jump.

func wall_jump():
	if is_on_wall() and Input.is_action_pressed("jump"):
		gravity = 75.0
		# character never jumped - I thought one wanted to jump here
		# gravity was changed - was that intentional? if so - remember to change it  back
	else:
		jump()

Can you please elaborate? What exactly would I put here:

func wall_jump():
	if is_on_wall() and Input.is_action_pressed("jump"):
		gravity = 75.0
	else:
		jump()

I know obviously I put the code I want to do the wall jump with but when I make them jump, it doesn’t do much. Do I make it so it pushes me forward if I’m on the wall on the X-axis?

Do I make it so it pushes me forward if I’m on the wall on the X-axis?

Yes, if you apply velocity to the x-axis it will push you sideways, and y-axis will be up/down.

Looking at jump() you apply a force on y-axis. A negative force, which is upwards.
velocity.y = jump_speed

I recommend you remove the gravity = 75.0 and replace it with velocity.y = jump_speed. The sideway “forward” movement will come from input as you allow the character to move while airborne.