This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

I am making an 8 directional movement project.

So far I have been able to store each direction as separate animations in an animatedsprite2d node.

Movement is working with a walk and a run animation depending on the distance the mouse is from the player character, but I am having issues with my "isAttacking" state.

In the physics process I check if isAttacking is false before I run any other animation. When the attack input is pressed it sets isAttacking to true. I used a signal from the AnimatedSprite2d that when the animation is finished if the animation is one of the attack animations then set isAttacking to false.

The end result is the attack animation plays but after that the character is stuck in one frame of animation for walking and running. If I attack one more time then the character gets stuck in place and doesnt animate.

The intended behavior is that if I attack the character will stay facing the same direction and finish their attack animation before being allowed to move again.

extends CharacterBody2D

var current_animation = "idle"
var speed = 200
var runspeed = 400
var ang = 0
var click_position = Vector2(0, 0)
var isAttacking = false

#const SPEED = 300.0
const JUMP_VELOCITY = -400.0

# Get the gravity from the project settings to be synced with RigidBody nodes.
#var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")

func _ready():
    click_position = Vector2(position.x, position.y)
    $AnimatedSprite2D.play()
    current_animation = "idle"


func _physics_process(delta):
    #mouse input
    var mouse = get_local_mouse_position()
    if isAttacking == false:
        ang = snapped(mouse.angle(), PI/4) / (PI/4)
        ang = wrapi(int(ang), 0, 8)

    if isAttacking == false:
        current_animation = "idle"

    if Input.is_action_pressed("left_mouse") and mouse.length() > 10 and mouse.length() < 200 and isAttacking == false:
        current_animation = "walk"
        velocity = mouse.normalized() * speed
        move_and_slide()

    elif Input.is_action_pressed("left_mouse") and mouse.length() > 200 and isAttacking == false:
        current_animation = "run"
        velocity = mouse.normalized() * runspeed
        move_and_slide()

    elif Input.is_action_just_pressed("right_mouse") and isAttacking == false:
        current_animation = "attack"
        isAttacking = true
    $AnimatedSprite2D.animation = current_animation + str(ang)


func _on_animated_sprite_2d_animation_finished():
    if $AnimatedSprite2D.animation == "attack" + str(ang):
        isAttacking = false
        print(isAttacking)
Godot version 4.0
in Engine by (12 points)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.