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.flip_h = true

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

    velocity.x = 0
    if on_ground == true:
        if isAttacking == false:

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:
            isAttacking = true
            $Attack_Area_Right/Attack_Collision.disabled = false
        if $AnimatedSprite.flip_h == true:
            isAttacking = true
            $Attack_Area_Left/Attack_Collision.disabled = false

velocity.y += GRAVITY

if is_on_floor():
    on_ground = true
    on_ground = false
    if velocity.y < 0:

velocity = move_and_slide(velocity, FLOOR)

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

in Engine by (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


&& (isAttacking == false)

could help?

by (160 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 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.