- Home /
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... :D
Unity Answers is not a script writing request service.
I took the liberty to change the title of your question so people would find the solution more easily.
Can anyone tell me how I can attach these two scripts to the Unity car tutorial please ?
Answer by save · Aug 02, 2011 at 12:31 AM
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;
}
Your zeal in this regard is admirable, dude. I'd plus it more than 1 if I could. I can't help it but fear you're fighting an uphill battle, though...
If i may add, it could also be solved via triggers and iterators :)
just a guess of course^^
also, great answer as usual :)
@biohazard That's right, this is sort of an iteration, but I see your point of method. @Christian-H-Pedersen "When a day starts like this it's all uphill from here". :-)
Thanks for your answer my $$anonymous$$aster...my game is completed now....
Good thing @ourchat. You could mark the above answer as correct to make people know that this has been solved (it will also give you karma closing up questions). @$$anonymous$$eltdown is also pointing to a good solution.
Answer by Nomibuilder · Apr 08, 2015 at 07:16 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 Maulik2208 · Jan 02, 2013 at 05:03 AM
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.....
Your answer
![](https://koobas.hobune.stream/wayback/20220612164043im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Race checkpoint system small help please :) 1 Answer
Racing Game - setting up a car TIRES 1 Answer
How to build a lap system for racing game 4 Answers
Show laptime after each lap ? 1 Answer