Depending on what you’ve done with the physics, you might not be “on the wall” very long or at all. The floor has gravity constantly pushing you towards it, but the wall does not. You could be bouncing off it or something.
My advice would be to add something like this to your code:
var player_sprite = # whatever your player sprite is...
[...]
func _process_physics(delta):
var flag: int = 0
if is_on_wall(): flag |= 1
if is_on_floor(): flag |= 2
match(flag)
0: player_sprite.self_modulate = Color.WHITE
1: player_sprite.self_modulate = Color.BLUE
2: player_sprite.self_modulate = Color.RED
3: player_sprite.self_modulate = Color.Magenta
[...]
That should tint your player sprite based on whether they’re on the wall or the floor. Visual stuff like this can help you debug things that are happening over time. I bet you’ll find the sprite only flashes blue for a single frame, or it never turns blue at all due to unembedding.
You could also potentially add some line2d debug lines to your sprite to show velocity and the like.
What you may wind up needing to do is, if you detect you’re on the wall, apply some small amount of force towards the wall (which would just be whatever direction you were going when you hit it…) to keep you stuck to it rather than unembedding/bouncing off.
But my advice would be first, put some visual debugging in place and see if you can get a sense of what’s actually happening.
ok so, I have done debug and I think I fixed it but now the character is directly not jumping on the wall, before, it was jumping on the wall but as many times as I wanted and in the same direction as the wall like climbing it, tried to fix that so it wall jumps to the other direction and now it doesnt work.
I have no idea of codding so please help me on this TwT
extends CharacterBody2D
@export var walk_speed = 5.0
@export var run_speed = 15.0
@export var jump_power = 10.0
@export_range(0.0, 1.0) var acceleration = 1.0
@export_range(0.0, 1.0) var deceleration = 1.0
var speed_multiplier = 20.0
var jump_multiplier = -25.0
var direction = 0
var last_jump_direction = 0
#----------------------------------------------#
#Wall Slide
var friction = 70.0
var wall_jump_pushback = 100.0
var wall_slide_gravity = 100.0
var is_wall_sliding = false
#----------------------------------------------#
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
func _physics_process(delta):
# Add the gravity
if is_on_floor():
last_jump_direction = 0
if not is_on_floor():
velocity.y += gravity * delta
# Handle Jump
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = jump_power * jump_multiplier
var speed
if Input.is_action_pressed("run"):
speed = run_speed
else:
speed = walk_speed
if is_on_wall():
if direction != last_jump_direction and Input.is_action_pressed("jump") and ((Input.is_action_pressed("move_left") and direction == 1.0) or (Input.is_action_pressed("move_right") and direction == -1.0)):
last_jump_direction = direction
velocity.x = wall_jump_pushback * speed_multiplier * -direction
velocity.y = jump_power * jump_multiplier
wall_slide(delta)
# Get the input direction
var direction = Input.get_axis("move_left", "move_right")
if direction:
velocity.x = move_toward(velocity.x, direction * speed * speed_multiplier, speed * speed_multiplier * acceleration)
else:
velocity.x = move_toward(velocity.x, 0, deceleration * speed_multiplier)
move_and_slide()
func wall_slide(delta):
if is_on_wall() and !is_on_floor():
if Input.is_action_pressed("move_left") or Input.is_action_pressed("move_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)
One of the things here is that you have two direction variables; one declared globally, one declared just below # Get the input direction where you make a newvar direction which is temporary and will be discarded at the end of the function. That may not be what you want.
You might also need to consider what’s happening with is_on_wall() in combination with if direction != last_jump.... If everything evaluates true for more than one frame, all sorts of chaos can ensue. If it is triggering over multiple frames and oscillating or something, you can always add a cooldown timer:
if jump_cooldown > 0: jump_cooldown -= 1
if is_on_wall():
if !jump_cooldown:
if [walljump stuff]:
jump_cooldown = 6 # Six update cooldown.
[...]
could you give me an example of the code fixed? Tbh I have no idea what the hell I gotta do to fix it lol. Like its my first day programming and I have no idea whats failing and all you are explaining is very confusing for me.