Gravity not applied while mid air

Godot Version

4.3

Question

In this code extends CharacterBody2D

const SPEED = 200.0
const JUMP_VELOCITY = -350.0

@onready var animatedsprite = $AnimatedSprite2D
@onready var animater = $CollisionShape2D/AnimationPlayer
@onready var sound = $AudioStreamPlayer2D

var is_attacking = false
var level = 1
var lives = randi_range(3,7)
var dead = 1

func _physics_process(delta: float) → void:
if lives == 0:
get_tree().call_deferred(“reload_current_scene”)
if Input.is_action_just_pressed(“attack”) and animatedsprite.animation != “attack”:
sound.play()
lives -= 1
animater.play(“battack”)
is_attacking = true
animatedsprite.play(“attack”)
if is_attacking:
return

# Handle gravity if in the air
if not is_on_floor():
	velocity += get_gravity() * delta
	# Play jump animation if not already jumping
	if animatedsprite.animation != "jump":
		animatedsprite.play("jump")
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
	animatedsprite.play("jump")
	velocity.y = JUMP_VELOCITY

var direction := Input.get_axis("ui_left", "ui_right")
if direction > 0:
	animatedsprite.flip_h = false
elif direction < 0:
	animatedsprite.flip_h = true

if direction:
	# Play run animation only if on the floor and moving horizontally
	if is_on_floor() and animatedsprite.animation != "run":
		animatedsprite.play("run")
	velocity.x = direction * SPEED
elif Input.is_action_just_pressed("attack") and animatedsprite.animation != "attack":
	sound.play()
	lives -= 1
	animater.play("battack")
	is_attacking = true
	animatedsprite.play("attack")
elif not is_on_floor():
	# Keep the jump animation if in the air
	if animatedsprite.animation != "jump":
		animatedsprite.play("jump")
else:
	# Decelerate to a stop
	velocity.x = move_toward(velocity.x, 0, SPEED)
	if animatedsprite.animation != "default":
		animatedsprite.play("default")

# Apply the movement
move_and_slide()

func _on_animated_sprite_2d_animation_finished() → void:
if animatedsprite.animation == “attack”:
animater.play(“RESET”)
is_attacking = false
the gravity isnt applied while attacking mid air I am pretty sure it is something simple but I just cant figure out how to fix it

You are currently returning before applying any gravity when is_attacking is true. This has to be called before you return:

#Handle gravity if in the air
if not is_on_floor():
	velocity += get_gravity() * delta

You also need to call the move_and_slide() method in any way for the velocity to apply (your current code returns before it can be called)

that cause the player to fall to the ground quicker after animation finishes

I don’t think I understand what you want to tell me. If you wonder why your gravity is not applied when attacking it is because of the return statement before your gravity code is called. I hope that answers your question?

ok I fixed I had to add a decelerate function inside if is_attacking

thanks for the help