How do I make laps in a racing game?

Anyone please help me to created easy LAP system for my Racing Game…
You are MASTER if can help me… :smiley:

This question gets asked now and then, so I thought it would be good to once and for all show an easy example of how checkpoints and laps can be achieved. In a few simple steps you can count laps around a course with the help of triggers. This is one way to skin the cat, or crack the nut if you will:

First of all we need an object that will trigger the checkpoint areas, a player for instance. To the player we add the ability to keep track of our current checkpoint number, the current lap and storage of all checkpoints as GameObjects in an array.

Player

#pragma strict

var checkPointArray : Transform[]; //Checkpoint GameObjects stored as an array
static var currentCheckpoint : int = 0; //Current checkpoint
static var currentLap : int = 0; //Current lap
static var startPos : Vector3; //Starting position
private var moveMultiplier : int = 8; //Multiplies the Input.GetAxis movement of our player

function Start () {
	//Set a simple visual aid for the Checkpoints
	for (objAlpha in checkPointArray) {
		objAlpha.renderer.material.color.a = 0.2;
	}
	checkPointArray[0].renderer.material.color.a = 0.8;
	
	//Store the starting position of the player
	startPos = transform.position;
}

function FixedUpdate () {
	//Movement for the player (Standard Input: Arrow Keys/W,A,S,D)
	rigidbody.AddForce(Input.GetAxis("Horizontal")*moveMultiplier, 0, Input.GetAxis("Vertical")*moveMultiplier);
}

The checkpoints will be triggered if it’s the correct checkpoint according to the currentCheckpoint variable (which the player keeps track of). If that variable exceeds our current available number of checkpoints we add to number of laps and reset the currentCheckpoint variable.

Checkpoints

#pragma strict

static var playerTransform : Transform; //Store the player transform

function Start () {
	playerTransform = gameObject.Find("Sphere (Player)").transform; //Set the player transform
}

function OnTriggerEnter (other : Collider) {
	//Is it the Player who enters the collider?
	if (!other.CompareTag("Player")) 
		return; //If it's not the player dont continue
		
	//Is this transform equal to the transform of checkpointArrays[currentCheckpoint]?
	if (transform==playerTransform.GetComponent(player).checkPointArray[player.currentCheckpoint].transform) {
		//Check so we dont exceed our checkpoint quantity
		if (player.currentCheckpoint+1<playerTransform.GetComponent(player).checkPointArray.length) {
			//Add to currentLap if currentCheckpoint is 0
			if(player.currentCheckpoint==0)
				player.currentLap++;
			player.currentCheckpoint++;
		} else {
			//If we dont have any Checkpoints left, go back to 0
			player.currentCheckpoint=0;
		}
		visualAid(); //Run a coroutine to update the visual aid of our Checkpoints
		//Update the 3dtext
		Camera.main.GetComponentInChildren(TextMesh).text = "Checkpoint: "+(player.currentCheckpoint)+" Lap: "+(player.currentLap);
	}
}

function visualAid () {
	//Set a simple visual aid for the Checkpoints
	for (objAlpha in playerTransform.GetComponent(player).checkPointArray) {
		objAlpha.renderer.material.color.a = 0.2;
	}
	playerTransform.GetComponent(player).checkPointArray[player.currentCheckpoint].renderer.material.color.a = 0.8;
}

See online demo

Download the source

Check out Step #5 of this excellent car introductory tutorial.
There is a good summary on implementing waypoints and getting AI cars to follow them.

Although for more robust AI you would want to look at implementing UnitySteer.

LAPS AND CHECKPOINT SYSTEM

You Might want to check this

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

Go to youtube and find for the same you will surely get it with demo enjoy…and for your kind information this question is massively repeated question on unity answer so you can also check that as well…cheers enjoy…