How to move downhill without triggering the fall animation?

Godot Version

4.5

Question

Hello! I’m learning to develop a 2D platformer game (with TileMapLayer) and I’m facing an issue. When my character moves downhill, it keeps playing the fall animation instead of moving smoothly along the slope. Here is my player script. I hope someone can help me. Thank you!

extends CharacterBody2D

@export var speed = 10.0
@export var jump_power = 10.0

var speed_multiplier = 30.0
var jump_multiplier = -30.0
var direction = 0

func _process(_delta: float) -> void:
	if direction == 1:
		$AnimatedSprite2D.flip_h = false
	elif direction == -1:
		$AnimatedSprite2D.flip_h = true
		
	if is_on_floor():
		if velocity.x != 0.0:
			$AnimatedSprite2D.play("walk")
		else:
			$AnimatedSprite2D.play("idle")
	else:
		if velocity.y < 0.0:
			$AnimatedSprite2D.play("jump")
		else:
			$AnimatedSprite2D.play("fall")

func _physics_process(delta: float) -> void:
	if not is_on_floor():
		velocity += get_gravity() * delta

	# Handle jump.
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = jump_power * jump_multiplier

	direction = Input.get_axis("move_left", "move_right")
	if direction:
		velocity.x = direction * speed * speed_multiplier
	else:
		velocity.x = move_toward(velocity.x, 0, speed * speed_multiplier)

	move_and_slide()

Try changing the CharacterBody2D.floor_snap_length property

1 Like