Help with wall jump code

Godot Version



hi im new to coding and was just trying some stuff out and i made this code for wall jumping in 2d, it jumps upwards correctly but there is no movement on the x axis. so id like to know what im doing wrong.

	var collision_pos = get_last_slide_collision()
	if Input.is_action_just_pressed("ui_accept") and is_on_wall():
		velocity.y = JUMP_VELOCITY
		if collision_pos.get_position()[0] > int(sprite_2d.global_position[0]):
			velocity.x = -500
			await $Timer.timeout
			velocity.x = 500
			await $Timer.timeout

Can you use the code block to format your code it’s three back ticks on the tilde key.




I would suggest not using the is_on_wall() function :face_with_symbols_over_mouth: - because the user has to have a force acting on it to get onto the wall either by pressing the arrow key into the wall, and there are times when it just plain doesn’t work. I could never get the function to work by jumping to the wall.

I would show you what I used to solve this but my code is pretty horrible and hard to read and I have ladders and Ice parts and changing of animations all over the place. So, I really suggest watching this video which is what my solution was based on and will work 100% of the time:

I had another video which was useless using that stupid is_on_wall() function :face_with_symbols_over_mouth:. This is much better and pretty simple idea. Mind you this video uses states which I didn’t use, so, depending of your understanding of coding you usage may vary. Even though this was made for Godot 3.4, I had no problems with 4.2.2, well, I lie I had problems with getting the animations correct, but, not the wall-jumping itself. I also ditched the idea of wall-sliding for wall-climbing.



1 Like

ty i was wondering how to do that

Is your move_and_slide() in the place it is shown here? Where it stands, This won’t act on your x axis update for the wall jump.

I wonder if your input code for left and right movement is overriding this on the next process cycle.

move_and_slide is usually the last line in the physics process unless you are using is_on_wall() :face_with_symbols_over_mouth:. Stupid function, another reason to get rid off it.

In any case I just realized that I do have my character code on the forum, so, if you want to knock yourself out, it is here:

Just realize there are a LOT of booleans being set and checked against so I know which item the character is on. It’s not the best code, but, it’s the best I can do for now.


1 Like

I was playing around with it, and @celticknight is right about wall detection…

Anyway here is my code, I did have some trouble with how velocity.x is managed. Here is my attempt.

1 Like

i move the move_and_slide() after the code and the wall jump code comes after the left right movement
and it still doesnt work.

i also tried the get_wall_normal code and it does move on the x axis but not for very long and i cant seem to get the timer to work so it moves in the x axis for a certain amount of time

Yea, it works best if you jump push a direction into the wall. Let go of direction and jump. Then you should be able to just keep jumping if you have a walled area.

It definitely needs some tuning. I think you can start by removing air movement.

I was mainly trying to show you how to manage the x axis. Just be aware that you will override the character movement if you simply say velocity.x = direction * SPEED

But in the end you should probably disable direction movement after a wall jump for a small period of time.

Also don’t use await in a process function. Do something like this with a signal connection.

var movement_enabled : bool = true
# after wall jump
movement_enabled = false
Timer.start() # or restart timer if multiple wall jumps
Timer.timeout.connect(func (): movement_enabled = true )

Or better yet, get an animation tree with a state machine to do the timeout for you

For the wall detection code I would start with at the beginning of the physics_process with this:

func _physics_process(delta):

Then create the function that does the checking:

## function implementing raycasting
## called: is_near_wall
func is_near_wall():
	return wallchecker.is_colliding()

Then of course to get that working you need a raycaster on your character sprite ala this:

That should return a true when you run into a wall made from tiles. Possibly a wall made from a sprite like I used can’t remember(???). In case you are wondering this is one frame of the wall_climbing animation. Notice the wallchecker is just one pixel wider than the sprite detection area yet shorter than the whole sprite to give the feeling of attachment to the wall.

Since the wallchecker is only facing to the right you will need to flip it in order to face to the left so in your direction code when you are going right you need to have:

  • wallchecker.scale.x = 1;

and left:

  • wallchecker.scale.x = 1;

And then it gets interesting!