0 votes

My character freezes when "Attack" is pressed. After this, I can only jump in place with ui_up. Here is my code:

extends KinematicBody2D

const SPEED = 100
const GRAVITY = 10
const JUMP_POWER = -250
const FLOOR = Vector2(0, -1)
var isAttacking = false

var velocity = Vector2()

var on_ground = true

func physicsprocess(_delta:float) -> void:

if Input.is_action_pressed("ui_left") && isAttacking == false:
        velocity.x = -SPEED
        $AnimatedSprite.play("Run")
        $AnimatedSprite.flip_h = true

elif Input.is_action_pressed("ui_right") && isAttacking == false :
        velocity.x = SPEED
        $AnimatedSprite.play("Run")
        $AnimatedSprite.flip_h = false

else:
    velocity.x = 0
    if on_ground == true:
        if isAttacking == false:
            $AnimatedSprite.play("Idle")


if Input.is_action_pressed("ui_up"):
    if on_ground == true:
        velocity.y = JUMP_POWER
        on_ground = false

if Input.is_action_just_pressed("Attack"):
    if on_ground == true:
        if $AnimatedSprite.flip_h == false:
            $AnimatedSprite.play("Attack")
            isAttacking = true
            $Attack_Area_Right/Attack_Collision.disabled = false
        if $AnimatedSprite.flip_h == true:
            $AnimatedSprite.play("Attack")
            isAttacking = true
            $Attack_Area_Left/Attack_Collision.disabled = false


velocity.y += GRAVITY

if is_on_floor():
    on_ground = true
else:
    on_ground = false
    if velocity.y < 0:
        $AnimatedSprite.play("Jump")
    else:
        $AnimatedSprite.play("Fall")

velocity = move_and_slide(velocity, FLOOR)

func onAnimatedSpriteanimationfinished():
if $AnimatedSprite.animation == "Attack":
$AttackAreaRight/AttackCollision.disabled = true
isAttacking = false
if $AnimatedSprite.animation == "Attack":
$Attack
AreaLeft/AttackCollision.disabled = true
isAttacking = false

asked Jun 3 in Engine by LifeRips (14 points)

I cannot reproduce your problem. Have you made sure the animation_finished-signal is connected? If your player character doesn't move anymore after pressing "Attack", then that's likely because isAttacking is never reset to false.

If you upload an example project, I can take a look.

1 Answer

–1 vote

Maybe change both

&& isAttacking == false

to

&& (isAttacking == false)

could help?

answered Jun 3 by Becbunzen (156 points)

Hi, thanks for the suggestion but still no luck.

Your code is a bit hard to read, with some not in code block. Try to comment out all effects of attacking, just print out that attack was pressed, and if that work add back the functionality you want until all is there. Then you will see which part is problematic.

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 How to use this Q&A? before posting your first questions.