Race Position help

I am working on making a position indicator when you finish the race. I found this code that make an ‘array’ each lap.

I changed the code slightly to make it so only objects with colliders tagged ‘Player’ or ‘Enemy’ will work.

here is my code.

#pragma strict

function Start () {

}

function Update () {

}

     public var position : GameObject[];
    var cont : int = 0;
    function OnTriggerEnter(hit : Collider){
     	//Is it the Player who enters the collider?
	if (!hit.CompareTag("Player")) 
	if (!hit.CompareTag("Enemy")) 
		return; //If it's not the player dont continue
    print("woot");
    position[cont] = hit.gameObject;
    cont++;
     
    }

My question is how do you make it so it will ‘return;’ if you haven’t hit another ‘Trigger’ box collider half way through the race each time? This prevent you from just going through the finish line over and over again or just going straight to the finish line right away.

I have found a script that does it but i’m not sure how. It wont let you activate the next checkpoint trigger collider until you hit the first.

This is checkpoints.js(for the trigger colliders)

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

function Start () {
	playerTransform = gameObject.Find("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(CarCheckpoint).checkPointArray[CarCheckpoint.currentCheckpoint].transform) {
		//Check so we dont exceed our checkpoint quantity
		if (CarCheckpoint.currentCheckpoint + 1<playerTransform.GetComponent(CarCheckpoint).checkPointArray.length) {
			//Add to currentLap if currentCheckpoint is 0
			if(CarCheckpoint.currentCheckpoint == 0)
				CarCheckpoint.currentLap++;
			CarCheckpoint.currentCheckpoint++;
		} else {
			//If we dont have any Checkpoints left, go back to 0
			CarCheckpoint.currentCheckpoint = 0;
		}
		visualAid(); //Run a coroutine to update the visual aid of our Checkpoints
	}
}

function visualAid () {
	//Set a simple visual aid for the Checkpoints
	for (objAlpha in playerTransform.GetComponent(CarCheckpoint).checkPointArray) {
		objAlpha.renderer.material.color.a = 0.2;
	}
	playerTransform.GetComponent(CarCheckpoint).checkPointArray[CarCheckpoint.currentCheckpoint].renderer.material.color.a = 0.8;
}
//start GUI
function OnGUI () {
	// Make a background box
    GUI.TextArea (Rect (10,10,60,20), " Lap: "+(CarCheckpoint.currentLap));
}
//end

This is CarCheckpoint.js(for vehicle)

#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

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;
}

The best way to check car positions is to have a desired racing line (or just one that goes through the middle of the track) using a beizer curve (plenty on the asset store).

Then you find the nearest point on that curve (asset store biezer curves have functions for this) and find out how far along you are on that curve which will be from 0-1.

Add that figure to the car’s lap count and then you’ll have a sortable list of positions.

Hey, would you mind saying exactly which package on the asset store has biezer curves as well as methods for finding the nearest point on it? It would be very helpful as I haven’t had any luck finding one yet…

Thanks!

Is there a way i could check which checkpoint i am on and only allow positions to be added if your on the right checkpoint?

This really stumping me. I’ve got it so the enemies register but the player doesn’t. Here is my new code
This is Postion.js

#pragma strict
var PreviousCheckpoint : float = 13;
function Start () {

}

function Update () {

}

     public var position : GameObject[];
    var cont : int = 0;
    function OnTriggerEnter(hit : Collider){
     	//Is it the Player who enters the collider?
	if (!hit.CompareTag("Player")) 
	if (!hit.CompareTag("Enemy")) 
		return; //If it's not the player dont continue
    if (hit.CompareTag ("Player")) { //If they are tagged player
    if(CarCheckpoint.currentCheckpoint == PreviousCheckpoint){ //THIS IS WHERE THE ISSUE IS!?
    print("Player Enter");
    position[cont] = hit.gameObject;
    cont++;
    }
    }
    if (hit.CompareTag ("Enemy")) { //If they are tagged enemy
    print("Enemy Enter");
    position[cont] = hit.gameObject;
    cont++;
    }
    }

I am trying to get the CarCheckpoint.js to see what checkpoint you are on in the array and only allow you to hit the postion indicator if the checkpoint is the correct one. I’ve tried a making the position script its own trigger collider and i’ve tried adding it to the final checkpoint. Both result in the same effect, enemies register but player does not. :frowning:

You never update PreviousCheckpoint .

I could be wrong(likely am) but i use PreviousCheckpoint to store the number for the checkpoint before the last trigger collider(Element 13) in the checkpoint array. My last one that has the ‘position.js’ is (Element 14) in the checkpoint array. Your saying i need to update the variable each frame or it wont work? I’ve tried 12,13,14,15 for PreviousCheckpoint with no change.

Sorry I just had the wrong number(feel dumb). 14 not 13. it worked must have missed it last time. but ya the last code i posted will work for those requiring the checkpoint system to be at the correct location for the position script to work.

Thanks wccrawford, trooper!!!