Godot Version
4.3
Question
I want the attack animation to play when I hit the button designated for it but it keeps playing idle or run or jump animation depending on the state is there any way I can fix it? (I’m new to godot and coding so it’s hard for me)
extends CharacterBody2D
const SPEED = 130.0
const JUMP_VELOCITY = -300.0
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
func _physics_process(delta: float) -> void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# Attack Function
if Input.is_action_just_pressed("AttackButton"):
animated_sprite_2d.play("Attack")
print("Attack!")
# Handle jump.
if Input.is_action_just_pressed("Jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction -1, 0, 1
var direction := Input.get_axis("Left", "Right")
# Flip the Sprite
if direction > 0:
animated_sprite_2d.flip_h = false
elif direction <0:
animated_sprite_2d.flip_h = true
# Play Animations
if is_on_floor():
if direction == 0:
animated_sprite_2d.play("Idle")
else:
animated_sprite_2d.play("Run")
else:
animated_sprite_2d.play("Jump")
# Apply Movement
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()