Add Checkpoints and Laps Unity Car Tutorial

Hey there ,please please can someone point me in the right direction of adding a lap timer and checkpoint system to the Unity Car Tutorial ,I have tried loads of systems but cannot get one to work ,people say it’s easy but I am stumped on this lol,any help will be greatly appreciated ,Its fine if its not possible ,I will stick to the Jcar one ,just would like another option if possible :slight_smile:

Laps and checkpoint sysyem

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

for the timer:

var time : float;
var lapTime : float;

function Update(){
time = Time.time - lapTime;
}

function OnGUI(){
GUI.Box (Rect (10,10,110,100), "current lap time:" + time);
}

function StartNewLap(){
lapTime = Time.time;
}

then use empty box colliders set to isTrigger for your checkpoints, and tag them:

function OnTriggerEnter(hit : Collider){
if(hit.gameObject.tag == "Checkpoint")
//add more time?
if(hit.gameObject.tag == "NewLap")
StartNewLap();
}

(this script would be on your car)
hope this gets you started

thanks ,I tried that and no errors:) (good sign) ,i’ll work on from there and see what I can do ,the way you have laid it out does seem simple :slight_smile: ,I’ll work on thru the night and crack this hopefully :slight_smile:

Getting there :slight_smile:

Not quite working first attempt above lol :slight_smile:

var time : float;
var lapTime : float;
var ckp = 0;
var lap = 0;
function Update(){
time = Time.time - lapTime;
}

function OnGUI(){

GUI.Box (Rect (10,10,110,100), “current lap time:” + time);

GUI.Box (Rect (120,120,110,100), "Checkpoint : " + ckp/2);

GUI.Box (Rect (240,10,110,100), “Laps” + lap);

if (lap==2)
GUI.Box(Rect(500,500,200,200), "Race Won !! ");
else;
}

function OnTriggerEnter(hit : Collider){
if(hit.gameObject.tag == “Checkpoint1”)
ckp=ckp +1;
if(hit.gameObject.tag == “Checkpoint2”)
ckp=ckp +1;
if(hit.gameObject.tag == “Checkpoint3”)
ckp=ckp +1;
if(hit.gameObject.tag == “Checkpoint4”)
ckp=ckp +1;
if(hit.gameObject.tag == “Checkpoint5”)
ckp=ckp +1;
if(hit.gameObject.tag == “Checkpoint6”)
ckp=ckp +1;
if(hit.gameObject.tag == “Checkpoint7”)
ckp=ckp +1;
if(hit.gameObject.tag == “Checkpoint8”)
ckp=ckp +1;
if(hit.gameObject.tag == “Checkpoint9”)
ckp=ckp +1;
if(hit.gameObject.tag == “Checkpoint10”)
ckp=ckp +1;

if (ckp==20) lap=1;
if (ckp==40) lap=2;
if (ckp==60) lap=3;
if (ckp==80) lap=4;
if (ckp==100) lap=5;

}