← Pixel Dreamer Studio

A dash that covers the same distance at any frame rate

Give it a distance and a time, and let the last frame move only for the time that is left.

A dash that lasts a number of frames covers a different distance at every frame rate, and a dash on a timer goes too far on its last frame. In our Godot 4.7 test a dash meant to cover 6 m in 0.2 s went 12 m at 30 fps and 1.5 m at 240 fps when it lasted 12 frames. On a timer it went 6.5 m at 60 fps, 7 m at 30 fps, and 7 m at 60 fps when one frame took 0.1 s. Moving the last frame only for the time that was left gave 6.00 m in every case.

The code

Save it as dash_move.gd, keep one in your player script with var dash := preload("res://dash_move.gd").new(), start it with dash.start(-global_transform.basis.z, 6.0, 0.2) (direction, metres, seconds) and every physics frame call if dash.is_dashing(): move_and_collide(dash.step(delta)). With move_and_slide(), set velocity = dash.step(delta) / delta instead. It works the same with Vector2 positions in 2D.

extends RefCounted
## A dash that covers the same distance in the same time at any frame rate,
## and through a stutter. Works with Vector3 and Vector2.
##   var dash := DashMove.new()
##   dash.start(-global_transform.basis.z, 6.0, 0.2)   # direction, metres, seconds
##   # then every frame, in _physics_process or _process:
##   if dash.is_dashing():
##       move_and_collide(dash.step(delta))
## For a CharacterBody that uses move_and_slide, set velocity = dash.step(delta) / delta.

var _dir = Vector3.ZERO
var _speed := 0.0
var _left := 0.0


func start(direction, distance: float, duration: float) -> void:
	_dir = direction.normalized()
	_speed = distance / duration
	_left = duration


func is_dashing() -> bool:
	return _left > 0.0


func step(delta: float):  # how far to move this frame
	var dt := minf(delta, _left)  # the last frame only moves for the time that was left
	if dt <= 0.0:
		return _dir * 0.0
	_left -= dt
	return _dir * (_speed * dt)

What we measured

A dash meant to cover 6 m in 0.2 s (30 m/s), run at five frame rates and once at 60 fps with a single 0.1 s frame, the kind of hitch a loading spike causes. Two runs gave the same numbers.

Why it happens

Counting frames ties the dash to the frame rate: 12 frames is 0.4 s at 30 fps and 0.05 s at 240, so the same speed covers 8 times the distance. A timer fixes the length, but the frame that crosses the end still moves for its whole delta, past the moment the dash should have stopped. Even at 30 and 60 fps, where 0.2 s divides evenly, counting down 1/30 or 1/60 at a time leaves a tiny remainder above zero in floating point, so the timer ran one frame too many. The script clamps the last step to the time that was left, so the distance is speed times duration, whatever the frames are.

What to change

What the dash looks like: Movement VFX for Godot 4 has 31 ready-made movement effects, including Dash Ground, Dash Air, Sprint Wind, landing dust and footsteps, in Modern and PSX looks. Call play() on Dash Ground when the dash starts.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Movement VFX for Godot 4 has 31 ready-made movement effects, including Dash Ground, Dash Air, Sprint Wind, landing dust and footsteps, in Modern and PSX looks.

More how-tos for Movement VFX for Godot 4: Running dust and landing puffs · Footstep particles that match the ground

More how-tos: Game feel