My colisionshape2d wont disable after attacking

I’ve been following a this Tutorial https://www.youtube.com/watch?v=q0WHhsmifkQ
and the colision wont disable after the input is pressed here’s the code if it will help
func _process(delta: float) → void:
#animations
if (velocity.x > 1 || velocity.x < -1):
$Sprite2D.play(“walk”)
else:
$Sprite2D.play(“idle”)
if Input.is_action_just_pressed(“attack”):
$Sprite2D.play(“attack”);
attack = true;
$attackarea/CollisionShape2D.disabled = false;

func _on_sprite_2d_animation_finished() → void:
if $Sprite2D.animation == “attack”:
$attackarea/CollisionShape2D.disabled = true;
attack = false;

Hi, youre watching a godot 3 video, but youre on godot 4, so it wont work, but i’ll try to fix it!

Copy what im saying:


func _process(delta: float) -> void:
	if velocity.x > 1:
		$AnimatedSprite2D.play("walk")
	elif velocity.x < -1:
		$AnimatedSprite2D.play("walk")
	else:
		$AnimatedSprite2D.play("idle")
	
	if Input.is_action_just_pressed("attack"):
		$AnimatedSprite2D.play("attack")
		attack = true
		$attackarea/CollisionShape.disabled = false
		
	

func _on_animated_sprite_2d_animation_finished() -> void:
	if $AnimatedSprite2D.animation == "attack":
		$attackarea/CollisionShape.disabled = true
		attack = false

I switched “$Sprite2D” to “$AnimatedSprite2D” Because from my knowledge… Sprite2Ds Don’t have a “.play()” thing, nor “.animation” thing, and i removed your ; because i don’t think they’re needed.

If my code doesn’t work then maybe do this code:


func _process(delta: float) -> void:
	if velocity.x > 1 or velocity.x < -1:
		$AnimatedSprite2D.play("walk")
	else:
		$AnimatedSprite2D.play("idle")
	
	if Input.is_action_just_pressed("attack"):
		$AnimatedSprite2D.play("attack")
		attack = true
		$attackarea/CollisionShape.disabled = false
			

func _on_animated_sprite_2d_animation_finished() -> void:
	if $AnimatedSprite2D.animation == "attack":
		$attackarea/CollisionShape.disabled = true
		attack = false

Tell me if this worked!

Make sure to change your sprite2d to animated sprite2d, because that might play the animations (if your animations are already playing then no need to switch to animatedsprite2d)

sorry i did not exspane my sprite2d was changed to an animatedsprite2d wich is why it appears that way.

1 Like

i fixed the problem it was just that i had to add this to the script

get_node(“attackarea/CollisionShape2D”).disabled = false
get_node(“attackarea/enabletimer”).start()

1 Like