• 2D
  • Fixing bugs fried my brain,is there no easy way to implement wall jump, double jump and ledge climb?

OK so i am trying to implement wall jump in my game but whenever the player jumps when he is next to a wall. He gets pushed to the opposite direction but he just goes in a line and then suddenly just drops down to the ground. When i make the y velocity increase with x velocity, he goes in a reversed L. I want him to go in an arc. Also whenever the player jumps, the jumps_made doesn't seem to be increasing . I can't implement double jump cause of this. Whenever i use the is_falling method for double jump instead of checking jumps_made, then the player is able to jump even if he is just falling and i only want him to jump after he has jumped once and is in the air. Also while using the is_falling method, if the player keeps pressing the jump button, he will keep jumping in the air. And when implementing ledge grab and ledge climb. When the player is hanging on a ledge and you press up then his x and y position will get increased by an amount which just teleports him. Is there a better way to implement ledge climb which lets me use animations too? This is what i mean: https://drive.google.com/file/d/1CCVJHlF6W5-RRwfOesG_5_qALXsS7iYL/view?usp=sharing

Hi, Adam, welcome to the forum!

The "teleportation" effect usually occurs when your character builds really high speeds, commonly a consequence of not resetting the velocity in the x's axis. Try to reset it after getting the input to notice if that fixes the problem.

For the value of jumps_made apparently not updating, I believe that exported variables, such as this one, are not updated in the inspector. Instead, for debugging I would recommend utilizing print() statements, so you can observe the values of the variable in the console. From what I could read in your code, I believe that the problem is in the if statements where the is_double_jumping is located, try to use a if instead of an elif. (I think that the condition is_falling is being satisfied, and since it is in the same if structure, the elif is not accessed).

