Godot Version
Godot version 4
So its my first time using godot apart from the tutorial and I am having trouble with the jump animation in my game which is not working properly like it plays fine the first time but after that instead of jump animation the player just keeps doing the run animation if I am running and jumping`and idle animation if I am idle.
extends CharacterBody2D
@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D
const SPEED = 150.0
const JUMP_VELOCITY = -500.0
var Facing_Direction : String = "R"
var animation_played: bool = false
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.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction := Input.get_axis("move_left", "move_right")
if Input.is_action_pressed("move_left"):
Facing_Direction = "L"
if Input.is_action_pressed("move_right"):
Facing_Direction = "R"
#Sprite Direction
if is_on_floor():
if direction == 0:
animated_sprite.play("Idle." + Facing_Direction)
#print("Idle." + Facing_Direction)
else:
animated_sprite.play("Run." + Facing_Direction)
#print("Run." + Facing_Direction)
elif velocity.y > 0:
velocity = get_gravity() * delta * 5
animated_sprite.play("Fall." + Facing_Direction)
#print("Fall." + Facing_Direction)
if Input.is_action_pressed("jump"):
if not animation_played: # Only play if not already played
animated_sprite.play("Jump." + Facing_Direction)
print("Is Jumping")
animation_played = true
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
func _on_animation_finished(animation_name):
if animation_name == "Jump.R" or animation_name == "Jump.L":
animation_played = false # Reset the flag when finished