← Pixel Dreamer Studio

Count a lap only when the car really went round

Put Area3D checkpoints around the track and count the finish line only after the car has passed all of them in order.

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.

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

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