Coding after movement

Godot Version

version 4 or newest

Question

Hello everyone! SO I did a tutorial i know up to adding an attack i cant get the coding right to play the animation i simply use if button is press animatedsprite 2d.play"" but the animation just kindof twitches

There could be many reasons why this happens, but without seeing the code the best people can do is guess what causes it. Please post your code (and post three backticks (i.e. ```) before and after your code so formatting is maintained.))

If you have something like this:

func _process(delta: float) -> void:
    if Input.is_action_pressed("attack"):
        $anim.play("attack")

That’s going to be called every frame. If the player pushes the attack button, it’s going to continue to be down for multiple frames, which is going to trigger this code repeatedly and re-start the animation repeatedly.

Your computer is probably calling _process() at 60Hz (60 times per second) or faster. At that speed, the player’s actions actually look fairly slow to the computer.

The fix for this is edge detection. In addition to is_action_pressed() there’s an is_action_just_pressed(). The second call only returns true if the button was not pressed last time you asked, but is pressed now.

There could be something else going on; as @TokyoFunkScene says, its’ a good idea to post your code. When you do, format it like this:

```gdscript
your code here
```

1 Like