Player animation not playing

Godot Version

version 4.3

Question

I have two animation, one for jump and falling.
My code can detect whether the player is jumping but the jump animation is not playing. Although falling animation is working fine.

Any help?

 func _physics_process(delta: float) -> void:
	## get the horizontal inputs by the user
	var horizontal_input = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")

	velocity.x = horizontal_input * movement_speed
	velocity.y += gravity
	
	##getting player current action state
	var is_falling = velocity.y > 0.0 and not is_on_floor()
	var is_jumping = Input.is_action_just_pressed("jump") and is_on_floor()
	var is_jump_cancelled = Input.is_action_just_released("jump") and not is_on_floor() and velocity.y < 0.0
	var is_idle = is_on_floor() and is_zero_approx(velocity.x)
	var is_walking = is_on_floor() and not is_zero_approx(velocity.x)
	
	if is_jumping:
		velocity.y = -jump_force
	
	move_and_slide()
	
	if is_jumping:
		player_sprite.play("jump")
	elif is_falling:
		player_sprite.play("fall")
	elif is_idle:
		player_sprite.play("idle")
	elif is_walking:
		player_sprite.play("walk")
	else:
		player_sprite.play("idle")
	## <0 is left
	## >0 is right
	## check if the player is not moving, if not retain the current sprite scale
	if not is_zero_approx(horizontal_input):
		if horizontal_input < 0:
			player_sprite.scale = Vector2(-initial_sprite_scale.x, initial_sprite_scale.y)
		else:
			player_sprite.scale = Vector2(initial_sprite_scale.x, initial_sprite_scale.y)

Is this supposed to say ‘but the jump animation is not playing’?

_physics_process is called many times a second. If your character is on the floor and the jump button is pressed, is_jumping will be set to true. The jump animation will play. The next time _physics_process is called, is_jumping will be set to false (because the player is no longer on the floor) and the animation will be set to something else. (Either fall or idle, depending on the character’s velocity.)

Because _physics_process is called something like sixty times a second, the fall animation will not play for very long.

yes sorry hehe

Do you have any code solution for this one?

The quickest fix is to change the line of code that determines whether the player is jumping. If velocity in the y-direction is negative and the player is not on the floor, that means the player is both in the air and moving up-- pretty much the definition of a jump. (You will need to detect the player input for the jump and adjust the character’s upward velocity somewhere else.)

The best fix is to create a state machine for your player controller, because once you start adding more states (i.e. falling, attacking, crouching, attacking while crouching, attacking while crouching and falling, etcetera) code tends to enter something I affectionately call “if-statement hell.”

If you search for something like “2d platform character state machine godot” you will probably find a ready-made solution. It takes a bit longer to implement, but it will save you many headaches in the long run.

1 Like