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)