- Home /
Lap timer using colliders
Hi everyone, I hope you can help me with a problem I have been having, I am trying to include a lap timer that restarts after each lap (pretty normal) using the code that I already have, I need it to simply start when it passes the first collider/sector and finish when it reaches that same one again (i.e. one full lap).
I looked up how to set up the track and got my answer from here too, my track is set up by creating 3 trigger boxes around the track, the code then checks to see the triggers had been gone through in order, once this is done the next lap begins.
this is my sectors script which checks all the sectors have been navigated (the script is placed onto all of my checkpoints/sectors
static var playerTransform : Transform;
function Start () {
playerTransform = gameObject.Find("Nissan_GTR").transform;
}
function OnTriggerExit (other : Collider) {
//check if this transform equals current checkpoint transform
if (transform == playerTransform.GetComponent(Car).sectorArray[Car.currentSector].transform) {
//do not exceed checkpoint quantity
if (Car.currentSector + 1 < playerTransform.GetComponent(Car).sectorArray.length) {
//add lap if completed all checkpoints
if(Car.currentSector == 0){
Car.currentLap++;
}
Car.currentSector++;
}
else {
//no checkpoints remain, set to 0
Car.currentSector = 0;
}
Camera.main.GetComponentInChildren(TextMesh).text = " Lap " + (Car.currentLap);
}
}
This is my one of my car scripts that is placed onto the car
var sectorArray : Transform[]; //Checkpoint GameObjects stored as an array
private static public var currentSector : int = 0; //Current checkpoint
private static public var currentLap : int = 0; //Current lap
private static public var startPos : Vector3; //Starting position
function Start(){
startPos = transform.position;
}
Answer by Atrius · Apr 21, 2012 at 03:17 AM
This seems overly complex, but I could be missing something. You should be able to use OnTriggerEnter() on your sections to detect what sector the car is in. Use other.gameObject to reference the car that is colliding with your trigger.
Then if your car script was called LapStats for example you could do:
var lapScript : LapStats = other.gameObject.GetComponent("LapStats");
if (lapScript) {
lapScript.currentSector = sector_number;
}
If it's the final sector of the track you could use the OnTriggerExit to increment the lap counter in the same way.
If you need to track each checkpoint because a person can miss one, then you could simply do a check to confirm that the sector is in sequence, ie. sector_number - 1 = currentSector. Without having to track a variable for each sector per lap. Just make sure they go in sequence.
Just some thoughts, I could be misinterpreting what you are doing.
Your answer
Follow this Question
Related Questions
Change main Camera to another camera with a Trigger then back with a Timer. 1 Answer
Can't make time go down after picking up a coin, 2 Answers
Help with making a triggered gui message 1 Answer
Stop watch for 100 meter dash 1 Answer
Event to be triggered after constant contact with gameobject 2 Answers