Godot Version
4.6
Question
Hi, I’m new to coding in general, and I’m trying to wrap my head around getting the attack animation to work without making a state machine. I don’t get any error messages, but every time I try to run the program, the character performs the animation extremely fast. Ive tried adding an await line before at certain points and used an animation finished signal but it never works. I really dont want to redo my code with a state machine because I don’t understand how they work and i just barely got all of the movement animations to work after struggling for a couple weeks.
extends CharacterBody2D
#variables for states
var is_attacking = false
var is_jumping = false
var is_running = false
var is_falling = false
var is_idle = true
#variables for physics
var SPEED := 300.0
var JUMP_VELOCITY := -400.0
var GRAVITY = ProjectSettings.get_setting("physics/2d/default_gravity")
#called nodes
@onready var animation_player = $AnimationPlayer
@onready var animation_sprite = $Sprite
func _physics_process(delta: float) -> void:
# Add gravity
if not is_on_floor():
velocity.y += GRAVITY * delta
# Handle Jump
if Input.is_action_just_pressed("jump_1") and is_on_floor():
is_idle = false
is_jumping = true
velocity.y = JUMP_VELOCITY
# Get input direction and handle movement/deceleration
var direction = Input.get_axis("left_1", "right_1")
if direction:
is_idle = false
is_running = true
velocity.x = direction * SPEED
else:
is_idle = true
velocity.x = move_toward(velocity.x, 0, SPEED)
#Movement Animation
if not is_on_floor():
if velocity.y < 0 and is_jumping == true:
animation_player.play("Jump")
if velocity.y and velocity.x < 0:
animation_sprite.flip_h = true
elif velocity.y and velocity.x > 0:
animation_sprite.flip_h = false
else:
is_falling = true
animation_player.play("Fall")
if velocity.y > 0 and velocity.x < 0:
animation_sprite.flip_h = true
elif velocity.y >0 and velocity.x > 0:
animation_sprite.flip_h = false
elif direction != 0.0:
is_running = true
animation_player.play("Dash")
if velocity.x < 0:
animation_sprite.flip_h = true
elif velocity.x > 0:
animation_sprite.flip_h = false
else:
is_idle = true
animation_player.play("idle")
if is_attacking: return
else:
is_idle = true
move_and_slide()