Also, for future posts, refrain from using videos/images to show your code, they can be hard for us to read and for other users to search if they have similar problems. Instead, try to use code blocks, they can be used in the following way (without the "\" at the start and at the end) :

\```
var who =  "Godot"

# Write your code inside the triple quotes
print("Waiting for," who)
\```

@bored_paramecium said: Hi, Adam, welcome to the forum!

The "teleportation" effect usually occurs when your character builds really high speeds, commonly a consequence of not resetting the velocity in the x's axis. Try to reset it after getting the input to notice if that fixes the problem.

For the value of jumps_made apparently not updating, I believe that exported variables, such as this one, are not updated in the inspector. Instead, for debugging I would recommend utilizing print() statements, so you can observe the values of the variable in the console. From what I could read in your code, I believe that the problem is in the if statements where the is_double_jumping is located, try to use a if instead of an elif. (I think that the condition is_falling is being satisfied, and since it is in the same if structure, the elif is not accessed).

Also, for future posts, refrain from using videos/images to show your code, they can be hard for us to read and for other users to search if they have similar problems. Instead, try to use code blocks, they can be used in the following way (without the "\" at the start and at the end) :

\```
var who =  "Godot"

# Write your code inside the triple quotes
print("Waiting for," who)
\```

I fixed the double jump by myself. I just changed the variable's name and turned it into a boolean. But i still am not able to fix the wall jump. Also what do you mean by resetting the x velocity. Are you talking about the ledge grab or wall jump? About the showing code in the video: I was gonna give a link to my project so that people can understand it. But then i thought that some people are just way too lazy when they think that they gotta check the whole project (sometimes i am too when i come across a question like that with a link to the project ) and my code doesn't even has any comments in it. So i just highlighted the important parts. But if there is a problem with that. I am gonna keep that in mind next time. Also can you tell me a better way to implement ledge grab other than just teleporting the player by increasing the x and y velocity by some amount(This is how i am doing it just in case you didn't understand it by the code video).

Sorry, I did misinterpret your first post a little. When I mentioned the "teleportation" I meant the wall jump "straight line then drop " movement that was occurring, instead of the arc one.

Usually for handling physics movement you need to be careful with the velocity vector, and in platformers, be extra careful with the horizontal (back and front) movement, this horizontal movement is dictated by the "x" (first element) of the vector. In your code even though you are clamping that velocity with the max_velocity, you are only doing this before handling the input and calculating a new velocity vector, and since you are not restarting it (in other words, setting velocity.x = 0), this value accumulates a lot, resulting in the unwanted movement. Usually this calculation is done following this steps:

  1. Set velocity.x = 0
  2. Get the user input (and based on that, calculate the new velocity)
  3. Apply acceleration/ friction and clamp the velocity if necessary
  4. Apply the velocity to the body (in this case with the move_and_slide() method)

In your script you are not performing the step 1 and switching steps 3 and 2. See if fixing this helps with the wall jump movement.

For the ledge grab and climb, you could implement it utilizing Raycasts. You could add one slightly under the head of your character and other one slightly above it. In the script you could check if the lower raycast is hitting a wall while the higher one isn't. If this condition is met and your player is holding some button (or not, the character can perform an "auto" grab), then you could not update the gravity in the body (in other words, not calculate vector.y += gravity * delta), making it "grab" the ledge. Than if they press other button, you could simply perform a jump into the wall direction. I've drawn this simple diagram in paint to illustrate it:

Notice that the arrows are the raycasts and that only the "middle" character would be able to perform the "climb" (jump). If you want to see some code, I've found this video (in French) that shows more or less what I'm talking about.

I already fixed the ledge grab and climb by myself too and with the same idea you have but it came in my mind first. I am gonna try to fix the wall jump through the steps you told me. Also i don't speak french and i think i have come across that video before but couldn't understand it.

I don't get what you mean by get the user input and calculate the new velocity based on that. If i put velocity.x = 0 at the start of physics process which is responsible for the input, animations,etc and then i put the velocity.x = acceleration delta horizontal direction and the friction and clamp before the move an slide and after the input. But then whenever the left or right movement keys, the player's speed is like 2 pixels per sec. I don't get it.

One of those multiplications might have to be a division instead of a multiplication perhaps.

You will have to excuse me again, but I misread your code... I'm deeply sorry for the wrong information previously.

You are doing the steps correctly. (notice that you are basically reseting the vector when the horizontal_direction is 0). Just for sanity check before I look for something more obscure/complex, have you considered lowering the wall_jump_knockback? It's quite higher than the other values, maybe set it to 300? You also could use a lerp() function instead of the clamp() and see if it smooths the movement a little, something like:

velocity.x = lerp(velocity.x, horizontal_direction * max_speed, acceleration)

instead of

velocity.x += horizontal_direction * acceleration * delta
velocity.x += clamp(velocity.x, -max_speed, max_speed)

(There is nothing wrong with your current method, but maybe the first one will do a more smooth movement)

Also, in the if statement that checks is_jumping, maybe is it better to set velocity.y += jump_speed instead of velocity.y = jump_speed. Lastly, does it have any significant difference if you move the velocity.y += delta * gravity to be just before the move_and_slide() method?

Again, sorry for the wrong information early, check if any of this adjustments fix the movement now.

@bored_paramecium said: You will have to excuse me again, but I misread your code... I'm deeply sorry for the wrong information previously.

You are doing the steps correctly. (notice that you are basically reseting the vector when the horizontal_direction is 0). Just for sanity check before I look for something more obscure/complex, have you considered lowering the wall_jump_knockback? It's quite higher than the other values, maybe set it to 300? You also could use a lerp() function instead of the clamp() and see if it smooths the movement a little, something like:

velocity.x = lerp(velocity.x, horizontal_direction * max_speed, acceleration)

instead of

velocity.x += horizontal_direction * acceleration * delta
velocity.x += clamp(velocity.x, -max_speed, max_speed)

(There is nothing wrong with your current method, but maybe the first one will do a more smooth movement)

Also, in the if statement that checks is_jumping, maybe is it better to set velocity.y += jump_speed instead of velocity.y = jump_speed. Lastly, does it have any significant difference if you move the velocity.y += delta * gravity to be just before the move_and_slide() method?

Again, sorry for the wrong information early, check if any of this adjustments fix the movement now.

None of your ideas did anything. I have everything fixed except the stupid wall-jump. When i used lerp instead of clamp, the player just flew across the screen. The camera wasn't able to keep up, the velocity.x in the remote menu said Nan while running the game as the player was going at the speed of lightning.