CharacterBody2D one part of code is overwriting the transform on the x axis and cant figure out which line :D

Godot Version

4.3

Question

So basically when i press “jump” and the player is on the wall as the pushback of the wall happens i can see the player go to move along the x axis but because i am holding “left” it is making the player move straight back into the wall therefore creating no push off the wall it just basically makes the character slide up the wall… been trying for about 5 hours swapping bits out and fiddling with the code and cant seem to figure out the issue any help would be much appreciated :smiley: i know im close to the solution but just cant seem to figure it out haha
extends CharacterBody2D

@export var climb_speed: float = 100
@export var speed: float = 150.0
@export var jump_velocity: float = -250.0
@export var wall_jump_velocity: float = 1000.0
@export var double_jump_velocity: float = -150
@export var wall_slide_gravity: float = 0.3
@onready var animated_sprite : AnimatedSprite2D = $Animated_ninja_sprite

Get the gravity from the project settings to be synced with RigidBody nodes.

var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)
var has_double_jumped: bool = false
var animation_locked : bool = false
var direction : Vector2 = Vector2.ZERO
var was_in_air : bool = false

func _physics_process(delta):

# Add the gravity.
if is_on_wall() and (Input.is_action_pressed("left") or Input.is_action_pressed("right")):
	velocity.y += wall_slide_gravity
elif not is_on_floor():
	velocity.y += gravity * delta
	was_in_air = true
else: #hit the floor
	has_double_jumped = false
	if was_in_air == true:
		land()
	was_in_air = false
	
# Get the input direction and handle the movement/deceleration.
direction = Input.get_vector("left", "right", "up", "down")

#handles movement
if direction.x && animated_sprite.animation != "landing":
	velocity.x = direction.x * speed # works
else:
	velocity.x = move_toward(velocity.x, 0, 36.0)


# Handle jump.
if Input.is_action_just_pressed("jump"):
	#jump from floor
	if is_on_floor():
		normal_jump()
	#jump from wall
	elif is_on_wall() and Input.is_action_pressed("left"):
		velocity.y = jump_velocity
		velocity.x = wall_jump_velocity
		#wall_jump()
	#double jump in the air
	elif not has_double_jumped:
		double_jump()



#climb()
move_and_slide()
update_animation() 
update_facing_direction()

func update_animation():
if not animation_locked:
if not is_on_floor() && not is_on_wall():
animated_sprite.play(“jump_loop”)
elif is_on_wall():
animated_sprite.play(“climb”)
else:
if direction.x != 0:
animated_sprite.play(‘run’)
else:
animated_sprite.play(‘idle’)

func update_facing_direction(): animation
if direction.x > 0:
animated_sprite.flip_h = false
elif direction.x < 0:
animated_sprite.flip_h = true

func climb():
#handles climbing
if is_on_wall():
animation_locked = false
has_double_jumped = false
if direction:
velocity.y = direction.y * climb_speed
else:
velocity.y = move_toward(velocity.y, 0, climb_speed)

func normal_jump():
velocity.y = jump_velocity
animated_sprite.play(“jump”)
animation_locked = true

func double_jump():
velocity.y = double_jump_velocity
has_double_jumped = true

func land():
animated_sprite.play(“landing”)
animation_locked = true

func _on_animated_ninja_sprite_animation_finished():
if(animated_sprite.animation == “landing”):
animation_locked = false
elif(animated_sprite.animation == “jump”):
animated_sprite.play(“jump_loop”)

Make sure to paste your code with proper formatting. Use the </> button or ctrl+e in the forum to create three ticks like so

```
type or paste code here
```

Seems like you might prefer to use move_toward instead of directly setting velocity.x in some places. Like so, make sure to define “ACCELERATION” as a high number

velocity.x = move_toward(velocity.x, direction.x * speed, ACCELERATION * delta)
1 Like

will try that thanks first post here so wasnt sure about how to make the code formatted properly thanks for info :smiley:

thankyou that worked perfectly
just asking so i know i understand why, was the velocity movement overwriting it
the movement of the jump?
so now instead what is happening is the velocity change is happening first and instead of overwriting the movement it is just slowly moving towards?

You can see the difference here, setting velocity equal to something overwrites the previous value, with move_toward it uses the previous value to “move toward” the desired value. the third parameter is how fast it will move toward that desired value, it’s good to multiply this by delta so it isn’t frame-dependent

thankyou so much for your help :smiley:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.