- Home /
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 :)
Answer by Seth-Bergman · Jul 26, 2012 at 12:25 AM
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
At the risk of sounding like a complete idiot :p is this one script or two ?
Answer by Nomibuilder · Apr 08, 2015 at 07:18 AM
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. Link
Answer by Moomooz · Jul 26, 2012 at 02:05 AM
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 :) ,I'll work on thru the night and crack this hopefully :)
Getting there :)
Not quite working first attempt above lol :)
Answer by Moomooz · Aug 01, 2012 at 08:01 PM
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;
}
This worked for me but if you go backwards it'll still increment therefore you can cheat. How can I prevent this? And how can I set a lap limit to end the race?
Your answer
Follow this Question
Related Questions
Question about rigidbody velocity 1 Answer
How do I create a mesh collider? 2 Answers
Beginner Car Control Script from FlatTutorials 3 Answers
UNEXPECTED TOKEN ) 1 Answer
Stuck in Car tutorial - Wheels 0 Answers