Give the track a few Area3D checkpoints in driving order with the finish line last, keep the index of the one the car has to reach next, and ignore every other checkpoint. In our Godot 4.7 test a car that drove 3 real laps got 11 from a finish line that added a lap on every body_entered: 1 for the start, 4 for driving back and forth over the line, 1 for a whole lap driven backwards and 2 for a U-turn back over the line halfway round. The script counted 3 and sent lap_finished with 1, 2 and 3.
The code
Add it to any node, drag the car into car and the checkpoints into checkpoints in driving order, with the finish line last. Connect lap_finished to your lap display, race timer or finish effect. Where the car starts does not matter: the finish line only counts once checkpoint 0 and the others have been passed.
extends Node
## Count a lap only after the car has passed every checkpoint in order.
## Put Area3D checkpoints around the track in driving order. The last one is the finish line.
signal lap_finished(lap: int)
@export var car: Node3D
@export var checkpoints: Array[Area3D] = []
var lap := 0
var next := 0 # the checkpoint the car has to reach next
func _ready() -> void:
for i in checkpoints.size():
checkpoints[i].body_entered.connect(_on_checkpoint.bind(i))
func _on_checkpoint(body: Node3D, i: int) -> void:
if body != car or i != next:
return # another body, or a checkpoint out of order: backwards, a wiggle on the line, a shortcut
next = (next + 1) % checkpoints.size()
if i == checkpoints.size() - 1:
lap += 1
lap_finished.emit(lap)
What we measured
A round track with a 20 m radius. The checkpoints were Area3D boxes 8 m across the track and 1 m thick, a quarter, half and three quarters of the way round, then the finish line. The car was a CharacterBody3D with a capsule, moved round at 180 degrees a second, about 63 m/s. Each line below is the finish line counter against the script. Two runs gave the same numbers.
- Starting 10 degrees behind the line and driving almost a lap: 1 against 0. The finish line counted the start.
- Over the line at the end of the first lap: 2 against 1.
- Back and forth over the line twice, 4 crossings: 6 against 1.
- A whole lap driven backwards: 7 against 1.
- A whole lap forwards: 8 against 2.
- Half a lap, a U-turn back over the line, then forwards over it again: 10 against 2. The script kept its place: the next checkpoint it wanted was still the one three quarters round.
- On round to the line: 11 against 3, and lap_finished came with 1, 2 and 3.
Why it happens
body_entered fires every time the car's shape starts touching the area, from either side and in either direction. A finish line on its own cannot tell a lap from a start, a wiggle or a car going the wrong way. Checkpoints in order can: the finish line only becomes the next checkpoint after the car has passed all the others.
What to change
- How many checkpoints: enough that there is no way from one to the next that skips part of the track. Put one on each side of any place where the track doubles back.
- The script ignores every body except the car. For several cars, give each car its own counter.
- Race position: lap times the number of checkpoints, plus next, says who is ahead. For two cars between the same checkpoints, compare how far along the track they are, as in picking the enemy furthest along the path.
- A ghost of the best lap that drives at real speed on any machine: record by time, not by frame.
For the finish: Racing & Kart VFX for Godot 4 has Finish_Confetti, a one-shot to play() from lap_finished on the last lap, plus drift smoke, nitro, backfire and crash impacts in Stylized and PSX looks.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Racing & Kart VFX for Godot 4 has Finish_Confetti to play() on the last lap, plus drift smoke, nitro and crash effects.
More how-tos for Racing & Kart VFX for Godot 4: Particles left behind by a moving object · Tire smoke that only shows when the car drifts
More how-tos: Game systems
- Hover a 3D object inside a SubViewport
- A turned building that covers the cells it stands on
- An inventory that never loses an item and never overfills a stack
- A JSON save that loads back exactly what you saved
- Fire that spreads one cell per step, in every direction
- A pooled enemy that comes back as fresh as a new one
- An enemy that stops at the dead player and finds the respawned one
- Poison, burn and bleed that keep ticking when applied again
- XP, qi and gold that keep growing without going negative
- Count slot line wins with wilds in front
- Random directions spread evenly over a sphere or a cone
- Make a seed repeat: daily runs, replays and roguelike seeds
- Offline progress that counts the time the game was closed
- A chance roll without the long bad-luck streaks
- Check crafting recipes in any order, with the right counts
- Make HP and mana regen the same at any frame rate
- Damage over time with the right number of ticks
- Spawn enemies where the camera cannot see them
- Keep a chasing horde from stacking into one enemy
- Make the fish bite after the same wait at any frame rate
- Snap buildings to the right grid cell on both sides of the origin
- Make a cooking timer fire once, on time
- Pick from a weighted loot table with the right chances
- Level up as many times as the XP covers
- Walk a patrol guard to each point without overshooting
- Remove items from an Array while checking them, without skipping any
- Scatter spikes, roots or trees in a circle without overlaps
- Count and take crafting items from every stack
- Play random sounds and events without obvious repeats
- Pick the growth image so the ripe one shows exactly when the crop can be picked
- Turn a day counter into season, day and year without a day 0
- An enemy that dies once, however many hits land
- A fair shuffle you can replay
- A ghost that drives the lap at real speed
- A vision cone that does not flicker at the edge
- Move a group of units without a pile-up
- A sprinkler that waters a round patch
- A slot reel that always lands forward on the result
- A crowd of NPCs that never emote or bob in step
- A song position that moves on every frame
- Nodes your editor tool adds that stay in the scene