My attack animation wont play

Godot Version

4.3

Question

so im trying to make an rpg platformer for my first game and ive hit a roadblock with the attack gravity run idle and jump all works but I cant make the attack animation play cause I think the idle run and jump is overridng it how do I fix it ive already tried a variable but the animations just keep playing regardless can someone help? (also swing is printing) extends CharacterBody2D

var swinging = false
const SPEED = 130
const JUMP_VELOCITY = -300
@onready var animated_sprite = $AnimatedSprite2D

func _physics_process(delta: float) → void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta

# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
	velocity.y = JUMP_VELOCITY

if Input.is_action_just_pressed("swing"):
	swinging = true
	animated_sprite.play("Swing 1")
	await get_tree().create_timer(0.6).timeout
	swinging = false
	print("swing")
# direction can be -1 0 1
var direction := Input.get_axis("left", "right")

if direction > 0:
	animated_sprite.flip_h = false
elif direction < 0:
	animated_sprite.flip_h = true
#animations
if is_on_floor():
	if direction == 0 and swinging == false:
		animated_sprite.play("idle")
	else:
		animated_sprite.play("run")
elif !is_on_floor():
		animated_sprite.play("jump")
	

if direction:
	velocity.x = direction * SPEED
else:
	velocity.x = move_toward(velocity.x, 0, SPEED)

move_and_slide()

Your states are all interfering with one another, so code that is detecting “swing” executes whether or not the character is in the air etc, and you have code that plays idle, run or jump without looking to see if the swing animation is being played. Changing your code to check if swinging is happening before playing other animations might help:

if not swinging:
	if is_on_floor():
		if direction == 0:
			animated_sprite.play("idle")
		else:
			animated_sprite.play("run")
	else:
		animated_sprite.play("jump")

consider giving your character a finite state, e.g.

enum State { Idle, Running, Jumping, Attacking }
var state : State = State.Idle

Then in _process (not _physics_process) you can test against that state to determine whether or not to take action, and set the state to the new state when you switch states. To get real sexy with it you can encapsulate this in a finite state machine that has functions that are automatically called when you enter or exit states.

wasnt my solution but I got it to work

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.