Thanks GameDev.tv and @kaanalpar for a great course on Godot for 2D.
I thought I would give double jumping a try.
extends CharacterBody2D
@onready var animated_sprite = $AnimatedSprite2D
@export var gravity = 400
@export var horiz_speed = 125
@export var vert_speed = 125
@export var jump_force = 200
@export var max_jumps = 2
var max_fall_gravity = 500
var jumps_left = max_jumps
func _physics_process(delta):
var onFloor = is_on_floor()
# Apply gravity when not on a floor
if !onFloor:
velocity.y += gravity * delta
velocity.y = clampf(velocity.y, -max_fall_gravity, max_fall_gravity)
else:
jumps_left = max_jumps
# Perform jump?
if (onFloor || (jumps_left > 0)) && Input.is_action_just_pressed("jump"):
velocity.y = -jump_force
jumps_left -= 1
# Get direction based on player's input
var direction = Input.get_axis("move_left", "move_right")
# Apply horizontal movement
velocity.x = direction * horiz_speed
# Flip sprite based on direction of travel
if direction != 0:
animated_sprite.flip_h = (direction < 0)
move_and_slide()
update_animation(direction, )
func update_animation(direction):
if is_on_floor():
if direction != 0:
animated_sprite.play("run")
else:
animated_sprite.play("idle")
else:
if velocity.y < 0:
animated_sprite.play("jump")
else:
animated_sprite.play("fall")