Checking is_action_just_pressed() only while the character is not attacking throws away every press made while a swing is still playing, and players press a little early. In our Godot 4.7 test, pressing every 0.3 s during 0.4 s swings started only 5 swings from 10 presses. Remember the press for 0.2 s and start the swing when the character is free: 8 of 10, 0.088 s after the press on average.
The code
Call press() from your input code when attack is pressed, and tick(delta) every frame. Wherever the character becomes free to attack, idle or at the end of a swing, start the next swing if consume() returns true. One press starts one swing at most.
extends Node
## Remembers an attack press for a short time, so a press made just before the
## current swing ends still starts the next swing.
## Call press() from your input code and tick(delta) every frame.
## When the character is free to attack, start a swing if consume() returns true.
@export var buffer_time := 0.2 # seconds a press is remembered
var _left := 0.0
func press() -> void:
_left = buffer_time
func tick(delta: float) -> void:
_left = maxf(_left - delta, 0.0)
func consume() -> bool:
if _left > 0.0:
_left = 0.0 # one press = one swing
return true
return false
What we measured
A 0.4 second swing at 60 fps, with presses at set times. Two runs with the same numbers.
- Pressing every 0.3 s, 10 presses: ignoring presses during a swing started 5 swings. The script started 8, on average 0.088 s after the press that started each one.
- A second press 0.1 s before the swing ends: ignored, so 1 swing. The script: 2 swings, the second one starting the moment the first ended.
- A second press 0.3 s before the swing ends: 1 swing both ways. That press was older than the 0.2 s buffer, so the script let it go, on purpose.
Why it happens
A swing takes time, and players press for the next one before it ends. If those presses are dropped, the game feels like it ate the input. A short buffer bridges that gap. Keep it short: a press made long before the character is free was probably not meant for the next swing, and a long buffer can make the character keep attacking after the player stopped.
What to change
buffer_time: how long a press is remembered. We tested 0.2 s. The same idea works for jump, dodge and parry presses.
Hit each enemy once per swing: sword hitbox that hits each enemy once. Make the hit land harder: hit stop. A parry that works the same at any frame rate: parry window in seconds.
Ready-made slashes: Japanese VFX for Godot 4 has 37 presets, including a Katana Slash crescent with sparks, kitsune fox fire, torii portals, ofuda barriers and sakura petals.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Japanese VFX for Godot 4 has 37 ready-made Japanese effects, including a Katana Slash crescent with sparks, kitsune fox fire, torii portals, ofuda barriers and sakura petals.
More how-tos for Japanese VFX for Godot 4: A sword hitbox that hits each enemy once per swing
More how-tos: Game feel
- A swing that settles the same at any frame rate
- Screen shake that returns to center
- 3D camera shake that lands back on center
- Hit stop and slow motion
- Confetti particles with random colors
- Choppy particles: fixed_fps and interpolate
- Play animations in 12 steps a second, at the right speed on any screen
- Keep a smooth follow camera, but jump with the player when they teleport
- Make a harvested crop pop out in an arc
- Shake a rock or tree when hit, without it drifting
- Pickups that fly to the player in the same time at any frame rate
- A cooldown that waits while the game is paused
- A dash that covers the same distance at any frame rate
- A hold-to-craft bar that takes the same time at any frame rate and finishes once