How to build a lap system for racing game

I already have a checkpoint system in my racing game.
But I want to accurately calculate the distance in any of the following examples/methods:
[11667-lap+system.jpg|11667]

Example 1 (see the image): Track distance between two checkpoints.

Example 2 (see the image): Track distance between distinct checkpoints.

Example 3 (see the image): Distance to be traveled in the entire track(lap).

I want to use this distance to calculate the player position.

Can anyone guide me on any of these methods.

Note: I don’t want to calculate the linear distance between the checkpoint objects, but I need to calculate the distance traveled on the track.

Thank you for your attention and I would be very grateful for any help.

God bless you.

If you are using C# Language. Then here is the C# version of CheckPoint and Laps system.

Laps Script

using UnityEngine;
using System.Collections;

public class Laps : MonoBehaviour {
	
	// These Static Variables are accessed in "checkpoint" Script
	public Transform[] checkPointArray;
	public static Transform[] checkpointA;
	public static int currentCheckpoint = 0; 
	public static int currentLap = 0; 
	public Vector3 startPos;
	public int Lap;
	
	void  Start ()
	{
		startPos = transform.position;
		currentCheckpoint = 0;
		currentLap = 0; 

	}

	void  Update ()
	{
		Lap = currentLap;
		checkpointA = checkPointArray;
	}
	
}

Check Point Script

using UnityEngine;
using System.Collections;

public class checkpoint : MonoBehaviour {
	
	void  Start ()
	{

	}
	
	void  OnTriggerEnter ( Collider other  )
	{
		//Is it the Player who enters the collider?
		if (!other.CompareTag("Player")) 
			return; //If it's not the player dont continue
		

		if (transform == Laps.checkpointA[Laps.currentCheckpoint].transform) 
		{
			//Check so we dont exceed our checkpoint quantity
			if (Laps.currentCheckpoint + 1 < Laps.checkpointA.Length) 
			{
				//Add to currentLap if currentCheckpoint is 0
				if(Laps.currentCheckpoint == 0)
					Laps.currentLap++;
				Laps.currentCheckpoint++;
			} 
			else 
			{
				//If we dont have any Checkpoints left, go back to 0
				Laps.currentCheckpoint = 0;
			}
		}


	}

}

And Just Follow this tutorial to attach scripts on your Game Objects. 1

initialize a variable (like ‘distanceTraveled’) to 0. On every update, find the distance between the last position and the current position, store the current position into a last position var, and add the distance to distanceTraveled.

You could have multiple variables for each distance that you want to track.

distanceTraveledFromStart

distanceTraveledFromLastCheckPoint

distanceTraveledThisLap

etc…

Explaining my image:

•The blue cubes are CP (Checkpoints/Waypoints)

•The green line is just drawing the track progress.

Explaining the CP system:

•When I pass a CP (trigger), a ‘counter’ storage my current CP by a ‘int’.

•When this counter reach the maximum number of CPs in the track, a var named ‘lap’ get a increment.

•I check the current CP and calculate the distance between the car and the CP using ‘Vector3.Distance’

Conditions:

if('Opp lap'   >   'My lap')
{
	//I'm in second place
}
else if('Opp lap' == 'My lap'  &  'Opp CP' > 'My CP')
{
	//I'm in second place
}
else if('Opp CP' == 'My CP' && 'Opp distance' < 'My distance')
{
	//I'm in second place
}
else
	//I'm in first place

‘My Lap’ = My current lap

‘My CP’ = My current checkpoint

‘Opp lap’ = Opponent current lap

‘Opp CP’ = Opponent current checkpoint

‘My distance’ = Distance of the player between next checkpoint

‘Opp distance’ = Distance of the opponent between next checkpoint


This is working fine with two drivers/racers. I’m working now to get this working with more drivers/racers.

I hope I have helped you.

What I’d probably do would be to define the track with some loop line. I’d calculate the position on the line by looking for the closest point of the car to the line. Here you’d also need some way how to prevent discontinuities (when your car goes off the track and closer to some other part of the loop, by somehow “dragging” the control point over the line, like it was a rail.