Godot 4.2
Question
How do can i fix my code to allow me to move and jump at the same time (not my code is long because i use multiple sprites for different inputs, as the assets i got were in individual sprite sheets)
extends CharacterBody2D
@export var speed = 1200
@export var jump_speed = -1800
@export var gravity = 4000
@onready var animation = $AnimationPlayer
func _physics_process(delta):
# Add gravity every frame
velocity.y += gravity * delta
# Input affects x axis only
velocity.x = Input.get_axis("Left", "Right") * speed
if Input.is_action_pressed("Left") and is_on_floor():
$"Run Sprite".flip_h = true
$"Jump Sprite".flip_h = true
$"Idle Sprite".flip_h = true
get_node("Run Sprite").show()
get_node("Idle Sprite").hide()
get_node("Jump Sprite").hide()
animation.play("Run")
elif Input.is_action_pressed("Right") and is_on_floor():
$"Run Sprite".flip_h = false
$"Jump Sprite".flip_h = false
$"Idle Sprite".flip_h = false
get_node("Idle Sprite").hide()
get_node("Jump Sprite").hide()
get_node("Run Sprite").show()
animation.play("Run")
# Only allow jumping when on the ground
elif Input.is_action_just_pressed("Jump") and is_on_floor():
velocity.y = jump_speed
get_node("Jump Sprite").show()
get_node("Run Sprite").hide()
get_node("Idle Sprite").hide()
animation.play("Jump")
elif not is_on_floor():
get_node("Idle Sprite").hide()
get_node("Run Sprite").hide()
get_node("Jump Sprite").show()
animation.play("Fall")
else:
get_node("Run Sprite").hide()
get_node("Jump Sprite").hide()
get_node("Idle Sprite").show()
animation.play("Idle")
move_and_slide()