problem with timer and falling animation

Godot Version

40202 stable

Question

falling animation keeps playing even when on floor

extends CharacterBody2D

var d = 0
const SPEED = 300.0
@export var JUMP_VELOCITY = -400.0
@onready var animatedS = $AnimatedSprite2D
@onready var timer = $Timer


#Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")



func animation(d):
	if d > 0:
		animatedS.flip_h = false
	elif d < 0:
		animatedS.flip_h = true

	if not is_on_floor():
		animatedS.play("jump")
		timer.start()
	elif is_on_floor():
		timer.stop()
		timer.set_wait_time(1.5)

	elif d != 0:
		animatedS.play("run")
	else:
		animatedS.play("idle")

func _physics_process(delta):


# Add the gravity.
	if not is_on_floor():
		velocity.y += gravity * delta
# Handle jump.
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = JUMP_VELOCITY
		
	print(timer.get_time_left())

# 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("ui_left", "ui_right")
	d = direction
	if direction:
		velocity.x = direction * SPEED
		
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)
	
	#handle the animation
	animation(direction)


	move_and_slide()


func _on_timer_timeout():
	animatedS.stop()
	animatedS.play("fall")



Do like this:

func animation(d):
	if d > 0:
		animatedS.flip_h = false
	elif d < 0:
		animatedS.flip_h = true

	if not is_on_floor():
		animatedS.play("jump")
		timer.start()
	elif is_on_floor():
		timer.stop()
		timer.set_wait_time(1.5)

	if d != 0 and is_on_floor(): #Changed line ***
		animatedS.play("run")
	else:
		animatedS.play("idle")

1 Like