Use 1.0 - exp(-sharpness * delta) as the weight of the lerp instead of a fixed number like 0.1. A fixed weight closes the same share of the gap every frame, so more frames a second means a faster pull. In our Godot 4.7 test a coin 4 m from the player took 0.967 s to be collected at 30 fps, 0.483 s at 60 fps and 0.201 s at 144 fps. With the script below it took 0.500 s at all three.
The code
Attach this to the pickup and set target to the player. Once the player is inside pull_radius, the pickup flies in, sends collected when it is within 0.2 m and removes itself. Connect collected to add the coin and play the collect effect.
extends Node3D
## Pulls a pickup (coin, gem, heart) to the player once it is close, in the same time at any frame rate.
## Attach it to the pickup and set target to the player, or to a Node3D at the player's chest.
signal collected
@export var target: Node3D
@export var pull_radius := 4.0
@export var sharpness := 6.0 ## how fast it closes the gap: 6 takes about half a second from 4 m
func _physics_process(delta: float) -> void:
if target == null:
return
var goal := target.global_position
if global_position.distance_to(goal) > pull_radius:
return
# the share of the gap closed this frame grows with delta, so 1 long frame = 2 short ones
global_position = global_position.lerp(goal, 1.0 - exp(-sharpness * delta))
if global_position.distance_to(goal) < 0.2:
collected.emit()
queue_free()
What we measured
A pickup 4 m from the player. We stepped the physics ourselves at a fixed frame rate and counted how long it took to come within 0.2 m. Two runs gave the same numbers.
- 30 fps: lerp with 0.1, 0.967 s. The script, 0.500 s.
- 60 fps: lerp with 0.1, 0.483 s. The script, 0.500 s.
- 144 fps: lerp with 0.1, 0.201 s. The script, 0.500 s.
- A pickup 6 m away, outside the 4 m pull radius, did not move.
Why a fixed weight depends on the frame rate
lerp(a, b, 0.1) moves 10% of the way each time it runs. At 144 fps it runs almost 5 times as often as at 30 fps, so the pickup arrives almost 5 times sooner. With 1.0 - exp(-sharpness * delta) two short frames close exactly as much of the gap as one long frame, so only the time passed counts. The same goes for a camera that follows the player or a health bar that slides to its new value.
What to change
sharpness: higher is snappier. From 4 m, 6 took half a second.pull_radius: how close the player has to be before the pickup starts to fly.- For a pickup that should arrive at an exact moment, a tween with a fixed duration is simpler.
Coins flying to a counter on the screen instead: coins fly to the HUD counter. The same by-time idea for a torch light: a flicker at the same speed at any frame rate.
Collect effects in a pixel art style: Pixel Art 3D VFX for Godot 4 has 40 effects with a 4-color palette and dithering, including Coin Collect, Collect Star Burst, Item Glow, Level Up, Heal, Teleport, portals, campfires and explosions.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Pixel Art 3D VFX for Godot 4 has 40 pixel art effects for 3D games, including Coin Collect, Collect Star Burst, Item Glow, Level Up and Teleport.
More how-tos for Pixel Art 3D VFX for Godot 4: A torch light that flickers the same on every screen
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
- A cooldown that waits while the game is paused
- Keep attack presses made just before the swing ends
- 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