How can I time my attacks with my hitbox?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By LyguN

I have an enemy who chases the player and has a raycast2d on him to detect if the player is in attack range for the enemy. If the player is in attack range, the enemy hitbox is enabled and the player takes damage. The problem is that the enemy attack animation is too slow for the hitbox which means that the player takes damage before the animation actually hits the player. How would I go about to delay the hitbox enabling a little bit so that it times better with the animation?

Here is the attack function(its called everytime the raycast detects the player):

func Attack():
   attackTimer.start()

   if attackTimer.time_left < 0:
   	 hitbox.disabled = false

   is_attacking = true
   animSprite.play(str(Enemy_Type)+"_Attack1")
   yield(animSprite, "animation_finished")
   hitbox.disabled = true
   is_attacking = false

As you can see I tried to delay the hitbox with a timer but it doesn’t really work. Also I’d like to note that I am not using an animation player for the enemy so I can’t enable/disable the hitbox via animations. If anyone could tell me what I did wrong in my script or give me other methods to do this then that would be great.

:bust_in_silhouette: Reply From: AlexTheRegent

You can make animation (using AnimationPlayer) to enable hitbox during exact animation frame when it should hit player.

Ok I made a new AnimationPlayer like you said so now I am using a AnimationPlayer for the attack animations and a AnimatedSprite for the other animations. The hitbox is working perfectly as expected but now I have another problem. The attack animations are kind of laggy and jittery. Any idea on how I can fix this?

LyguN | 2021-02-02 21:32

It’s hard to say without video or example how it is jitters.

AlexTheRegent | 2021-02-03 08:41