Godot Version
4.4.1
Question
Hello everyone, I’m new to Godot (and appart from some previous small experience with Python, to programming). I’m currently working on small projects to learn, this one is about creating a player template suited for 2D platformers.
The issue is that all my animations (walking_right, walking_left, iddle_right, iddle_left) are working as intended except for the jumping ones (jumping_right, jumping_left), which are not showing up. I’ve used the script offered by the CharacterBody2D template and I’ve built the rest from there mainly using trial and error, but I haven’t been able to solve this last problem. That’s the code, any help will be highly appreciated:
extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
var previous_direction = 1
func _physics_process(delta: float) → void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
var direction := Input.get_axis("walk_left", "walk_right")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
#Handle animation
if Input.is_action_pressed("walk_right") and is_on_floor():
$AnimatedSprite2D.play("walking_right")
previous_direction = 1
elif Input.is_action_pressed("walk_left") and is_on_floor():
$AnimatedSprite2D.play("walking_left")
previous_direction = -1
if velocity.x == 0 and previous_direction == 1:
$AnimatedSprite2D.play("iddle_right")
elif velocity.x == 0 and previous_direction == -1:
$AnimatedSprite2D.play("iddle_left")
if not is_on_floor() and previous_direction == 1:
$AnimatedSprite2D.play("jumping_right")
elif not is_on_floor() and previous_direction == -1:
$AnimatedSprite2D.play("jumping_left")
move_and_slide()